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 */
66 * Returns: a new memory pool.
69 mono_mempool_new (void)
71 return mono_mempool_new_size (MONO_MEMPOOL_PAGESIZE);
75 mono_mempool_new_size (int initial_size)
77 #ifdef MALLOC_ALLOCATION
78 return g_new0 (MonoMemPool, 1);
81 if (initial_size < MONO_MEMPOOL_MINSIZE)
82 initial_size = MONO_MEMPOOL_MINSIZE;
83 pool = g_malloc (initial_size);
86 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
87 pool->end = pool->pos + initial_size - sizeof (MonoMemPool);
88 pool->d.allocated = pool->size = initial_size;
94 * mono_mempool_destroy:
95 * @pool: the memory pool to destroy
97 * Free all memory associated with this pool.
100 mono_mempool_destroy (MonoMemPool *pool)
102 #ifdef MALLOC_ALLOCATION
103 mono_mempool_empty (pool);
119 * mono_mempool_invalidate:
120 * @pool: the memory pool to invalidate
122 * Fill the memory associated with this pool to 0x2a (42). Useful for debugging.
125 mono_mempool_invalidate (MonoMemPool *pool)
127 #ifdef MALLOC_ALLOCATION
128 g_assert_not_reached ();
135 memset (p, 42, p->size);
142 mono_mempool_empty (MonoMemPool *pool)
144 #ifdef MALLOC_ALLOCATION
157 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
158 pool->end = pool->pos + pool->size - sizeof (MonoMemPool);
163 * mono_mempool_stats:
164 * @pool: the momory pool we need stats for
166 * Print a few stats about the mempool
169 mono_mempool_stats (MonoMemPool *pool)
171 #ifdef MALLOC_ALLOCATION
172 g_assert_not_reached ();
176 guint32 still_free = 0;
180 still_free += p->end - p->pos;
185 g_print ("Mempool %p stats:\n", pool);
186 g_print ("Total mem allocated: %d\n", pool->d.allocated);
187 g_print ("Num chunks: %d\n", count);
188 g_print ("Free memory: %d\n", still_free);
193 #ifndef MALLOC_ALLOCATION
194 #ifdef TRACE_ALLOCATIONS
195 #include <execinfo.h>
196 #include "metadata/appdomain.h"
197 #include "metadata/metadata-internals.h"
199 static CRITICAL_SECTION mempool_tracing_lock;
200 #define BACKTRACE_DEPTH 7
202 mono_backtrace (int size)
204 void *array[BACKTRACE_DEPTH];
207 static gboolean inited;
210 InitializeCriticalSection (&mempool_tracing_lock);
214 EnterCriticalSection (&mempool_tracing_lock);
215 g_print ("Allocating %d bytes\n", size);
216 symbols = backtrace (array, BACKTRACE_DEPTH);
217 names = backtrace_symbols (array, symbols);
218 for (i = 1; i < symbols; ++i) {
219 g_print ("\t%s\n", names [i]);
222 LeaveCriticalSection (&mempool_tracing_lock);
228 get_next_size (MonoMemPool *pool, int size)
230 int target = pool->next? pool->next->size: pool->size;
231 size += sizeof (MonoMemPool);
232 /* increase the size */
233 target += target / 2;
234 while (target < size) {
235 target += target / 2;
237 if (target > MONO_MEMPOOL_PAGESIZE)
238 target = MONO_MEMPOOL_PAGESIZE;
239 /* we are called with size smaller than 4096 */
240 g_assert (size <= MONO_MEMPOOL_PAGESIZE);
246 * mono_mempool_alloc:
247 * @pool: the momory pool to use
248 * @size: size of the momory block
250 * Allocates a new block of memory in @pool.
252 * Returns: the address of a newly allocated memory block.
255 mono_mempool_alloc (MonoMemPool *pool, guint size)
259 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
261 #ifdef MALLOC_ALLOCATION
263 Chunk *c = g_malloc (sizeof (Chunk) + size);
265 c->next = pool->chunks;
269 pool->allocated += size;
271 rval = ((guint8*)c) + sizeof (Chunk);
275 pool->pos = (guint8*)rval + size;
277 #ifdef TRACE_ALLOCATIONS
278 if (pool == mono_get_corlib ()->mempool) {
279 mono_backtrace (size);
282 if (G_UNLIKELY (pool->pos >= pool->end)) {
285 MonoMemPool *np = g_malloc (sizeof (MonoMemPool) + size);
286 np->next = pool->next;
288 np->pos = (guint8*)np + sizeof (MonoMemPool);
289 np->size = sizeof (MonoMemPool) + size;
290 np->end = np->pos + np->size - sizeof (MonoMemPool);
291 pool->d.allocated += sizeof (MonoMemPool) + size;
292 return (guint8*)np + sizeof (MonoMemPool);
294 int new_size = get_next_size (pool, size);
295 MonoMemPool *np = g_malloc (new_size);
296 np->next = pool->next;
298 pool->pos = (guint8*)np + sizeof (MonoMemPool);
299 np->pos = (guint8*)np + sizeof (MonoMemPool);
302 pool->end = pool->pos + new_size - sizeof (MonoMemPool);
303 pool->d.allocated += new_size;
315 * mono_mempool_alloc0:
317 * same as mono_mempool_alloc, but fills memory with zero.
320 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
324 #ifdef MALLOC_ALLOCATION
325 rval = mono_mempool_alloc (pool, size);
327 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
330 pool->pos = (guint8*)rval + size;
332 if (G_UNLIKELY (pool->pos >= pool->end)) {
333 rval = mono_mempool_alloc (pool, size);
335 #ifdef TRACE_ALLOCATIONS
336 else if (pool == mono_get_corlib ()->mempool) {
337 mono_backtrace (size);
342 memset (rval, 0, size);
347 * mono_mempool_contains_addr:
349 * Determines whenever ADDR is inside the memory used by the mempool.
352 mono_mempool_contains_addr (MonoMemPool *pool,
355 #ifdef MALLOC_ALLOCATION
360 guint8 *p = ((guint8*)c) + sizeof (Chunk);
362 if (addr >= (gpointer)p && addr < (gpointer)(p + c->size))
372 if (addr > (gpointer)p && addr <= (gpointer)((guint8*)p + p->size))
382 * mono_mempool_strdup:
384 * Same as strdup, but allocates memory from the mempool.
385 * Returns: a pointer to the newly allocated string data inside the mempool.
388 mono_mempool_strdup (MonoMemPool *pool,
398 res = mono_mempool_alloc (pool, l + 1);
399 memcpy (res, s, l + 1);
405 * mono_mempool_get_allocated:
407 * Return the amount of memory allocated for this mempool.
410 mono_mempool_get_allocated (MonoMemPool *pool)
412 #ifdef MALLOC_ALLOCATION
413 return pool->allocated;
415 return pool->d.allocated;
420 g_list_prepend_mempool (MonoMemPool *mp, GList *list, gpointer data)
424 new_list = mono_mempool_alloc (mp, sizeof (GList));
425 new_list->data = data;
426 new_list->prev = list ? list->prev : NULL;
427 new_list->next = list;
430 new_list->prev->next = new_list;
432 list->prev = new_list;
438 g_slist_prepend_mempool (MonoMemPool *mp, GSList *list, gpointer data)
442 new_list = mono_mempool_alloc (mp, sizeof (GSList));
443 new_list->data = data;
444 new_list->next = list;
450 g_slist_append_mempool (MonoMemPool *mp, GSList *list, gpointer data)
455 new_list = mono_mempool_alloc (mp, sizeof (GSList));
456 new_list->data = data;
457 new_list->next = NULL;
463 last->next = new_list;