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.
33 #define MONO_MEMPOOL_PAGESIZE 4096
34 #define MONO_MEMPOOL_MINSIZE 256
36 #define MONO_MEMPOOL_PAGESIZE 8192
37 #define MONO_MEMPOOL_MINSIZE 512
41 #define G_LIKELY(a) (a)
42 #define G_UNLIKELY(a) (a)
45 #ifdef MALLOC_ALLOCATION
46 typedef struct _Chunk {
62 double pad; /* to assure proper alignment */
68 static long total_bytes_allocated = 0;
73 * Returns: a new memory pool.
76 mono_mempool_new (void)
78 return mono_mempool_new_size (MONO_MEMPOOL_PAGESIZE);
82 mono_mempool_new_size (int initial_size)
84 #ifdef MALLOC_ALLOCATION
85 return g_new0 (MonoMemPool, 1);
88 if (initial_size < MONO_MEMPOOL_MINSIZE)
89 initial_size = MONO_MEMPOOL_MINSIZE;
90 pool = g_malloc (initial_size);
93 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
94 pool->end = pool->pos + initial_size - sizeof (MonoMemPool);
95 pool->d.allocated = pool->size = initial_size;
96 total_bytes_allocated += initial_size;
102 * mono_mempool_destroy:
103 * @pool: the memory pool to destroy
105 * Free all memory associated with this pool.
108 mono_mempool_destroy (MonoMemPool *pool)
110 #ifdef MALLOC_ALLOCATION
111 mono_mempool_empty (pool);
117 total_bytes_allocated -= pool->d.allocated;
129 * mono_mempool_invalidate:
130 * @pool: the memory pool to invalidate
132 * Fill the memory associated with this pool to 0x2a (42). Useful for debugging.
135 mono_mempool_invalidate (MonoMemPool *pool)
137 #ifdef MALLOC_ALLOCATION
138 g_assert_not_reached ();
145 memset (p, 42, p->size);
152 mono_mempool_empty (MonoMemPool *pool)
154 #ifdef MALLOC_ALLOCATION
167 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
168 pool->end = pool->pos + pool->size - sizeof (MonoMemPool);
173 * mono_mempool_stats:
174 * @pool: the momory pool we need stats for
176 * Print a few stats about the mempool
179 mono_mempool_stats (MonoMemPool *pool)
181 #ifdef MALLOC_ALLOCATION
182 g_assert_not_reached ();
186 guint32 still_free = 0;
190 still_free += p->end - p->pos;
195 g_print ("Mempool %p stats:\n", pool);
196 g_print ("Total mem allocated: %d\n", pool->d.allocated);
197 g_print ("Num chunks: %d\n", count);
198 g_print ("Free memory: %d\n", still_free);
203 #ifndef MALLOC_ALLOCATION
204 #ifdef TRACE_ALLOCATIONS
205 #include <execinfo.h>
206 #include "metadata/appdomain.h"
207 #include "metadata/metadata-internals.h"
209 static CRITICAL_SECTION mempool_tracing_lock;
210 #define BACKTRACE_DEPTH 7
212 mono_backtrace (int size)
214 void *array[BACKTRACE_DEPTH];
217 static gboolean inited;
220 InitializeCriticalSection (&mempool_tracing_lock);
224 EnterCriticalSection (&mempool_tracing_lock);
225 g_print ("Allocating %d bytes\n", size);
226 symbols = backtrace (array, BACKTRACE_DEPTH);
227 names = backtrace_symbols (array, symbols);
228 for (i = 1; i < symbols; ++i) {
229 g_print ("\t%s\n", names [i]);
232 LeaveCriticalSection (&mempool_tracing_lock);
238 get_next_size (MonoMemPool *pool, int size)
240 int target = pool->next? pool->next->size: pool->size;
241 size += sizeof (MonoMemPool);
242 /* increase the size */
243 target += target / 2;
244 while (target < size) {
245 target += target / 2;
247 if (target > MONO_MEMPOOL_PAGESIZE && size <= MONO_MEMPOOL_PAGESIZE)
248 target = MONO_MEMPOOL_PAGESIZE;
254 * mono_mempool_alloc:
255 * @pool: the momory pool to use
256 * @size: size of the momory block
258 * Allocates a new block of memory in @pool.
260 * Returns: the address of a newly allocated memory block.
263 mono_mempool_alloc (MonoMemPool *pool, guint size)
267 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
269 #ifdef MALLOC_ALLOCATION
271 Chunk *c = g_malloc (size);
273 c->next = pool->chunks;
275 c->size = size - sizeof(Chunk);
277 pool->allocated += size;
279 rval = ((guint8*)c) + sizeof (Chunk);
283 pool->pos = (guint8*)rval + size;
285 #ifdef TRACE_ALLOCATIONS
286 if (pool == mono_get_corlib ()->mempool) {
287 mono_backtrace (size);
290 if (G_UNLIKELY (pool->pos >= pool->end)) {
293 MonoMemPool *np = g_malloc (sizeof (MonoMemPool) + size);
294 np->next = pool->next;
296 np->pos = (guint8*)np + sizeof (MonoMemPool);
297 np->size = sizeof (MonoMemPool) + size;
298 np->end = np->pos + np->size - sizeof (MonoMemPool);
299 pool->d.allocated += sizeof (MonoMemPool) + size;
300 total_bytes_allocated += sizeof (MonoMemPool) + size;
301 return (guint8*)np + sizeof (MonoMemPool);
303 int new_size = get_next_size (pool, size);
304 MonoMemPool *np = g_malloc (new_size);
305 np->next = pool->next;
307 pool->pos = (guint8*)np + sizeof (MonoMemPool);
308 np->pos = (guint8*)np + sizeof (MonoMemPool);
311 pool->end = pool->pos + new_size - sizeof (MonoMemPool);
312 pool->d.allocated += new_size;
313 total_bytes_allocated += new_size;
325 * mono_mempool_alloc0:
327 * same as mono_mempool_alloc, but fills memory with zero.
330 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
334 #ifdef MALLOC_ALLOCATION
335 rval = mono_mempool_alloc (pool, size);
337 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
340 pool->pos = (guint8*)rval + size;
342 if (G_UNLIKELY (pool->pos >= pool->end)) {
343 rval = mono_mempool_alloc (pool, size);
345 #ifdef TRACE_ALLOCATIONS
346 else if (pool == mono_get_corlib ()->mempool) {
347 mono_backtrace (size);
352 memset (rval, 0, size);
357 * mono_mempool_contains_addr:
359 * Determines whenever ADDR is inside the memory used by the mempool.
362 mono_mempool_contains_addr (MonoMemPool *pool,
365 #ifdef MALLOC_ALLOCATION
370 guint8 *p = ((guint8*)c) + sizeof (Chunk);
372 if (addr >= (gpointer)p && addr < (gpointer)(p + c->size))
382 if (addr > (gpointer)p && addr <= (gpointer)((guint8*)p + p->size))
392 * mono_mempool_strdup:
394 * Same as strdup, but allocates memory from the mempool.
395 * Returns: a pointer to the newly allocated string data inside the mempool.
398 mono_mempool_strdup (MonoMemPool *pool,
408 res = mono_mempool_alloc (pool, l + 1);
409 memcpy (res, s, l + 1);
415 * mono_mempool_get_allocated:
417 * Return the amount of memory allocated for this mempool.
420 mono_mempool_get_allocated (MonoMemPool *pool)
422 #ifdef MALLOC_ALLOCATION
423 return pool->allocated;
425 return pool->d.allocated;
430 * mono_mempool_get_bytes_allocated:
432 * Return the number of bytes currently allocated for mempools.
435 mono_mempool_get_bytes_allocated (void)
437 return total_bytes_allocated;