diff options
author | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 2005-09-29 04:34:11 +0000 |
---|---|---|
committer | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 2005-09-29 04:34:11 +0000 |
commit | 7fa03f76b2f572f27887c4b56b7e4ab1111b16d4 (patch) | |
tree | 458630f670bc5789ca8754c774b906ff91e986aa /astmm.c | |
parent | ef582bac50fb4c2bebed715578e8cd8cdff0a3c2 (diff) |
Print anomlies at free / show memory allocations time
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@6694 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'astmm.c')
-rwxr-xr-x | astmm.c | 44 |
1 files changed, 43 insertions, 1 deletions
@@ -56,6 +56,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #undef free #undef vasprintf +#define FENCE_MAGIC 0xdeadbeef + static FILE *mmlog; static struct ast_region { @@ -65,6 +67,7 @@ static struct ast_region { int lineno; int which; size_t len; + unsigned int fence; unsigned char data[0]; } *regions[SOME_PRIME]; @@ -78,8 +81,9 @@ static inline void *__ast_alloc_region(size_t size, int which, const char *file, { struct ast_region *reg; void *ptr = NULL; + unsigned int *fence; int hash; - reg = malloc(size + sizeof(struct ast_region)); + reg = malloc(size + sizeof(struct ast_region) + sizeof(unsigned int)); ast_mutex_lock(®lock); if (reg) { ast_copy_string(reg->file, file, sizeof(reg->file)); @@ -93,6 +97,9 @@ static inline void *__ast_alloc_region(size_t size, int which, const char *file, hash = HASH(ptr); reg->next = regions[hash]; regions[hash] = reg; + reg->fence = FENCE_MAGIC; + fence = (ptr + reg->len); + *fence = FENCE_MAGIC; } ast_mutex_unlock(®lock); if (!reg) { @@ -128,6 +135,8 @@ static void __ast_free_region(void *ptr, const char *file, int lineno, const cha { int hash = HASH(ptr); struct ast_region *reg, *prev = NULL; + unsigned int *fence; + ast_mutex_lock(®lock); reg = regions[hash]; while (reg) { @@ -144,6 +153,21 @@ static void __ast_free_region(void *ptr, const char *file, int lineno, const cha } ast_mutex_unlock(®lock); if (reg) { + fence = (unsigned int *)(reg->data + reg->len); + if (reg->fence != FENCE_MAGIC) { + fprintf(stderr, "WARNING: Low fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno); + if (mmlog) { + fprintf(mmlog, "%ld - WARNING: Low fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg->file, reg->lineno); + fflush(mmlog); + } + } + if (*fence != FENCE_MAGIC) { + fprintf(stderr, "WARNING: High fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno); + if (mmlog) { + fprintf(mmlog, "%ld - WARNING: High fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg->file, reg->lineno); + fflush(mmlog); + } + } free(reg); } else { fprintf(stderr, "WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno); @@ -254,6 +278,7 @@ static int handle_show_memory(int fd, int argc, char *argv[]) struct ast_region *reg; unsigned int len = 0; int count = 0; + unsigned int *fence; if (argc > 3) fn = argv[3]; @@ -263,6 +288,23 @@ static int handle_show_memory(int fd, int argc, char *argv[]) for (x = 0; x < SOME_PRIME; x++) { reg = regions[x]; while (reg) { + if (!fn || !strcasecmp(fn, reg->file) || !strcasecmp(fn, "anomolies")) { + fence = (unsigned int *)(reg->data + reg->len); + if (reg->fence != FENCE_MAGIC) { + fprintf(stderr, "WARNING: Low fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno); + if (mmlog) { + fprintf(mmlog, "%ld - WARNING: Low fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg-> file, reg->lineno); + fflush(mmlog); + } + } + if (*fence != FENCE_MAGIC) { + fprintf(stderr, "WARNING: High fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno); + if (mmlog) { + fprintf(mmlog, "%ld - WARNING: High fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg->file, reg->lineno); + fflush(mmlog); + } + } + } if (!fn || !strcasecmp(fn, reg->file)) { ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %s\n", (int) reg->len, reg->func, reg->lineno, reg->file); len += reg->len; |