2010-04-27 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / mempool.c
index 36453fb938e3255b5553baed9afac5b65e9589b6..33d381f59fc56f83d4a25135fc8be64657fffc02 100644 (file)
@@ -7,7 +7,8 @@
  * Author:
  *   Dietmar Maurer (dietmar@ximian.com)
  *
- * (C) 2001 Ximian, Inc.
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include <config.h>
 
 #define MEM_ALIGN 8
 
+#if MONO_SMALL_CONFIG
+#define MONO_MEMPOOL_PAGESIZE 4096
+#define MONO_MEMPOOL_MINSIZE 256
+#else
 #define MONO_MEMPOOL_PAGESIZE 8192
 #define MONO_MEMPOOL_MINSIZE 512
+#endif
 
 #ifndef G_LIKELY
 #define G_LIKELY(a) (a)
@@ -59,6 +65,8 @@ struct _MonoMemPool {
 };
 #endif
 
+static long total_bytes_allocated = 0;
+
 /**
  * mono_mempool_new:
  *
@@ -85,6 +93,7 @@ mono_mempool_new_size (int initial_size)
        pool->pos = (guint8*)pool + sizeof (MonoMemPool);
        pool->end = pool->pos + initial_size - sizeof (MonoMemPool);
        pool->d.allocated = pool->size = initial_size;
+       total_bytes_allocated += initial_size;
        return pool;
 #endif
 }
@@ -105,6 +114,8 @@ mono_mempool_destroy (MonoMemPool *pool)
 #else
        MonoMemPool *p, *n;
 
+       total_bytes_allocated -= pool->d.allocated;
+
        p = pool;
        while (p) {
                n = p->next;
@@ -195,18 +206,30 @@ mono_mempool_stats (MonoMemPool *pool)
 #include "metadata/appdomain.h"
 #include "metadata/metadata-internals.h"
 
+static CRITICAL_SECTION mempool_tracing_lock;
+#define BACKTRACE_DEPTH 7
 static void
-mono_backtrace (int limit)
+mono_backtrace (int size)
 {
-        void *array[limit];
+        void *array[BACKTRACE_DEPTH];
         char **names;
-        int i;
-        backtrace (array, limit);
-        names = backtrace_symbols (array, limit);
-        for (i = 1; i < limit; ++i) {
+        int i, symbols;
+        static gboolean inited;
+
+        if (!inited) {
+            InitializeCriticalSection (&mempool_tracing_lock);
+            inited = TRUE;
+        }
+
+        EnterCriticalSection (&mempool_tracing_lock);
+        g_print ("Allocating %d bytes\n", size);
+        symbols = backtrace (array, BACKTRACE_DEPTH);
+        names = backtrace_symbols (array, symbols);
+        for (i = 1; i < symbols; ++i) {
                 g_print ("\t%s\n", names [i]);
         }
-        g_free (names);
+        free (names);
+        LeaveCriticalSection (&mempool_tracing_lock);
 }
 
 #endif
@@ -221,10 +244,8 @@ get_next_size (MonoMemPool *pool, int size)
        while (target < size) {
                target += target / 2;
        }
-       if (target > MONO_MEMPOOL_PAGESIZE)
+       if (target > MONO_MEMPOOL_PAGESIZE && size <= MONO_MEMPOOL_PAGESIZE)
                target = MONO_MEMPOOL_PAGESIZE;
-       /* we are called with size smaller than 4096 */
-       g_assert (size <= MONO_MEMPOOL_PAGESIZE);
        return target;
 }
 #endif
@@ -263,8 +284,7 @@ mono_mempool_alloc (MonoMemPool *pool, guint size)
 
 #ifdef TRACE_ALLOCATIONS
        if (pool == mono_get_corlib ()->mempool) {
-               g_print ("Allocating %d bytes\n", size);
-               mono_backtrace (7);
+               mono_backtrace (size);
        }
 #endif
        if (G_UNLIKELY (pool->pos >= pool->end)) {
@@ -277,6 +297,7 @@ mono_mempool_alloc (MonoMemPool *pool, guint size)
                        np->size = sizeof (MonoMemPool) + size;
                        np->end = np->pos + np->size - sizeof (MonoMemPool);
                        pool->d.allocated += sizeof (MonoMemPool) + size;
+                       total_bytes_allocated += sizeof (MonoMemPool) + size;
                        return (guint8*)np + sizeof (MonoMemPool);
                } else {
                        int new_size = get_next_size (pool, size);
@@ -289,6 +310,7 @@ mono_mempool_alloc (MonoMemPool *pool, guint size)
                        np->end = np->pos;
                        pool->end = pool->pos + new_size - sizeof (MonoMemPool);
                        pool->d.allocated += new_size;
+                       total_bytes_allocated += new_size;
 
                        rval = pool->pos;
                        pool->pos += size;
@@ -320,6 +342,11 @@ mono_mempool_alloc0 (MonoMemPool *pool, guint size)
        if (G_UNLIKELY (pool->pos >= pool->end)) {
                rval = mono_mempool_alloc (pool, size);
        }
+#ifdef TRACE_ALLOCATIONS
+       else if (pool == mono_get_corlib ()->mempool) {
+               mono_backtrace (size);
+       }
+#endif
 #endif
 
        memset (rval, 0, size);
@@ -399,53 +426,13 @@ mono_mempool_get_allocated (MonoMemPool *pool)
 #endif
 }
 
-GList*
-g_list_prepend_mempool (MonoMemPool *mp, GList *list, gpointer data)
-{
-       GList *new_list;
-       
-       new_list = mono_mempool_alloc (mp, sizeof (GList));
-       new_list->data = data;
-       new_list->prev = list ? list->prev : NULL;
-    new_list->next = list;
-
-    if (new_list->prev)
-            new_list->prev->next = new_list;
-    if (list)
-            list->prev = new_list;
-
-       return new_list;
-}
-
-GSList*
-g_slist_prepend_mempool (MonoMemPool *mp, GSList *list, gpointer  data)
-{
-       GSList *new_list;
-       
-       new_list = mono_mempool_alloc (mp, sizeof (GSList));
-       new_list->data = data;
-       new_list->next = list;
-
-       return new_list;
-}
-
-GSList*
-g_slist_append_mempool (MonoMemPool *mp, GSList *list, gpointer data)
+/**
+ * mono_mempool_get_bytes_allocated:
+ *
+ * Return the number of bytes currently allocated for mempools.
+ */
+long
+mono_mempool_get_bytes_allocated (void)
 {
-       GSList *new_list;
-       GSList *last;
-
-       new_list = mono_mempool_alloc (mp, sizeof (GSList));
-       new_list->data = data;
-       new_list->next = NULL;
-
-       if (list) {
-               last = list;
-               while (last->next)
-                       last = last->next;
-               last->next = new_list;
-
-               return list;
-       } else
-               return new_list;
+       return total_bytes_allocated;
 }