argus 3.0.0 rc.28 memory leaks

Gabriel L. Somlo somlo at cmu.edu
Mon Sep 18 16:22:39 EDT 2006


Third time's a charm. I screwed up a curly bracket earlier today; the
posix_memalign() / autoconf patch is attached. 

Thanks,
Gabriel

On Mon, Sep 18, 2006 at 01:35:29PM -0400, Gabriel L. Somlo wrote:
...
> I redid the second one (for posix_memalign()) so that autoconf would
> detect at build time if posix_memalign() is present, and fall back to
> the old magic tricks if it isn't... Code still looks simpler and
> easier to read, and life is good...
...
-------------- next part --------------
diff -NarU5 argus-3.0.0.rc.29.orig/common/argus_util.c argus-3.0.0.rc.29/common/argus_util.c
--- argus-3.0.0.rc.29.orig/common/argus_util.c	2006-09-08 15:08:08.000000000 -0400
+++ argus-3.0.0.rc.29/common/argus_util.c	2006-09-18 16:11:51.000000000 -0400
@@ -1011,12 +1011,13 @@
    fflush(stdout);
 }
 
 
 
+#define _XOPEN_SOURCE 600
 #include <stdlib.h>
-#define ARGUS_ALIGN_128	1
+#define ARGUS_ALIGN 128
 
 struct ArgusMemoryHeader {
    struct ArgusMemoryHeader *nxt, *prv;
 #if defined(__GNUC__)
    void *frame[4];
@@ -1033,182 +1034,106 @@
 
 int ArgusAllocTotal = 0;
 int ArgusAllocBytes = 0;
 int ArgusFreeTotal = 0;
 
-struct ArgusMemoryList memory = {NULL, 0};
+#ifndef HAVE_POSIX_MEMALIGN
+int argus_memalign(void **memptr, size_t alignment, size_t size) {
+  void *mem;
+  unsigned short offset;
 
-#define ARGUS_ALLOC		0x45672381
+  if ((mem = malloc(size + alignment)) == NULL) {
+    *memptr = NULL;
+    return(-1);
+  }
+
+  offset = alignment - ((size_t)mem & (alignment - 1));
+  mem = (void *)((unsigned short *)mem + offset);
+  ((unsigned short *)mem)[-1] = offset;
+
+  *memptr = mem;
+  return(0);
+}
+
+void argus_memalign_free(void *mem) {
+  unsigned short offset;
+
+  if ((offset = ((unsigned short *)mem)[-1]) > 0)
+    mem = (void *)((unsigned short *)mem - offset);
+
+  free(mem);
+}
+#endif
 
 void *     
 ArgusMalloc (int bytes) 
 {          
-   void *retn = NULL; 
-   int offset = 0;
+   void *retn; 
  
-   if (bytes) {
+   if (bytes > 0) {
       ArgusAllocTotal++;
       ArgusAllocBytes += bytes;
 
-#if defined(ARGUS_ALIGN_128)
-      offset = 128;
-#endif
-
-#if defined(ARGUSMEMDEBUG)
-      if ((retn = (u_int *) malloc (bytes + sizeof(struct ArgusMemoryHeader) + offset)) != NULL) {
-         struct ArgusMemoryHeader *mem = (struct ArgusMemoryHeader *)retn;
-         mem->tag = ARGUS_ALLOC;
-         mem->len = bytes;
-         mem->offset = offset;
-#if defined(__GNUC__)
-         mem->frame[0] = __builtin_return_address(0);
-         mem->frame[1] = __builtin_return_address(1);
-         mem->frame[2] = __builtin_return_address(2);
-#endif
-         if (memory.start) {
-            mem->nxt = memory.start;
-            mem->prv = memory.start->prv;
-            mem->prv->nxt = mem;
-            mem->nxt->prv = mem;
-            memory.end = mem;
-         } else {
-            memory.start = mem;
-            memory.end = mem;
-            mem->nxt = mem;
-            mem->prv = mem;
-         }
-         memory.count++;
-         memory.total++;
-         retn = (void *)(mem + 1);
-      }
+#ifdef HAVE_POSIX_MEMALIGN
+      if (posix_memalign(&retn, ARGUS_ALIGN, bytes) != 0)
 #else
-      retn = (void *) malloc (bytes + offset);
+      if (argus_memalign(&retn, ARGUS_ALIGN, bytes) != 0)
 #endif
-
-      if (retn != NULL) {
-#if defined(ARGUS_ALIGN_128)
-         unsigned short toff;
-         toff = ((unsigned long)retn & (offset - 1));
-         toff = offset - toff;
-         retn = (void *)((char *)retn + toff);
-         ((unsigned short *)retn)[-1] = toff;
-#endif
-      }
+         retn = NULL;
    }
+
 #ifdef ARGUSDEBUG
    ArgusDebug (6, "ArgusMalloc (%d) returning 0x%x\n", bytes, retn); 
 #endif
+
    return (retn); 
 }
 
 void *
 ArgusCalloc (int nitems, int bytes)
 {
-   int offset = 0, total = nitems * bytes;
-   void *retn = NULL;
+   int total = nitems * bytes;
+   void *retn;
 
    if (total) {
       ArgusAllocTotal++;
       ArgusAllocBytes += total;
 
-#if defined(ARGUS_ALIGN_128)
-      offset = 128;
-#endif
-
-#if defined(ARGUSMEMDEBUG)
-      if ((retn = calloc (1, total + sizeof(struct ArgusMemoryHeader) + offset)) != NULL) {
-         struct ArgusMemoryHeader *mem = retn;
-         mem->tag = ARGUS_ALLOC;
-         mem->len = total;
-         mem->offset = offset;
-#if defined(__GNUC__)
-         mem->frame[0] = __builtin_return_address(0);
-         mem->frame[1] = __builtin_return_address(1);
-         mem->frame[2] = __builtin_return_address(2);
-#endif
-         if (memory.start) {
-            mem->nxt = memory.start;
-            mem->prv = memory.start->prv;
-            mem->prv->nxt = mem;
-            mem->nxt->prv = mem;
-            memory.end = mem;
-         } else {
-            memory.start = mem;
-            memory.end = mem;
-            mem->nxt = mem;
-            mem->prv = mem;
-         }
-         memory.total++;
-         memory.count++;
-         retn = (void *)(mem + 1);
-      }
+#ifdef HAVE_POSIX_MEMALIGN
+      if (posix_memalign(&retn, ARGUS_ALIGN, total) != 0) {
 #else
-      retn = calloc (1, total + offset);
+      if (argus_memalign(&retn, ARGUS_ALIGN, bytes) != 0) {
 #endif
-
-#if defined(ARGUS_ALIGN_128)
-      if (retn != NULL) {
-         unsigned short toff;
-         toff = ((unsigned long)retn & (offset - 1));
-         toff = offset - toff;
-         retn = (void *)((char *)retn + toff);
-         ((unsigned short *)retn)[-1] = toff;
+         retn = NULL;
+      } else {
+         /* we're a "calloc" replacement, so zero out the block: */
+         memset(retn, 0, total);
       }
-#endif
    }
 
 #ifdef ARGUSDEBUG
    ArgusDebug (6, "ArgusCalloc (%d, %d) returning 0x%x\n", nitems, bytes, retn);
 #endif
+
    return (retn);
 }
 
 
 void
 ArgusFree (void *buf)
 {
-   void *ptr = buf;
-
-   if (ptr) {
+   if (buf) {
       ArgusFreeTotal++;
-#if defined(ARGUSMEMDEBUG)
-      {
-         struct ArgusMemoryHeader *mem = ptr;
-#if defined(ARGUS_ALIGN_128)
-         unsigned short offset = ((unsigned short *)mem)[-1];
-         mem = (void *)((char *)mem - offset);
-#endif
-         mem--;
-         if (mem->tag != ARGUS_ALLOC)
-            ArgusLog (LOG_ERR, "ArgusFree: buffer error 0x%x", ptr);
-         if (memory.count == 1) {
-            memory.start = NULL;
-            memory.end = NULL;
-         } else {
-            mem->prv->nxt = mem->nxt;
-            mem->nxt->prv = mem->prv;
-            if (mem == memory.start) {
-               memory.start = mem->nxt;
-            } else if (mem == memory.end) {
-               memory.end = mem->prv;
-            }
-         }
-         ArgusAllocBytes -= mem->len;
-         memory.count--;
-         ptr = mem;
-      }
+      /* FIXME: we should decrement ArgusAllocBytes here,
+         but can't know for sure by how much -- GLS */
+#ifdef HAVE_POSIX_MEMALIGN
+      free (buf);
 #else
-#if defined(ARGUS_ALIGN_128)
-      {
-         unsigned short offset;
-         if ((offset = ((unsigned short *)ptr)[-1]) > 0)
-            ptr = (void *)((char *)ptr - offset);
-      }
+      argus_memalign_free (buf);
 #endif
-#endif
-      free (ptr);
    }
+
 #ifdef ARGUSDEBUG
    ArgusDebug (6, "ArgusFree (0x%x)\n", buf);
 #endif
 }
 
diff -NarU5 argus-3.0.0.rc.29.orig/configure argus-3.0.0.rc.29/configure
--- argus-3.0.0.rc.29.orig/configure	2006-08-18 13:15:14.000000000 -0400
+++ argus-3.0.0.rc.29/configure	2006-09-18 11:52:09.000000000 -0400
@@ -4015,11 +4015,12 @@
 fi
 done
 
 
 
-for ac_func in setlinebuf alarm
+
+for ac_func in setlinebuf alarm posix_memalign
 do
 as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
 echo "$as_me:$LINENO: checking for $ac_func" >&5
 echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6
 if eval "test \"\${$as_ac_var+set}\" = set"; then
diff -NarU5 argus-3.0.0.rc.29.orig/configure.in argus-3.0.0.rc.29/configure.in
--- argus-3.0.0.rc.29.orig/configure.in	2006-08-18 13:15:14.000000000 -0400
+++ argus-3.0.0.rc.29/configure.in	2006-09-18 11:52:04.000000000 -0400
@@ -27,11 +27,11 @@
 AC_PROG_MAKE_SET
 AC_PROG_RANLIB
 
 AC_REPLACE_FUNCS(vfprintf strcasecmp strlcat strlcpy strdup)
 AC_CHECK_FUNCS(strftime)
-AC_CHECK_FUNCS(setlinebuf alarm)
+AC_CHECK_FUNCS(setlinebuf alarm posix_memalign)
 
 AC_CHECK_HEADERS(sys/bitypes.h)
 
 AC_CHECK_TYPE([int8_t], ,
 	[AC_DEFINE([int8_t], [signed char],


More information about the argus mailing list