[asterisk-commits] tilghman: branch tilghman/malloc_hold r212711 - /team/tilghman/malloc_hold/main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Aug 17 16:41:34 CDT 2009


Author: tilghman
Date: Mon Aug 17 16:41:30 2009
New Revision: 212711

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=212711
Log:
Optimize realloc() in preparation for more debugging fun

Modified:
    team/tilghman/malloc_hold/main/astmm.c

Modified: team/tilghman/malloc_hold/main/astmm.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/tilghman/malloc_hold/main/astmm.c?view=diff&rev=212711&r1=212710&r2=212711
==============================================================================
--- team/tilghman/malloc_hold/main/astmm.c (original)
+++ team/tilghman/malloc_hold/main/astmm.c Mon Aug 17 16:41:30 2009
@@ -503,22 +503,19 @@
 	return ptr;
 }
 
-static inline size_t __ast_sizeof_region(void *ptr)
+static inline struct ast_region *ptr2region(void *ptr)
 {
 	int hash = HASH(ptr);
 	struct ast_region *reg;
-	size_t len = 0;
-	
+
 	ast_mutex_lock(&reglock);
 	for (reg = regions[hash]; reg; reg = reg->next) {
 		if (reg->data == ptr) {
-			len = reg->len;
 			break;
 		}
 	}
 	ast_mutex_unlock(&reglock);
-
-	return len;
+	return reg;
 }
 
 static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
@@ -664,24 +661,32 @@
 void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func) 
 {
 	void *tmp;
-	size_t len = 0;
-
-	if (ptr && !(len = __ast_sizeof_region(ptr))) {
+	struct ast_region *reg = NULL;
+
+	if (ptr && !(reg = ptr2region(ptr))) {
 		astmm_log("WARNING: Realloc of unalloced memory at %p, in %s of %s, "
 			"line %d\n", ptr, func, file, lineno);
 		return NULL;
 	}
 
+#ifdef MALLOC_HOLD
+	if (ptr && (size + sizeof(*reg) < reg->fullsize)) {
+		reg->len = size;
+#else
+	if (ptr && (size + sizeof(*reg) < reg->len)) {
+#endif
+		/* No need to allocate another block */
+		return ptr;
+	}
+
 	if (!(tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func, 0)))
 		return NULL;
 
-	if (len > size)
-		len = size;
 	if (ptr) {
-		memcpy(tmp, ptr, len);
+		memcpy(tmp, ptr, reg->len);
 		__ast_free_region(ptr, file, lineno, func);
 	}
-	
+
 	return tmp;
 }
 




More information about the asterisk-commits mailing list