[xiph-commits] r13437 - trunk/ezstream/src

moritz at svn.xiph.org moritz at svn.xiph.org
Sat Aug 4 09:46:18 PDT 2007


Author: moritz
Date: 2007-08-04 09:46:17 -0700 (Sat, 04 Aug 2007)
New Revision: 13437

Modified:
   trunk/ezstream/src/xalloc.c
   trunk/ezstream/src/xalloc.h
Log:
Merge xalloc changes.


Modified: trunk/ezstream/src/xalloc.c
===================================================================
--- trunk/ezstream/src/xalloc.c	2007-08-04 16:39:09 UTC (rev 13436)
+++ trunk/ezstream/src/xalloc.c	2007-08-04 16:46:17 UTC (rev 13437)
@@ -71,6 +71,7 @@
 struct memory {
 	RB_ENTRY(memory) entry;
 	void		*ptr;
+	unsigned int	 id;
 	size_t		 size;
 	const char	*allocated_by;
 	unsigned int	 allocated_in_line;
@@ -95,6 +96,7 @@
 
 static unsigned int	  debug_level = 0;
 static FILE		 *debug_output = NULL;
+static unsigned int	  xalloc_next_id = 0;
 static int		  xalloc_initialized = 0;
 static size_t		  xalloc_allocated;
 static size_t		  xalloc_total;
@@ -103,6 +105,7 @@
 static void *		(*real_malloc)(size_t) = NULL;
 static void *		(*real_calloc)(size_t, size_t) = NULL;
 static void *		(*real_realloc)(void *, size_t) = NULL;
+static void		(*real_free)(void *) = NULL;
 static const char	 *unknown_file = "<unknown>";
 
 #ifdef XALLOC_DEBUG
@@ -132,7 +135,7 @@
 		mem->reallocated_by = NULL;
 	if (mem->freed_by != NULL)
 		mem->freed_by = NULL;
-	free(mem);
+	real_free(mem);
 	*mem_p = NULL;
 }
 #endif /* XALLOC_DEBUG */
@@ -249,7 +252,7 @@
 			int	 pret;
 
 			if ((tbuf = real_realloc(buf, *strsiz)) == NULL) {
-				free(buf);
+				real_free(buf);
 				return (-1);
 			}
 			buf = tbuf;
@@ -259,7 +262,7 @@
 			if (pret > 0 && pret < (int)*strsiz)
 				break;
 			if ((int)(*strsiz *= 2) < 0) {
-				free(buf);
+				real_free(buf);
 				return (-1);
 			}
 		}
@@ -303,6 +306,7 @@
 	real_malloc = malloc;
 	real_calloc = calloc;
 	real_realloc = realloc;
+	real_free = free;
 	xalloc_allocated = 0;
 	xalloc_total = 0;
 	xalloc_peak = 0;
@@ -321,7 +325,8 @@
 void
 xalloc_set_functions(void *(*malloc_func)(size_t),
 		     void *(*calloc_func)(size_t, size_t),
-		     void *(*realloc_func)(void *, size_t))
+		     void *(*realloc_func)(void *, size_t),
+		     void (*free_func)(void *))
 {
 	if (!xalloc_initialized)
 		_xalloc_fatal("XALLOC: xalloc_set_functions(): Xalloc library not initialized\n");
@@ -335,6 +340,7 @@
 	real_malloc = malloc_func;
 	real_calloc = calloc_func;
 	real_realloc = realloc_func;
+	real_free = free_func;
 	XALLOC_UNLOCK(xalloc_mutex);
 }
 
@@ -358,8 +364,9 @@
 			RB_REMOVE(memory_tree, &memory_tree_head, mem);
 
 			if (mem->freed_by == NULL) {
-				_xalloc_debug_printf(1, "XALLOC: MEMORY LEAK (%p): allocated in %s:%u, ",
+				_xalloc_debug_printf(1, "XALLOC: MEMORY LEAK (%p:%u): allocated in %s:%u, ",
 						     mem->ptr,
+						     mem->id,
 						     mem->allocated_by,
 						     mem->allocated_in_line);
 				if (mem->reallocated_by != NULL)
@@ -369,7 +376,7 @@
 				_xalloc_debug_printf(1, "leaks %lu bytes\n",
 						     (unsigned long)mem->size);
 				leaked_bytes += mem->size;
-				free(mem->ptr);
+				real_free(mem->ptr);
 			}
 
 			_memory_free(&mem);
@@ -421,7 +428,7 @@
 
 #ifdef XALLOC_DEBUG
 	if (debug_level > 0) {
-		struct memory	*mem;
+		struct memory	*mem, *mem_exists;
 
 		if ((mem = real_calloc(1, sizeof(struct memory))) == NULL)
 			_xalloc_error(errno, "XALLOC: Internal error");
@@ -433,7 +440,16 @@
 			mem->allocated_by = unknown_file;
 		mem->allocated_in_line = line;
 		XALLOC_LOCK(xalloc_mutex);
-		RB_INSERT(memory_tree, &memory_tree_head, mem);
+		mem->id = ++xalloc_next_id;
+		if ((mem_exists = RB_INSERT(memory_tree, &memory_tree_head, mem)) != NULL) {
+			/* Freed pointer is being reused: */
+			if (mem_exists->id != 0)
+				_xalloc_fatal("XALLOC: Internal error: Assertion (mem_exists->id == 0) in %s:%u failed!\n",
+					      __FILE__, __LINE__);
+			RB_REMOVE(memory_tree, &memory_tree_head, mem_exists);
+			_memory_free(&mem_exists);
+			RB_INSERT(memory_tree, &memory_tree_head, mem);
+		}
 		xalloc_allocated += size;
 		xalloc_total += size;
 		if (xalloc_allocated > xalloc_peak)
@@ -469,7 +485,7 @@
 
 #ifdef XALLOC_DEBUG
 	if (ret != NULL && debug_level > 0) {
-		struct memory	*mem;
+		struct memory	*mem, *mem_exists;
 
 		if ((mem = real_calloc(1, sizeof(struct memory))) == NULL)
 			_xalloc_error(errno, "XALLOC: Internal error");
@@ -481,7 +497,16 @@
 			mem->allocated_by = unknown_file;
 		mem->allocated_in_line = line;
 		XALLOC_LOCK(xalloc_mutex);
-		RB_INSERT(memory_tree, &memory_tree_head, mem);
+		mem->id = ++xalloc_next_id;
+		if ((mem_exists = RB_INSERT(memory_tree, &memory_tree_head, mem)) != NULL) {
+			/* Freed pointer is being reused: */
+			if (mem_exists->id != 0)
+				_xalloc_fatal("XALLOC: Internal error: Assertion (mem_exists->id == 0) in %s:%u failed!\n",
+					      __FILE__, __LINE__);
+			RB_REMOVE(memory_tree, &memory_tree_head, mem_exists);
+			_memory_free(&mem_exists);
+			RB_INSERT(memory_tree, &memory_tree_head, mem);
+		}
 		xalloc_allocated += nmemb * size;
 		xalloc_total += nmemb * size;
 		if (xalloc_allocated > xalloc_peak)
@@ -521,6 +546,9 @@
 			if ((mem = real_calloc(1, sizeof(struct memory))) == NULL)
 				_xalloc_error(errno, "XALLOC: Internal error");
 			mem->ptr = ret;
+			XALLOC_LOCK(xalloc_mutex);
+			mem->id = ++xalloc_next_id;
+			XALLOC_UNLOCK(xalloc_mutex);
 			if (file)
 				mem->allocated_by = file;
 			else
@@ -563,7 +591,8 @@
 
 #ifdef XALLOC_DEBUG
 	if (debug_level > 0) {
-		ssize_t diff = nsiz - mem->size;
+		struct memory	*mem_exists;
+		ssize_t 	 diff = nsiz - mem->size;
 
 		XALLOC_LOCK(xalloc_mutex);
 		xalloc_allocated += diff;
@@ -574,7 +603,15 @@
 		if (xalloc_allocated > xalloc_peak)
 			xalloc_peak = xalloc_allocated;
 		mem->size = nsiz;
-		RB_INSERT(memory_tree, &memory_tree_head, mem);
+		if ((mem_exists = RB_INSERT(memory_tree, &memory_tree_head, mem)) != NULL) {
+			/* Freed pointer is being reused: */
+			if (mem_exists->id != 0)
+				_xalloc_fatal("XALLOC: Internal error: Assertion (mem_exists->id == 0) in %s:%u failed!\n",
+					      __FILE__, __LINE__);
+			RB_REMOVE(memory_tree, &memory_tree_head, mem_exists);
+			_memory_free(&mem_exists);
+			RB_INSERT(memory_tree, &memory_tree_head, mem);
+		}
 		XALLOC_UNLOCK(xalloc_mutex);
 	}
 #endif /* XALLOC_DEBUG */
@@ -626,8 +663,9 @@
 				      file ? file : unknown_file, line,
 				      *ptr_p);
 
-		if (mem->freed_by != NULL) {
-			_xalloc_debug_printf(2, "XALLOC: DOUBLE FREE in %s:%u: allocated in %s:%u, ",
+		if (mem->freed_by != NULL && mem->id == 0) {
+			_xalloc_debug_printf(2, "XALLOC: DOUBLE FREE of pointer %p in %s:%u: allocated in %s:%u, ",
+					     mem->ptr,
 					     file ? file : unknown_file, line,
 					     mem->allocated_by,
 					     mem->allocated_in_line);
@@ -643,6 +681,7 @@
 
 		xalloc_freed += mem->size;
 		xalloc_allocated -= mem->size;
+		mem->id = 0;
 		mem->size = 0;
 		if (debug_level > 1) {
 			if (file)
@@ -658,7 +697,7 @@
 	}
 #endif /* XALLOC_DEBUG */
 
-	free(*ptr_p);
+	real_free(*ptr_p);
 #ifdef XALLOC_DEBUG
 	if (debug_level <= 1)
 #endif /* XALLOC_DEBUG */
@@ -691,7 +730,7 @@
 
 # ifdef XALLOC_DEBUG
 	if (debug_level > 0) {
-		struct memory	*mem;
+		struct memory	*mem, *mem_exists;
 
 		if ((mem = real_calloc(1, sizeof(struct memory))) == NULL)
 			_xalloc_error(errno, "XALLOC: Internal error");
@@ -703,7 +742,16 @@
 			mem->allocated_by = unknown_file;
 		mem->allocated_in_line = line;
 		XALLOC_LOCK(xalloc_mutex);
-		RB_INSERT(memory_tree, &memory_tree_head, mem);
+		mem->id = ++xalloc_next_id;
+		if ((mem_exists = RB_INSERT(memory_tree, &memory_tree_head, mem)) != NULL) {
+			/* Freed pointer is being reused: */
+			if (mem_exists->id != 0)
+				_xalloc_fatal("XALLOC: Internal error: Assertion (mem_exists->id == 0) in %s:%u failed!\n",
+					      __FILE__, __LINE__);
+			RB_REMOVE(memory_tree, &memory_tree_head, mem_exists);
+			_memory_free(&mem_exists);
+			RB_INSERT(memory_tree, &memory_tree_head, mem);
+		}
 		xalloc_allocated += strsiz;
 		xalloc_total += strsiz;
 		if (xalloc_allocated > xalloc_peak)

Modified: trunk/ezstream/src/xalloc.h
===================================================================
--- trunk/ezstream/src/xalloc.h	2007-08-04 16:39:09 UTC (rev 13436)
+++ trunk/ezstream/src/xalloc.h	2007-08-04 16:46:17 UTC (rev 13437)
@@ -70,7 +70,8 @@
 
 void	xalloc_set_functions(void *(*)(size_t) /* malloc function */,
 			     void *(*)(size_t, size_t) /* calloc function */,
-			     void *(*)(void *, size_t) /* realloc function */);
+			     void *(*)(void *, size_t) /* realloc function */,
+			     void (*)(void *) /* free function */);
 
 /* Memory leak checks happen during shutdown! */
 void	xalloc_shutdown(void);



More information about the commits mailing list