2 * mempool.c: efficient memory allocation
4 * MonoMemPool is for fast allocation of memory. We free
5 * all memory when the pool is destroyed.
8 * Dietmar Maurer (dietmar@ximian.com)
10 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
11 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 * Copyright 2011 Xamarin Inc. (http://www.xamarin.com)
20 #include "mempool-internals.h"
22 #if USE_MALLOC_FOR_MEMPOOLS
23 #define MALLOC_ALLOCATION
27 * MonoMemPool is for fast allocation of memory. We free
28 * all memory when the pool is destroyed.
34 #define MONO_MEMPOOL_PAGESIZE 4096
35 #define MONO_MEMPOOL_MINSIZE 256
37 #define MONO_MEMPOOL_PAGESIZE 8192
38 #define MONO_MEMPOOL_MINSIZE 512
42 #define G_LIKELY(a) (a)
43 #define G_UNLIKELY(a) (a)
46 #ifdef MALLOC_ALLOCATION
47 typedef struct _Chunk {
63 double pad; /* to assure proper alignment */
69 static long total_bytes_allocated = 0;
74 * Returns: a new memory pool.
77 mono_mempool_new (void)
79 return mono_mempool_new_size (MONO_MEMPOOL_PAGESIZE);
83 mono_mempool_new_size (int initial_size)
85 #ifdef MALLOC_ALLOCATION
86 return g_new0 (MonoMemPool, 1);
89 if (initial_size < MONO_MEMPOOL_MINSIZE)
90 initial_size = MONO_MEMPOOL_MINSIZE;
91 pool = g_malloc (initial_size);
94 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
95 pool->end = pool->pos + initial_size - sizeof (MonoMemPool);
96 pool->d.allocated = pool->size = initial_size;
97 total_bytes_allocated += initial_size;
103 * mono_mempool_destroy:
104 * @pool: the memory pool to destroy
106 * Free all memory associated with this pool.
109 mono_mempool_destroy (MonoMemPool *pool)
111 #ifdef MALLOC_ALLOCATION
112 mono_mempool_empty (pool);
118 total_bytes_allocated -= pool->d.allocated;
130 * mono_mempool_invalidate:
131 * @pool: the memory pool to invalidate
133 * Fill the memory associated with this pool to 0x2a (42). Useful for debugging.
136 mono_mempool_invalidate (MonoMemPool *pool)
138 #ifdef MALLOC_ALLOCATION
139 g_assert_not_reached ();
146 memset (p, 42, p->size);
153 mono_mempool_empty (MonoMemPool *pool)
155 #ifdef MALLOC_ALLOCATION
168 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
169 pool->end = pool->pos + pool->size - sizeof (MonoMemPool);
174 * mono_mempool_stats:
175 * @pool: the momory pool we need stats for
177 * Print a few stats about the mempool
180 mono_mempool_stats (MonoMemPool *pool)
182 #ifdef MALLOC_ALLOCATION
183 g_assert_not_reached ();
187 guint32 still_free = 0;
191 still_free += p->end - p->pos;
196 g_print ("Mempool %p stats:\n", pool);
197 g_print ("Total mem allocated: %d\n", pool->d.allocated);
198 g_print ("Num chunks: %d\n", count);
199 g_print ("Free memory: %d\n", still_free);
204 #ifndef MALLOC_ALLOCATION
205 #ifdef TRACE_ALLOCATIONS
206 #include <execinfo.h>
207 #include "metadata/appdomain.h"
208 #include "metadata/metadata-internals.h"
210 static CRITICAL_SECTION mempool_tracing_lock;
211 #define BACKTRACE_DEPTH 7
213 mono_backtrace (int size)
215 void *array[BACKTRACE_DEPTH];
218 static gboolean inited;
221 InitializeCriticalSection (&mempool_tracing_lock);
225 EnterCriticalSection (&mempool_tracing_lock);
226 g_print ("Allocating %d bytes\n", size);
227 symbols = backtrace (array, BACKTRACE_DEPTH);
228 names = backtrace_symbols (array, symbols);
229 for (i = 1; i < symbols; ++i) {
230 g_print ("\t%s\n", names [i]);
233 LeaveCriticalSection (&mempool_tracing_lock);
239 get_next_size (MonoMemPool *pool, int size)
241 int target = pool->next? pool->next->size: pool->size;
242 size += sizeof (MonoMemPool);
243 /* increase the size */
244 target += target / 2;
245 while (target < size) {
246 target += target / 2;
248 if (target > MONO_MEMPOOL_PAGESIZE && size <= MONO_MEMPOOL_PAGESIZE)
249 target = MONO_MEMPOOL_PAGESIZE;
255 * mono_mempool_alloc:
256 * @pool: the momory pool to use
257 * @size: size of the momory block
259 * Allocates a new block of memory in @pool.
261 * Returns: the address of a newly allocated memory block.
264 mono_mempool_alloc (MonoMemPool *pool, guint size)
268 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
270 #ifdef MALLOC_ALLOCATION
272 Chunk *c = g_malloc (size);
274 c->next = pool->chunks;
276 c->size = size - sizeof(Chunk);
278 pool->allocated += size;
280 rval = ((guint8*)c) + sizeof (Chunk);
284 pool->pos = (guint8*)rval + size;
286 #ifdef TRACE_ALLOCATIONS
287 if (pool == mono_get_corlib ()->mempool) {
288 mono_backtrace (size);
291 if (G_UNLIKELY (pool->pos >= pool->end)) {
294 MonoMemPool *np = g_malloc (sizeof (MonoMemPool) + size);
295 np->next = pool->next;
297 np->pos = (guint8*)np + sizeof (MonoMemPool);
298 np->size = sizeof (MonoMemPool) + size;
299 np->end = np->pos + np->size - sizeof (MonoMemPool);
300 pool->d.allocated += sizeof (MonoMemPool) + size;
301 total_bytes_allocated += sizeof (MonoMemPool) + size;
302 return (guint8*)np + sizeof (MonoMemPool);
304 int new_size = get_next_size (pool, size);
305 MonoMemPool *np = g_malloc (new_size);
306 np->next = pool->next;
308 pool->pos = (guint8*)np + sizeof (MonoMemPool);
309 np->pos = (guint8*)np + sizeof (MonoMemPool);
312 pool->end = pool->pos + new_size - sizeof (MonoMemPool);
313 pool->d.allocated += new_size;
314 total_bytes_allocated += new_size;
326 * mono_mempool_alloc0:
328 * same as mono_mempool_alloc, but fills memory with zero.
331 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
335 #ifdef MALLOC_ALLOCATION
336 rval = mono_mempool_alloc (pool, size);
338 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
341 pool->pos = (guint8*)rval + size;
343 if (G_UNLIKELY (pool->pos >= pool->end)) {
344 rval = mono_mempool_alloc (pool, size);
346 #ifdef TRACE_ALLOCATIONS
347 else if (pool == mono_get_corlib ()->mempool) {
348 mono_backtrace (size);
353 memset (rval, 0, size);
358 * mono_mempool_contains_addr:
360 * Determines whenever ADDR is inside the memory used by the mempool.
363 mono_mempool_contains_addr (MonoMemPool *pool,
366 #ifdef MALLOC_ALLOCATION
371 guint8 *p = ((guint8*)c) + sizeof (Chunk);
373 if (addr >= (gpointer)p && addr < (gpointer)(p + c->size))
383 if (addr > (gpointer)p && addr <= (gpointer)((guint8*)p + p->size))
393 * mono_mempool_strdup:
395 * Same as strdup, but allocates memory from the mempool.
396 * Returns: a pointer to the newly allocated string data inside the mempool.
399 mono_mempool_strdup (MonoMemPool *pool,
409 res = mono_mempool_alloc (pool, l + 1);
410 memcpy (res, s, l + 1);
416 * mono_mempool_get_allocated:
418 * Return the amount of memory allocated for this mempool.
421 mono_mempool_get_allocated (MonoMemPool *pool)
423 #ifdef MALLOC_ALLOCATION
424 return pool->allocated;
426 return pool->d.allocated;
431 * mono_mempool_get_bytes_allocated:
433 * Return the number of bytes currently allocated for mempools.
436 mono_mempool_get_bytes_allocated (void)
438 return total_bytes_allocated;