[svn-commits] kpfleming: branch 1.6.0 r132965 - in /branches/1.6.0: ./ include/asterisk/ main/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Jul 23 11:34:47 CDT 2008


Author: kpfleming
Date: Wed Jul 23 11:34:46 2008
New Revision: 132965

URL: http://svn.digium.com/view/asterisk?view=rev&rev=132965
Log:
Merged revisions 132964 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/trunk

................
r132964 | kpfleming | 2008-07-23 11:30:18 -0500 (Wed, 23 Jul 2008) | 10 lines

Merged revisions 132872 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/branches/1.4

........
r132872 | kpfleming | 2008-07-23 06:52:18 -0500 (Wed, 23 Jul 2008) | 2 lines

minor optimization for stringfields: when a field is being set to a larger value than it currently contains and it happens to be the most recent field allocated from the currentl pool, it is possible to 'grow' it without having to waste the space it is currently using (or potentially even allocate a new pool)

........

................

Modified:
    branches/1.6.0/   (props changed)
    branches/1.6.0/include/asterisk/stringfields.h
    branches/1.6.0/main/utils.c

Propchange: branches/1.6.0/
------------------------------------------------------------------------------
Binary property 'trunk-merged' - no diff available.

Modified: branches/1.6.0/include/asterisk/stringfields.h
URL: http://svn.digium.com/view/asterisk/branches/1.6.0/include/asterisk/stringfields.h?view=diff&rev=132965&r1=132964&r2=132965
==============================================================================
--- branches/1.6.0/include/asterisk/stringfields.h (original)
+++ branches/1.6.0/include/asterisk/stringfields.h Wed Jul 23 11:34:46 2008
@@ -135,9 +135,27 @@
   pool, so the numbers here reflect just that.
 */
 struct ast_string_field_mgr {
-	size_t size;		/*!< the total size of the current pool */
-	size_t used;		/*!< the space used in the current pool */
+	size_t size;				/*!< the total size of the current pool */
+	size_t used;				/*!< the space used in the current pool */
+	ast_string_field last_alloc;		/*!< the last field allocated */
 };
+
+/*!
+  \internal
+  \brief Attempt to 'grow' an already allocated field to a larger size
+  \param mgr Pointer to the pool manager structure
+  \param needed Amount of space needed for this field
+  \param ptr Pointer to a field within the structure
+  \return 0 on success, non-zero on failure
+
+  This function will attempt to increase the amount of space allocated to
+  an existing field to the amount requested; this is only possible if the
+  field was the last field allocated from the current storage pool and
+  the pool has enough space available. If so, the additional space will be
+  allocated to this field and the field's address will not be changed.
+*/
+int __ast_string_field_ptr_grow(struct ast_string_field_mgr *mgr, size_t needed,
+				const ast_string_field *ptr);
 
 /*!
   \internal
@@ -152,7 +170,7 @@
   an additional pool will be allocated.
 */
 ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr,
-	 struct ast_string_field_pool **pool_head, size_t needed);
+						struct ast_string_field_pool **pool_head, size_t needed);
 
 /*!
   \internal
@@ -164,8 +182,8 @@
   \return nothing
 */
 void __ast_string_field_ptr_build(struct ast_string_field_mgr *mgr,
-	struct ast_string_field_pool **pool_head,
-	const ast_string_field *ptr, const char *format, ...);
+				  struct ast_string_field_pool **pool_head,
+				  const ast_string_field *ptr, const char *format, ...);
 
 /*!
   \internal
@@ -179,8 +197,8 @@
   \return nothing
 */
 void __ast_string_field_ptr_build_va(struct ast_string_field_mgr *mgr,
-	struct ast_string_field_pool **pool_head,
-	const ast_string_field *ptr, const char *format, va_list a1, va_list a2);
+				     struct ast_string_field_pool **pool_head,
+				     const ast_string_field *ptr, const char *format, va_list a1, va_list a2);
 
 /*!
   \brief Declare a string field
@@ -233,17 +251,16 @@
   \param data String value to be copied into the field
   \return nothing
 */
-
 #define ast_string_field_ptr_set(x, ptr, data) do { 		\
 	const char *__d__ = (data);				\
-	size_t __dlen__ = (__d__) ? strlen(__d__) : 0;		\
-	const char **__p__ = (const char **)(ptr);		\
-	if (__dlen__ == 0)					\
+	size_t __dlen__ = (__d__) ? strlen(__d__) + 1 : 1;	\
+	const char **__p__ = (const char **) (ptr);		\
+	if (__dlen__ == 1)					\
 		*__p__ = __ast_string_field_empty;		\
-	else if (__dlen__ <= strlen(*__p__))			\
-		strcpy((char *)*__p__, __d__);			\
-	else if ( (*__p__ = __ast_string_field_alloc_space(&(x)->__field_mgr, &(x)->__field_mgr_pool, __dlen__ + 1) ) )	\
-		strcpy((char *)*__p__, __d__);			\
+	else if (!__ast_string_field_ptr_grow(&(x)->__field_mgr, __dlen__, ptr)) \
+		memcpy((char *) *__p__, __d__, __dlen__);	\
+	else if ((*__p__ = __ast_string_field_alloc_space(&(x)->__field_mgr, &(x)->__field_mgr_pool, __dlen__))) \
+		memcpy((char *) *__p__, __d__, __dlen__);	\
 	} while (0)
 
 /*!
@@ -253,10 +270,9 @@
   \param data String value to be copied into the field
   \return nothing
 */
-#define ast_string_field_set(x, field, data)	do {		\
+#define ast_string_field_set(x, field, data) do {		\
 	ast_string_field_ptr_set(x, &(x)->field, data);		\
 	} while (0)
-
 
 /*!
   \brief Set a field to a complex (built) value

Modified: branches/1.6.0/main/utils.c
URL: http://svn.digium.com/view/asterisk/branches/1.6.0/main/utils.c?view=diff&rev=132965&r1=132964&r2=132965
==============================================================================
--- branches/1.6.0/main/utils.c (original)
+++ branches/1.6.0/main/utils.c Wed Jul 23 11:34:46 2008
@@ -1318,7 +1318,8 @@
  * fields in *mgr reflect the size of that only.
  */
 static int add_string_pool(struct ast_string_field_mgr *mgr,
-	struct ast_string_field_pool **pool_head, size_t size)
+			   struct ast_string_field_pool **pool_head,
+			   size_t size)
 {
 	struct ast_string_field_pool *pool;
 
@@ -1329,6 +1330,7 @@
 	*pool_head = pool;
 	mgr->size = size;
 	mgr->used = 0;
+	mgr->last_alloc = NULL;
 
 	return 0;
 }
@@ -1344,14 +1346,16 @@
  *	This must be done before destroying the object.
  */
 int __ast_string_field_init(struct ast_string_field_mgr *mgr,
-	struct ast_string_field_pool **pool_head, int size)
-{
-	const char **p = (const char **)pool_head + 1;
+			    struct ast_string_field_pool **pool_head,
+			    int size)
+{
+	const char **p = (const char **) pool_head + 1;
 	struct ast_string_field_pool *cur = *pool_head;
 
 	/* clear fields - this is always necessary */
-	while ((struct ast_string_field_mgr *)p != mgr)
+	while ((struct ast_string_field_mgr *) p != mgr)
 		*p++ = __ast_string_field_empty;
+	mgr->last_alloc = NULL;
 	if (size > 0) {			/* allocate the initial pool */
 		*pool_head = NULL;
 		return add_string_pool(mgr, pool_head, size);
@@ -1367,16 +1371,19 @@
 		(*pool_head)->prev = NULL;
 		mgr->used = 0;
 	}
+
 	while (cur) {
 		struct ast_string_field_pool *prev = cur->prev;
+
 		ast_free(cur);
 		cur = prev;
 	}
+
 	return 0;
 }
 
 ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr,
-	struct ast_string_field_pool **pool_head, size_t needed)
+						struct ast_string_field_pool **pool_head, size_t needed)
 {
 	char *result = NULL;
 	size_t space = mgr->size - mgr->used;
@@ -1393,17 +1400,41 @@
 
 	result = (*pool_head)->base + mgr->used;
 	mgr->used += needed;
+	mgr->last_alloc = result;
 	return result;
+}
+
+int __ast_string_field_ptr_grow(struct ast_string_field_mgr *mgr, size_t needed,
+				const ast_string_field *ptr)
+{
+	int grow = needed - (strlen(*ptr) + 1);
+	size_t space = mgr->size - mgr->used;
+
+	if (grow <= 0) {
+		return 0;
+	}
+
+	if (*ptr != mgr->last_alloc) {
+		return 1;
+	}
+
+	if (space < grow) {
+		return 1;
+	}
+
+	mgr->used += grow;
+
+	return 0;
 }
 
 __attribute((format (printf, 4, 0)))
 void __ast_string_field_ptr_build_va(struct ast_string_field_mgr *mgr,
-	struct ast_string_field_pool **pool_head,
-	const ast_string_field *ptr, const char *format, va_list ap1, va_list ap2)
+				     struct ast_string_field_pool **pool_head,
+				     const ast_string_field *ptr, const char *format, va_list ap1, va_list ap2)
 {
 	size_t needed;
 	char *dst = (*pool_head)->base + mgr->used;
-	const char **p = (const char **)ptr;
+	const char **p = (const char **) ptr;
 	size_t space = mgr->size - mgr->used;
 
 	/* try to write using available space */
@@ -1424,14 +1455,14 @@
 		vsprintf(dst, format, ap2);
 	}
 
-	*p = dst;
+	mgr->last_alloc = *p = dst;
 	mgr->used += needed;
 }
 
 __attribute((format (printf, 4, 5)))
 void __ast_string_field_ptr_build(struct ast_string_field_mgr *mgr,
-	struct ast_string_field_pool **pool_head,
-	const ast_string_field *ptr, const char *format, ...)
+				  struct ast_string_field_pool **pool_head,
+				  const ast_string_field *ptr, const char *format, ...)
 {
 	va_list ap1, ap2;
 




More information about the svn-commits mailing list