+
+ pool->allocated = 0;
+#else
+ pool->pos = (guint8*)pool + sizeof (MonoMemPool);
+ pool->end = pool->pos + pool->size - sizeof (MonoMemPool);
+#endif
+}
+
+/**
+ * mono_mempool_stats:
+ * @pool: the momory pool we need stats for
+ *
+ * Print a few stats about the mempool
+ */
+void
+mono_mempool_stats (MonoMemPool *pool)
+{
+#ifdef MALLOC_ALLOCATION
+ g_assert_not_reached ();
+#else
+ MonoMemPool *p;
+ int count = 0;
+ guint32 still_free = 0;
+
+ p = pool;
+ while (p) {
+ still_free += p->end - p->pos;
+ p = p->next;
+ count++;
+ }
+ if (pool) {
+ g_print ("Mempool %p stats:\n", pool);
+ g_print ("Total mem allocated: %d\n", pool->d.allocated);
+ 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 CRITICAL_SECTION 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) {
+ 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]);
+ }
+ free (names);
+ LeaveCriticalSection (&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;