[asterisk-commits] kpfleming: branch 1.6.1 r192281 - in /branches/1.6.1: ./ include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue May 5 04:15:16 CDT 2009


Author: kpfleming
Date: Tue May  5 04:15:13 2009
New Revision: 192281

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=192281
Log:
Merged revisions 192279 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/trunk

........
  r192279 | kpfleming | 2009-05-05 10:51:06 +0200 (Tue, 05 May 2009) | 5 lines
  
  Ensure that string pools allocated to hold stringfields are properly accounted in MALLOC_DEBUG mode
  
  This commit modifies the stringfield pool allocator to remember the 'owner' of the stringfield manager the pool is being allocated for, and ensures that pools allocated in the future when fields are populated are owned by that file/function.
........

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

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

Modified: branches/1.6.1/include/asterisk/stringfields.h
URL: http://svn.asterisk.org/svn-view/asterisk/branches/1.6.1/include/asterisk/stringfields.h?view=diff&rev=192281&r1=192280&r2=192281
==============================================================================
--- branches/1.6.1/include/asterisk/stringfields.h (original)
+++ branches/1.6.1/include/asterisk/stringfields.h Tue May  5 04:15:13 2009
@@ -138,6 +138,11 @@
 	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 */
+#if defined(__AST_DEBUG_MALLOC)
+	const char *owner_file;				/*!< filename of owner */
+	const char *owner_func;				/*!< function name of owner */
+	int owner_line;					/*!< line number of owner */
+#endif
 };
 
 /*!
@@ -234,15 +239,18 @@
   \return 0 on success, non-zero on failure
 */
 #define ast_string_field_init(x, size) \
-	__ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, size)
+	__ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 
 /*! \brief free all memory - to be called before destroying the object */
 #define ast_string_field_free_memory(x)	\
-	__ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, -1)
-
-/*! \internal \brief internal version of ast_string_field_init */
-int __ast_string_field_init(struct ast_string_field_mgr *mgr,
-	struct ast_string_field_pool **pool_head, int needed);
+	__ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, -1, NULL, 0, NULL)
+
+/*!
+ * \internal
+ * \brief internal version of ast_string_field_init
+ */
+int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
+			    int needed, const char *file, int lineno, const char *func);
 
 /*!
   \brief Set a field to a simple string value

Modified: branches/1.6.1/main/utils.c
URL: http://svn.asterisk.org/svn-view/asterisk/branches/1.6.1/main/utils.c?view=diff&rev=192281&r1=192280&r2=192281
==============================================================================
--- branches/1.6.1/main/utils.c (original)
+++ branches/1.6.1/main/utils.c Tue May  5 04:15:13 2009
@@ -1475,15 +1475,21 @@
  * We can only allocate from the topmost pool, so the
  * 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)
+static int add_string_pool(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
+			   size_t size, const char *file, int lineno, const char *func)
 {
 	struct ast_string_field_pool *pool;
 
-	if (!(pool = ast_calloc(1, sizeof(*pool) + size)))
+#if defined(__AST_DEBUG_MALLOC)
+	if (!(pool = __ast_calloc(1, sizeof(*pool) + size, file, lineno, func))) {
 		return -1;
-	
+	}
+#else
+	if (!(pool = ast_calloc(1, sizeof(*pool) + size))) {
+		return -1;
+	}
+#endif
+
 	pool->prev = *pool_head;
 	*pool_head = pool;
 	mgr->size = size;
@@ -1503,39 +1509,52 @@
  * size < 0 means release all pools.
  *	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)
+int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
+			    int needed, const char *file, int lineno, const char *func)
 {
 	const char **p = (const char **) pool_head + 1;
-	struct ast_string_field_pool *cur = *pool_head;
+	struct ast_string_field_pool *cur = NULL;
+	struct ast_string_field_pool *preserve = NULL;
 
 	/* clear fields - this is always necessary */
 	while ((struct ast_string_field_mgr *) p != mgr)
 		*p++ = __ast_string_field_empty;
 	mgr->last_alloc = NULL;
-	if (size > 0) {			/* allocate the initial pool */
+#if defined(__AST_DEBUG_MALLOC)
+	mgr->owner_file = file;
+	mgr->owner_func = func;
+	mgr->owner_line = lineno;
+#endif
+	if (needed > 0) {		/* allocate the initial pool */
 		*pool_head = NULL;
-		return add_string_pool(mgr, pool_head, size);
-	}
-	if (size < 0) {			/* reset all pools */
-		*pool_head = NULL;
-	} else {			/* preserve the first pool */
-		if (cur == NULL) {
+		return add_string_pool(mgr, pool_head, needed, file, lineno, func);
+	}
+	if (needed < 0) {		/* reset all pools */
+		/* nothing to do */
+	} else {			/* preserve the last pool */
+		if (*pool_head == NULL) {
 			ast_log(LOG_WARNING, "trying to reset empty pool\n");
 			return -1;
 		}
-		cur = cur->prev;
-		(*pool_head)->prev = NULL;
 		mgr->used = 0;
+		preserve = *pool_head;
+		cur = preserve->prev;
+	}
+
+	if (preserve) {
+		preserve->prev = NULL;
 	}
 
 	while (cur) {
 		struct ast_string_field_pool *prev = cur->prev;
 
-		ast_free(cur);
+		if (cur != preserve) {
+			ast_free(cur);
+		}
 		cur = prev;
 	}
+
+	*pool_head = preserve;
 
 	return 0;
 }
@@ -1552,8 +1571,13 @@
 		while (new_size < needed)
 			new_size *= 2;
 
-		if (add_string_pool(mgr, pool_head, new_size))
+#if defined(__AST_DEBUG_MALLOC)
+		if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func))
 			return NULL;
+#else
+		if (add_string_pool(mgr, pool_head, new_size, NULL, 0, NULL))
+			return NULL;
+#endif
 	}
 
 	result = (*pool_head)->base + mgr->used;
@@ -1622,8 +1646,13 @@
 			while (new_size < needed)
 				new_size *= 2;
 			
-			if (add_string_pool(mgr, pool_head, new_size))
+#if defined(__AST_DEBUG_MALLOC)
+			if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func))
 				return;
+#else
+			if (add_string_pool(mgr, pool_head, new_size, NULL, 0, NULL))
+				return;
+#endif
 		}
 
 		target = (*pool_head)->base + mgr->used;




More information about the asterisk-commits mailing list