CVSROOT    : /cvsroot/undernet-ircu
Module     : ircu2.10
Branch tags: u2_10_11_03
Commit time: 2002-11-24 01:46:11 UTC

Modified files:
  Tag: u2_10_11_03
     ircd/ircd_alloc.c include/ircd_alloc.h ChangeLog

Log message:

- Implemented some memory frobbing on allocation and free. This should help us
  find "Bug #47".

---------------------- diff included ----------------------
Index: ircu2.10/ChangeLog
diff -u ircu2.10/ChangeLog:1.290.2.125.2.19 ircu2.10/ChangeLog:1.290.2.125.2.20
--- ircu2.10/ChangeLog:1.290.2.125.2.19 Sat Nov 23 12:01:35 2002
+++ ircu2.10/ChangeLog  Sat Nov 23 17:46:01 2002
@@ -1,3 +1,10 @@
+2002-11-24  Andrew Miller <[EMAIL PROTECTED]>
+       * include/ircd_alloc.h: Support FROBONMALLOC and FROBONFREE. Turned on
+       now for debugging.
+       
+       * ircd/ircd_alloc.c (MyMalloc,MyRealloc,MyCalloc,MyFree): Made these
+       functions work with frobbing.
+
 2002-11-23  Kevin L Mitchell  <[EMAIL PROTECTED]>
 
        * ircd/m_kick.c (ms_kick): if a kick comes from a user, it should
Index: ircu2.10/include/ircd_alloc.h
diff -u ircu2.10/include/ircd_alloc.h:1.1.46.1 
ircu2.10/include/ircd_alloc.h:1.1.46.1.8.1
--- ircu2.10/include/ircd_alloc.h:1.1.46.1      Thu Jan 10 16:20:32 2002
+++ ircu2.10/include/ircd_alloc.h       Sat Nov 23 17:46:01 2002
@@ -19,11 +19,14 @@
  *
  * Commentary by Bleep (Thomas Helvey)
  *
- * $Id: ircd_alloc.h,v 1.1.46.1 2002/01/11 00:20:32 kev Exp $
+ * $Id: ircd_alloc.h,v 1.1.46.1.8.1 2002/11/24 01:46:01 a1kmm Exp $
  */
 #ifndef INCLUDED_ircd_alloc_h
 #define INCLUDED_ircd_alloc_h
 
+#define FROBONMALLOC
+#define FROBONFREE
+
 /*
  * memory resource allocation and test functions
  */
@@ -39,7 +42,11 @@
 #define INCLUDED_stdlib_h
 #endif
 
+#ifdef FROBONFREE
+extern void MyFree(void *x);
+#else
 #define MyFree(x) do { free((x)); (x) = 0; } while(0)
+#endif
 
 extern void* MyMalloc(size_t size);
 extern void* MyCalloc(size_t nelem, size_t size);
Index: ircu2.10/ircd/ircd_alloc.c
diff -u ircu2.10/ircd/ircd_alloc.c:1.3.2.1 ircu2.10/ircd/ircd_alloc.c:1.3.2.1.8.1
--- ircu2.10/ircd/ircd_alloc.c:1.3.2.1  Thu Jan 10 16:20:32 2002
+++ ircu2.10/ircd/ircd_alloc.c  Sat Nov 23 17:46:00 2002
@@ -19,13 +19,14 @@
  *   along with this program; if not, write to the Free Software
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
- *   $Id: ircd_alloc.c,v 1.3.2.1 2002/01/11 00:20:32 kev Exp $
+ *   $Id: ircd_alloc.c,v 1.3.2.1.8.1 2002/11/24 01:46:00 a1kmm Exp $
  */
 #include "config.h"
 
 #include "ircd_alloc.h"
 #include "ircd_string.h"
 #include "s_debug.h"
+#include <string.h>
 
 #include <assert.h>
 
@@ -47,29 +48,106 @@
   noMemHandler = handler;
 }
 
+#if defined(FROBONFREE) || defined(FROBONMALLOC)
+static void
+memfrob(void *ptr, size_t size)
+{
+  unsigned char *p = ptr, *ep = p + size - 4;
+  while (p <= ep)
+  {
+    *(unsigned long*)p = 0xDEADBEEF;
+    p += 4;
+  }
+  switch (ep - p)
+  {
+  case 3:
+    *(unsigned short*)p = 0xDEAD;
+    p[2] = 0xBE;
+    return;
+  case 2:
+    *(unsigned short*)p = 0xDEAD;
+    return;
+  case 1:
+    *p++ = 0xDE;
+    return;
+  }
+  return;
+}
+#endif
+
 void* MyMalloc(size_t size)
 {
-  void* p = malloc(size);
+  void* p = 
+#ifdef FROBONFREE
+    malloc(size + sizeof(size_t));
+#else
+    malloc(size);
+#endif
   if (!p)
     (*noMemHandler)();
+#ifdef FROBONFREE
+  *(size_t*)p = size;
+  p =  ((size_t*)p) + 1;
+#endif
+#ifdef FROBONMALLOC
+  memfrob(p, size);
+#endif
   return p;
 }
 
-void* MyRealloc(void* p, size_t size)
+void* MyRealloc(void* x, size_t size)
 {
-  void* x = realloc(p, size);
+#ifdef FROBONFREE
+   size_t old_size = ((size_t*)x)[-1];
+   if (old_size > size)
+     memfrob(((char*)x) + size + sizeof(size_t), old_size - size);
+   x = realloc(((size_t*)x) - 1, size + sizeof(size_t));
+#else
+  x = realloc(x, size);
+#endif
   if (!x)
     (*noMemHandler)();
+  /* Both are needed in all cases to work with realloc... */
+#if defined(FROBONMALLOC) && defined(FROBONFREE)
+  if (old_size < size)
+    memfrob(((char*)x) + old_size + sizeof(size_t), size - old_size);
+#endif
+#ifdef FROBONFREE
+  *(size_t*)x = size;
+  x =  ((size_t*)x) + 1;
+#endif
   return x;
 }
 
 void* MyCalloc(size_t nelem, size_t size)
 {
-  void* p = calloc(nelem, size);
+  void* p =
+#ifdef FROBONFREE
+    malloc(nelem * size + sizeof(size_t));
+#else
+    malloc(nelem * size);
+#endif
   if (!p)
     (*noMemHandler)();
+#ifdef FROBONFREE
+  *((size_t*)p) = nelem * size;
+  p = ((size_t*)p) + 1;
+#endif
+  memset(p, 0, size * nelem);
   return p;
 }
+
+#ifdef FROBONFREE
+void
+MyFree(void *p)
+{
+  size_t *stp = (size_t*)p;
+  if (p == NULL)
+    return;
+  memfrob(p, stp[-1]);
+  free(stp - 1);
+}
+#endif
 
 #else /* defined(MDEBUG) */
 /*
----------------------- End of diff -----------------------

Reply via email to