Index: build_tools/cflags.xml
===================================================================
--- build_tools/cflags.xml (revision 184273)
+++ build_tools/cflags.xml (working copy)
@@ -9,6 +9,9 @@
+
+ MALLOC_DEBUG
+
Index: main/astmm.c
===================================================================
--- main/astmm.c (revision 184273)
+++ main/astmm.c (working copy)
@@ -50,7 +50,8 @@
FUNC_STRDUP,
FUNC_STRNDUP,
FUNC_VASPRINTF,
- FUNC_ASPRINTF
+ FUNC_ASPRINTF,
+ FUNC_FREE,
};
/* Undefine all our macros */
@@ -159,11 +160,56 @@
{
int hash = HASH(ptr);
struct ast_region *reg, *prev = NULL;
+#ifndef MALLOC_HOLD
unsigned int *fence;
+#endif
+ if (ptr == NULL) {
+ return;
+ }
+
+#ifdef MALLOC_HOLD
+ /* Mark this segment as freed */
ast_mutex_lock(®lock);
for (reg = regions[hash]; reg; reg = reg->next) {
if (reg->data == ptr) {
+ memset(ptr, reg->len, 0);
+ reg->which = FUNC_FREE;
+ reg->fence = time(NULL);
+ }
+ }
+
+ /* Delete any segment which is free for at least 120 seconds */
+ ast_mutex_lock(®lock);
+ for (reg = regions[hash]; reg; reg = reg->next) {
+ if (reg->which == FUNC_FREE && reg->fence < time(NULL) + 120) {
+ struct ast_region *tbf = reg;
+ unsigned char *ptr;
+ if (prev) {
+ prev->next = reg->next;
+ reg = prev;
+ } else {
+ regions[hash] = reg->next;
+ reg = regions[hash];
+ }
+ for (ptr = tbf->data; ptr < tbf->data + tbf->len; ptr++) {
+ if (*ptr != 0) {
+ astmm_log("WARNING: memory written to after being freed "
+ "at %p, (originally in %s of %s, line %d\n",
+ reg->data, reg->func, reg->file, reg->lineno);
+ break;
+ }
+ }
+ free(tbf);
+ } else {
+ prev = reg;
+ }
+ }
+ ast_mutex_unlock(®lock);
+#else
+ ast_mutex_lock(®lock);
+ for (reg = regions[hash]; reg; reg = reg->next) {
+ if (reg->data == ptr) {
if (prev)
prev->next = reg->next;
else
@@ -189,6 +235,7 @@
astmm_log("WARNING: Freeing unused memory at %p, in %s of %s, line %d\n",
ptr, func, file, lineno);
}
+#endif
}
void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)