Added MonoString<->UTF-32 conversion helper functions.
[mono.git] / mono / metadata / mempool.c
index 69f7c6a27cb0c319a1b195b301ad9025874573f7..221a2a904b14c4afb66a8918ac4acc6d62691ce0 100644 (file)
@@ -7,7 +7,9 @@
  * 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)
+ * Copyright 2011 Xamarin Inc. (http://www.xamarin.com)
  */
 
 #include <config.h>
 #include <string.h>
 
 #include "mempool.h"
+#include "mempool-internals.h"
+
+#if USE_MALLOC_FOR_MEMPOOLS
+#define MALLOC_ALLOCATION
+#endif
 
 /*
  * MonoMemPool is for fast allocation of memory. We free
 
 #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)
 #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;
@@ -40,6 +64,9 @@ struct _MonoMemPool {
                guint32 allocated;
        } d;
 };
+#endif
+
+static long total_bytes_allocated = 0;
 
 /**
  * mono_mempool_new:
@@ -47,15 +74,29 @@ struct _MonoMemPool {
  * Returns: a new memory pool.
  */
 MonoMemPool *
-mono_mempool_new ()
+mono_mempool_new (void)
+{
+       return mono_mempool_new_size (MONO_MEMPOOL_PAGESIZE);
+}
+
+MonoMemPool *
+mono_mempool_new_size (int initial_size)
 {
-       MonoMemPool *pool = g_malloc (MONO_MEMPOOL_PAGESIZE);
+#ifdef MALLOC_ALLOCATION
+       return g_new0 (MonoMemPool, 1);
+#else
+       MonoMemPool *pool;
+       if (initial_size < MONO_MEMPOOL_MINSIZE)
+               initial_size = MONO_MEMPOOL_MINSIZE;
+       pool = g_malloc (initial_size);
 
        pool->next = NULL;
        pool->pos = (guint8*)pool + sizeof (MonoMemPool);
-       pool->end = pool->pos + MONO_MEMPOOL_PAGESIZE - sizeof (MonoMemPool);
-       pool->d.allocated = pool->size = MONO_MEMPOOL_PAGESIZE;
+       pool->end = pool->pos + initial_size - sizeof (MonoMemPool);
+       pool->d.allocated = pool->size = initial_size;
+       total_bytes_allocated += initial_size;
        return pool;
+#endif
 }
 
 /**
@@ -67,14 +108,22 @@ mono_mempool_new ()
 void
 mono_mempool_destroy (MonoMemPool *pool)
 {
+#ifdef MALLOC_ALLOCATION
+       mono_mempool_empty (pool);
+
+       g_free (pool);
+#else
        MonoMemPool *p, *n;
 
+       total_bytes_allocated -= pool->d.allocated;
+
        p = pool;
        while (p) {
                n = p->next;
                g_free (p);
                p = n;
        }
+#endif
 }
 
 /**
@@ -86,6 +135,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;
@@ -94,13 +146,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 + MONO_MEMPOOL_PAGESIZE - sizeof (MonoMemPool);
+       pool->end = pool->pos + pool->size - sizeof (MonoMemPool);
+#endif
 }
 
 /**
@@ -112,6 +179,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;
@@ -128,11 +198,62 @@ 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"
+#include "metadata/metadata-internals.h"
+
+static mono_mutex_t mempool_tracing_lock;
+#define BACKTRACE_DEPTH 7
+static void
+mono_backtrace (int size)
+{
+        void *array[BACKTRACE_DEPTH];
+        char **names;
+        int i, symbols;
+        static gboolean inited;
+
+        if (!inited) {
+            mono_mutex_init_recursive (&mempool_tracing_lock);
+            inited = TRUE;
+        }
+
+        mono_mutex_lock (&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]);
+        }
+        free (names);
+        mono_mutex_unlock (&mempool_tracing_lock);
 }
 
+#endif
+
+static int
+get_next_size (MonoMemPool *pool, int size)
+{
+       int target = pool->next? pool->next->size: pool->size;
+       size += sizeof (MonoMemPool);
+       /* increase the size */
+       target += target / 2;
+       while (target < size) {
+               target += target / 2;
+       }
+       if (target > MONO_MEMPOOL_PAGESIZE && size <= MONO_MEMPOOL_PAGESIZE)
+               target = MONO_MEMPOOL_PAGESIZE;
+       return target;
+}
+#endif
+
 /**
  * mono_mempool_alloc:
- * @pool: the momory pool to destroy
+ * @pool: the momory pool to use
  * @size: size of the momory block
  *
  * Allocates a new block of memory in @pool.
@@ -146,9 +267,27 @@ mono_mempool_alloc (MonoMemPool *pool, guint size)
        
        size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
 
+#ifdef MALLOC_ALLOCATION
+       {
+               Chunk *c = g_malloc (size);
+
+               c->next = pool->chunks;
+               pool->chunks = c;
+               c->size = size - sizeof(Chunk);
+
+               pool->allocated += size;
+
+               rval = ((guint8*)c) + sizeof (Chunk);
+       }
+#else
        rval = pool->pos;
        pool->pos = (guint8*)rval + size;
 
+#ifdef TRACE_ALLOCATIONS
+       if (pool == mono_get_corlib ()->mempool) {
+               mono_backtrace (size);
+       }
+#endif
        if (G_UNLIKELY (pool->pos >= pool->end)) {
                pool->pos -= size;
                if (size >= 4096) {
@@ -159,22 +298,26 @@ 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 {
-                       MonoMemPool *np = g_malloc (MONO_MEMPOOL_PAGESIZE);
+                       int new_size = get_next_size (pool, size);
+                       MonoMemPool *np = g_malloc (new_size);
                        np->next = pool->next;
                        pool->next = np;
                        pool->pos = (guint8*)np + sizeof (MonoMemPool);
                        np->pos = (guint8*)np + sizeof (MonoMemPool);
-                       np->size = MONO_MEMPOOL_PAGESIZE;
+                       np->size = new_size;
                        np->end = np->pos;
-                       pool->end = pool->pos + MONO_MEMPOOL_PAGESIZE - sizeof (MonoMemPool);
-                       pool->d.allocated += MONO_MEMPOOL_PAGESIZE;
+                       pool->end = pool->pos + new_size - sizeof (MonoMemPool);
+                       pool->d.allocated += new_size;
+                       total_bytes_allocated += new_size;
 
                        rval = pool->pos;
                        pool->pos += size;
                }
        }
+#endif
 
        return rval;
 }
@@ -188,7 +331,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;
@@ -197,6 +343,12 @@ 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);
        return rval;
@@ -211,6 +363,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;
@@ -219,6 +384,7 @@ mono_mempool_contains_addr (MonoMemPool *pool,
                        return TRUE;
                p = p->next;
        }
+#endif
 
        return FALSE;
 }
@@ -254,5 +420,20 @@ mono_mempool_strdup (MonoMemPool *pool,
 guint32
 mono_mempool_get_allocated (MonoMemPool *pool)
 {
+#ifdef MALLOC_ALLOCATION
+       return pool->allocated;
+#else
        return pool->d.allocated;
+#endif
+}
+
+/**
+ * mono_mempool_get_bytes_allocated:
+ *
+ * Return the number of bytes currently allocated for mempools.
+ */
+long
+mono_mempool_get_bytes_allocated (void)
+{
+       return total_bytes_allocated;
 }