Update of /usr/cvsroot/asterisk
In directory mongoose.digium.com:/tmp/cvs-serv9930

Modified Files:
        astmm.c 
Log Message:
Print anomlies at free / show memory allocations time


Index: astmm.c
===================================================================
RCS file: /usr/cvsroot/asterisk/astmm.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- astmm.c     14 Sep 2005 20:46:49 -0000      1.21
+++ astmm.c     29 Sep 2005 04:34:11 -0000      1.22
@@ -56,6 +56,8 @@
 #undef free
 #undef vasprintf
 
+#define FENCE_MAGIC 0xdeadbeef
+
 static FILE *mmlog;
 
 static struct ast_region {
@@ -65,6 +67,7 @@
        int lineno;
        int which;
        size_t len;
+       unsigned int fence;
        unsigned char data[0];
 } *regions[SOME_PRIME];
 
@@ -78,8 +81,9 @@
 {
        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(&reglock);
        if (reg) {
                ast_copy_string(reg->file, file, sizeof(reg->file));
@@ -93,6 +97,9 @@
                hash = HASH(ptr);
                reg->next = regions[hash];
                regions[hash] = reg;
+               reg->fence = FENCE_MAGIC;
+               fence = (ptr + reg->len);
+               *fence = FENCE_MAGIC;
        }
        ast_mutex_unlock(&reglock);
        if (!reg) {
@@ -128,6 +135,8 @@
 {
        int hash = HASH(ptr);
        struct ast_region *reg, *prev = NULL;
+       unsigned int *fence;
+
        ast_mutex_lock(&reglock);
        reg = regions[hash];
        while (reg) {
@@ -144,6 +153,21 @@
        }
        ast_mutex_unlock(&reglock);
        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 @@
        struct ast_region *reg;
        unsigned int len = 0;
        int count = 0;
+       unsigned int *fence;
        if (argc > 3) 
                fn = argv[3];
 
@@ -263,6 +288,23 @@
        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;

_______________________________________________
Asterisk-Cvs mailing list
[email protected]
http://lists.digium.com/mailman/listinfo/asterisk-cvs

Reply via email to