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)
19 #include "mempool-internals.h"
21 #if USE_MALLOC_FOR_MEMPOOLS
22 #define MALLOC_ALLOCATION
26 * MonoMemPool is for fast allocation of memory. We free
27 * all memory when the pool is destroyed.
32 #define MONO_MEMPOOL_PAGESIZE 8192
33 #define MONO_MEMPOOL_MINSIZE 512
36 #define G_LIKELY(a) (a)
37 #define G_UNLIKELY(a) (a)
40 #ifdef MALLOC_ALLOCATION
41 typedef struct _Chunk {
57 double pad; /* to assure proper alignment */
63 static long total_bytes_allocated = 0;
68 * Returns: a new memory pool.
71 mono_mempool_new (void)
73 return mono_mempool_new_size (MONO_MEMPOOL_PAGESIZE);
77 mono_mempool_new_size (int initial_size)
79 #ifdef MALLOC_ALLOCATION
80 return g_new0 (MonoMemPool, 1);
83 if (initial_size < MONO_MEMPOOL_MINSIZE)
84 initial_size = MONO_MEMPOOL_MINSIZE;
85 pool = g_malloc (initial_size);
88 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
89 pool->end = pool->pos + initial_size - sizeof (MonoMemPool);
90 pool->d.allocated = pool->size = initial_size;
91 total_bytes_allocated += initial_size;
97 * mono_mempool_destroy:
98 * @pool: the memory pool to destroy
100 * Free all memory associated with this pool.
103 mono_mempool_destroy (MonoMemPool *pool)
105 #ifdef MALLOC_ALLOCATION
106 mono_mempool_empty (pool);
112 total_bytes_allocated -= pool->d.allocated;
124 * mono_mempool_invalidate:
125 * @pool: the memory pool to invalidate
127 * Fill the memory associated with this pool to 0x2a (42). Useful for debugging.
130 mono_mempool_invalidate (MonoMemPool *pool)
132 #ifdef MALLOC_ALLOCATION
133 g_assert_not_reached ();
140 memset (p, 42, p->size);
147 mono_mempool_empty (MonoMemPool *pool)
149 #ifdef MALLOC_ALLOCATION
162 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
163 pool->end = pool->pos + pool->size - sizeof (MonoMemPool);
168 * mono_mempool_stats:
169 * @pool: the momory pool we need stats for
171 * Print a few stats about the mempool
174 mono_mempool_stats (MonoMemPool *pool)
176 #ifdef MALLOC_ALLOCATION
177 g_assert_not_reached ();
181 guint32 still_free = 0;
185 still_free += p->end - p->pos;
190 g_print ("Mempool %p stats:\n", pool);
191 g_print ("Total mem allocated: %d\n", pool->d.allocated);
192 g_print ("Num chunks: %d\n", count);
193 g_print ("Free memory: %d\n", still_free);
198 #ifndef MALLOC_ALLOCATION
199 #ifdef TRACE_ALLOCATIONS
200 #include <execinfo.h>
201 #include "metadata/appdomain.h"
202 #include "metadata/metadata-internals.h"
204 static CRITICAL_SECTION mempool_tracing_lock;
205 #define BACKTRACE_DEPTH 7
207 mono_backtrace (int size)
209 void *array[BACKTRACE_DEPTH];
212 static gboolean inited;
215 InitializeCriticalSection (&mempool_tracing_lock);
219 EnterCriticalSection (&mempool_tracing_lock);
220 g_print ("Allocating %d bytes\n", size);
221 symbols = backtrace (array, BACKTRACE_DEPTH);
222 names = backtrace_symbols (array, symbols);
223 for (i = 1; i < symbols; ++i) {
224 g_print ("\t%s\n", names [i]);
227 LeaveCriticalSection (&mempool_tracing_lock);
233 get_next_size (MonoMemPool *pool, int size)
235 int target = pool->next? pool->next->size: pool->size;
236 size += sizeof (MonoMemPool);
237 /* increase the size */
238 target += target / 2;
239 while (target < size) {
240 target += target / 2;
242 if (target > MONO_MEMPOOL_PAGESIZE)
243 target = MONO_MEMPOOL_PAGESIZE;
244 /* we are called with size smaller than 4096 */
245 g_assert (size <= MONO_MEMPOOL_PAGESIZE);
251 * mono_mempool_alloc:
252 * @pool: the momory pool to use
253 * @size: size of the momory block
255 * Allocates a new block of memory in @pool.
257 * Returns: the address of a newly allocated memory block.
260 mono_mempool_alloc (MonoMemPool *pool, guint size)
264 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
266 #ifdef MALLOC_ALLOCATION
268 Chunk *c = g_malloc (sizeof (Chunk) + size);
270 c->next = pool->chunks;
274 pool->allocated += size;
276 rval = ((guint8*)c) + sizeof (Chunk);
280 pool->pos = (guint8*)rval + size;
282 #ifdef TRACE_ALLOCATIONS
283 if (pool == mono_get_corlib ()->mempool) {
284 mono_backtrace (size);
287 if (G_UNLIKELY (pool->pos >= pool->end)) {
290 MonoMemPool *np = g_malloc (sizeof (MonoMemPool) + size);
291 np->next = pool->next;
293 np->pos = (guint8*)np + sizeof (MonoMemPool);
294 np->size = sizeof (MonoMemPool) + size;
295 np->end = np->pos + np->size - sizeof (MonoMemPool);
296 pool->d.allocated += sizeof (MonoMemPool) + size;
297 total_bytes_allocated += sizeof (MonoMemPool) + size;
298 return (guint8*)np + sizeof (MonoMemPool);
300 int new_size = get_next_size (pool, size);
301 MonoMemPool *np = g_malloc (new_size);
302 np->next = pool->next;
304 pool->pos = (guint8*)np + sizeof (MonoMemPool);
305 np->pos = (guint8*)np + sizeof (MonoMemPool);
308 pool->end = pool->pos + new_size - sizeof (MonoMemPool);
309 pool->d.allocated += new_size;
310 total_bytes_allocated += new_size;
322 * mono_mempool_alloc0:
324 * same as mono_mempool_alloc, but fills memory with zero.
327 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
331 #ifdef MALLOC_ALLOCATION
332 rval = mono_mempool_alloc (pool, size);
334 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
337 pool->pos = (guint8*)rval + size;
339 if (G_UNLIKELY (pool->pos >= pool->end)) {
340 rval = mono_mempool_alloc (pool, size);
342 #ifdef TRACE_ALLOCATIONS
343 else if (pool == mono_get_corlib ()->mempool) {
344 mono_backtrace (size);
349 memset (rval, 0, size);
354 * mono_mempool_contains_addr:
356 * Determines whenever ADDR is inside the memory used by the mempool.
359 mono_mempool_contains_addr (MonoMemPool *pool,
362 #ifdef MALLOC_ALLOCATION
367 guint8 *p = ((guint8*)c) + sizeof (Chunk);
369 if (addr >= (gpointer)p && addr < (gpointer)(p + c->size))
379 if (addr > (gpointer)p && addr <= (gpointer)((guint8*)p + p->size))
389 * mono_mempool_strdup:
391 * Same as strdup, but allocates memory from the mempool.
392 * Returns: a pointer to the newly allocated string data inside the mempool.
395 mono_mempool_strdup (MonoMemPool *pool,
405 res = mono_mempool_alloc (pool, l + 1);
406 memcpy (res, s, l + 1);
412 * mono_mempool_get_allocated:
414 * Return the amount of memory allocated for this mempool.
417 mono_mempool_get_allocated (MonoMemPool *pool)
419 #ifdef MALLOC_ALLOCATION
420 return pool->allocated;
422 return pool->d.allocated;
427 g_list_prepend_mempool (MonoMemPool *mp, GList *list, gpointer data)
431 new_list = mono_mempool_alloc (mp, sizeof (GList));
432 new_list->data = data;
433 new_list->prev = list ? list->prev : NULL;
434 new_list->next = list;
437 new_list->prev->next = new_list;
439 list->prev = new_list;
445 g_slist_prepend_mempool (MonoMemPool *mp, GSList *list, gpointer data)
449 new_list = mono_mempool_alloc (mp, sizeof (GSList));
450 new_list->data = data;
451 new_list->next = list;
457 g_slist_append_mempool (MonoMemPool *mp, GSList *list, gpointer data)
462 new_list = mono_mempool_alloc (mp, sizeof (GSList));
463 new_list->data = data;
464 new_list->next = NULL;
470 last->next = new_list;
478 * mono_mempool_get_bytes_allocated:
480 * Return the number of bytes currently allocated for mempools.
483 mono_mempool_get_bytes_allocated (void)
485 return total_bytes_allocated;