2008-07-22 Mark Probst <mark.probst@gmail.com>
authorMark Probst <mark.probst@gmail.com>
Tue, 22 Jul 2008 14:15:37 +0000 (14:15 -0000)
committerMark Probst <mark.probst@gmail.com>
Tue, 22 Jul 2008 14:15:37 +0000 (14:15 -0000)
* configure.in: New configure option (--with-malloc-mempools) to
make mempools use malloc for every single allocation, which makes
it easier to use debugging/profiling tools like Valgrind.

2008-07-22  Mark Probst  <mark.probst@gmail.com>

* mempool.c: Use malloc for every single mempool allocation if the
configure option is set.  This makes it easier to track mempool
allocations with tools like Valgrind.

svn path=/trunk/mono/; revision=108469

ChangeLog
configure.in
mono/metadata/ChangeLog
mono/metadata/mempool.c

index dac830bdfc1d9c2080aab41259bd210a23e38234..225d86dea07489269342dd34872997b432b4e9c0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2008-07-22  Mark Probst  <mark.probst@gmail.com>
+
+       * configure.in: New configure option (--with-malloc-mempools) to
+       make mempools use malloc for every single allocation, which makes
+       it easier to use debugging/profiling tools like Valgrind.
+
 2008-07-21  Zoltan Varga  <vargaz@gmail.com>
 
        * configure.in: Invoke patch-quiet.sh using AC_CONFIG_COMMANDS so it
index 03c8958faa6972d769a4bd2938345bcd3682f10c..9b292e776f2f0c8d4a5f7bb71f35374806be4436 100644 (file)
@@ -2110,6 +2110,14 @@ AC_ARG_WITH(oprofile,[ --with-oprofile=no,<path>   Enable oprofile support (defa
        fi
 ])
 
+MALLOC_MEMPOOLS=no
+AC_ARG_WITH(malloc_mempools,[ --with-malloc-mempools=yes,no  Use malloc for each single mempool allocation (only for runtime debugging, defaults to NO)],[
+       if test x$with_malloc_mempools = xyes; then
+               MALLOC_MEMPOOLS=yes
+               AC_DEFINE(USE_MALLOC_FOR_MEMPOOLS,1,[Use malloc for each single mempool allocation])
+       fi
+])
+
 AM_CONDITIONAL(HAVE_OPROFILE, test x$OPROFILE = xyes)
 AC_SUBST(OPROFILE_CFLAGS)
 AC_SUBST(OPROFILE_LIBS)
index 93fa68b5ec285e839d674c1be30012c03c69e211..8aad8cad130b557e5c0451cf4dee7319291f1d98 100644 (file)
@@ -1,3 +1,9 @@
+2008-07-22  Mark Probst  <mark.probst@gmail.com>
+
+       * mempool.c: Use malloc for every single mempool allocation if the
+       configure option is set.  This makes it easier to track mempool
+       allocations with tools like Valgrind.
+
 2008-07-22  Jb Evain  <jbevain@novell.com>
 
        * reflection.c (create_dynamic_mono_image): emit the same
index 0c20eef7afbf7468635d09cb51d7d42152cfd9f3..86ad7fa3f67c5944e33a8bb0f6a37177515a7454 100644 (file)
 
 #include "mempool.h"
 
+#if USE_MALLOC_FOR_MEMPOOLS
+#define MALLOC_ALLOCATION
+#endif
+
 /*
  * MonoMemPool is for fast allocation of memory. We free
  * all memory when the pool is destroyed.
 #define G_UNLIKELY(a) (a)
 #endif
 
+#ifdef MALLOC_ALLOCATION
+typedef struct _Chunk {
+       struct _Chunk *next;
+       guint32 size;
+} Chunk;
+
+struct _MonoMemPool {
+       Chunk *chunks;
+       guint32 allocated;
+};
+#else
 struct _MonoMemPool {
        MonoMemPool *next;
        gint rest;
@@ -41,6 +56,7 @@ struct _MonoMemPool {
                guint32 allocated;
        } d;
 };
+#endif
 
 /**
  * mono_mempool_new:
@@ -56,6 +72,9 @@ mono_mempool_new (void)
 MonoMemPool *
 mono_mempool_new_size (int initial_size)
 {
+#ifdef MALLOC_ALLOCATION
+       return g_new0 (MonoMemPool, 1);
+#else
        MonoMemPool *pool;
        if (initial_size < MONO_MEMPOOL_MINSIZE)
                initial_size = MONO_MEMPOOL_MINSIZE;
@@ -66,6 +85,7 @@ mono_mempool_new_size (int initial_size)
        pool->end = pool->pos + initial_size - sizeof (MonoMemPool);
        pool->d.allocated = pool->size = initial_size;
        return pool;
+#endif
 }
 
 /**
@@ -77,6 +97,11 @@ mono_mempool_new_size (int initial_size)
 void
 mono_mempool_destroy (MonoMemPool *pool)
 {
+#ifdef MALLOC_ALLOCATION
+       mono_mempool_empty (pool);
+
+       g_free (pool);
+#else
        MonoMemPool *p, *n;
 
        p = pool;
@@ -85,6 +110,7 @@ mono_mempool_destroy (MonoMemPool *pool)
                g_free (p);
                p = n;
        }
+#endif
 }
 
 /**
@@ -96,6 +122,9 @@ mono_mempool_destroy (MonoMemPool *pool)
 void
 mono_mempool_invalidate (MonoMemPool *pool)
 {
+#ifdef MALLOC_ALLOCATION
+       g_assert_not_reached ();
+#else
        MonoMemPool *p, *n;
 
        p = pool;
@@ -104,13 +133,28 @@ mono_mempool_invalidate (MonoMemPool *pool)
                memset (p, 42, p->size);
                p = n;
        }
+#endif
 }
 
 void
 mono_mempool_empty (MonoMemPool *pool)
 {
+#ifdef MALLOC_ALLOCATION
+       Chunk *p, *n;
+
+       p = pool->chunks;
+       pool->chunks = NULL;
+       while (p) {
+               n = p->next;
+               g_free (p);
+               p = n;
+       }
+
+       pool->allocated = 0;
+#else
        pool->pos = (guint8*)pool + sizeof (MonoMemPool);
        pool->end = pool->pos + pool->size - sizeof (MonoMemPool);
+#endif
 }
 
 /**
@@ -122,6 +166,9 @@ mono_mempool_empty (MonoMemPool *pool)
 void
 mono_mempool_stats (MonoMemPool *pool)
 {
+#ifdef MALLOC_ALLOCATION
+       g_assert_not_reached ();
+#else
        MonoMemPool *p;
        int count = 0;
        guint32 still_free = 0;
@@ -138,8 +185,10 @@ mono_mempool_stats (MonoMemPool *pool)
                g_print ("Num chunks: %d\n", count);
                g_print ("Free memory: %d\n", still_free);
        }
+#endif
 }
 
+#ifndef MALLOC_ALLOCATION
 #ifdef TRACE_ALLOCATIONS
 #include <execinfo.h>
 #include "metadata/appdomain.h"
@@ -177,6 +226,7 @@ get_next_size (MonoMemPool *pool, int size)
        g_assert (size <= MONO_MEMPOOL_PAGESIZE);
        return target;
 }
+#endif
 
 /**
  * mono_mempool_alloc:
@@ -194,6 +244,19 @@ mono_mempool_alloc (MonoMemPool *pool, guint size)
        
        size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
 
+#ifdef MALLOC_ALLOCATION
+       {
+               Chunk *c = g_malloc (sizeof (Chunk) + size);
+
+               c->next = pool->chunks;
+               pool->chunks = c;
+               c->size = size;
+
+               pool->allocated += size;
+
+               rval = ((guint8*)c) + sizeof (Chunk);
+       }
+#else
        rval = pool->pos;
        pool->pos = (guint8*)rval + size;
 
@@ -232,6 +295,7 @@ mono_mempool_alloc (MonoMemPool *pool, guint size)
        }
 
        return rval;
+#endif
 }
 
 /**
@@ -243,7 +307,10 @@ gpointer
 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
 {
        gpointer rval;
-       
+
+#ifdef MALLOC_ALLOCATION
+       rval = mono_mempool_alloc (pool, size);
+#else
        size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
 
        rval = pool->pos;
@@ -252,6 +319,7 @@ mono_mempool_alloc0 (MonoMemPool *pool, guint size)
        if (G_UNLIKELY (pool->pos >= pool->end)) {
                rval = mono_mempool_alloc (pool, size);
        }
+#endif
 
        memset (rval, 0, size);
        return rval;
@@ -266,6 +334,19 @@ gboolean
 mono_mempool_contains_addr (MonoMemPool *pool,
                                                        gpointer addr)
 {
+#ifdef MALLOC_ALLOCATION
+       Chunk *c;
+
+       c = pool->chunks;
+       while (c) {
+               guint8 *p = ((guint8*)c) + sizeof (Chunk);
+
+               if (addr >= (gpointer)p && addr < (gpointer)(p + c->size))
+                       return TRUE;
+
+               c = c->next;
+       }
+#else
        MonoMemPool *p;
 
        p = pool;
@@ -274,6 +355,7 @@ mono_mempool_contains_addr (MonoMemPool *pool,
                        return TRUE;
                p = p->next;
        }
+#endif
 
        return FALSE;
 }
@@ -309,5 +391,9 @@ mono_mempool_strdup (MonoMemPool *pool,
 guint32
 mono_mempool_get_allocated (MonoMemPool *pool)
 {
+#ifdef MALLOC_ALLOCATION
+       return pool->allocated;
+#else
        return pool->d.allocated;
+#endif
 }