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.
32 #define ALIGN_SIZE(s) (((s) + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1))
33 #define SIZEOF_MEM_POOL (ALIGN_SIZE (sizeof (MonoMemPool)))
36 #define MONO_MEMPOOL_PAGESIZE 4096
37 #define MONO_MEMPOOL_MINSIZE 256
39 #define MONO_MEMPOOL_PAGESIZE 8192
40 #define MONO_MEMPOOL_MINSIZE 512
44 #define G_LIKELY(a) (a)
45 #define G_UNLIKELY(a) (a)
48 #ifdef MALLOC_ALLOCATION
49 typedef struct _Chunk {
65 double pad; /* to assure proper alignment */
71 static long total_bytes_allocated = 0;
76 * Returns: a new memory pool.
79 mono_mempool_new (void)
81 return mono_mempool_new_size (MONO_MEMPOOL_PAGESIZE);
85 mono_mempool_new_size (int initial_size)
87 #ifdef MALLOC_ALLOCATION
88 return g_new0 (MonoMemPool, 1);
91 if (initial_size < MONO_MEMPOOL_MINSIZE)
92 initial_size = MONO_MEMPOOL_MINSIZE;
93 pool = g_malloc (initial_size);
96 pool->pos = (guint8*)pool + SIZEOF_MEM_POOL;
97 pool->end = pool->pos + initial_size - SIZEOF_MEM_POOL;
98 pool->d.allocated = pool->size = initial_size;
99 total_bytes_allocated += initial_size;
105 * mono_mempool_destroy:
106 * @pool: the memory pool to destroy
108 * Free all memory associated with this pool.
111 mono_mempool_destroy (MonoMemPool *pool)
113 #ifdef MALLOC_ALLOCATION
114 mono_mempool_empty (pool);
120 total_bytes_allocated -= pool->d.allocated;
132 * mono_mempool_invalidate:
133 * @pool: the memory pool to invalidate
135 * Fill the memory associated with this pool to 0x2a (42). Useful for debugging.
138 mono_mempool_invalidate (MonoMemPool *pool)
140 #ifdef MALLOC_ALLOCATION
141 g_assert_not_reached ();
148 memset (p, 42, p->size);
155 mono_mempool_empty (MonoMemPool *pool)
157 #ifdef MALLOC_ALLOCATION
170 pool->pos = (guint8*)pool + SIZEOF_MEM_POOL;
171 pool->end = pool->pos + pool->size - SIZEOF_MEM_POOL;
176 * mono_mempool_stats:
177 * @pool: the momory pool we need stats for
179 * Print a few stats about the mempool
182 mono_mempool_stats (MonoMemPool *pool)
184 #ifdef MALLOC_ALLOCATION
185 g_assert_not_reached ();
189 guint32 still_free = 0;
193 still_free += p->end - p->pos;
198 g_print ("Mempool %p stats:\n", pool);
199 g_print ("Total mem allocated: %d\n", pool->d.allocated);
200 g_print ("Num chunks: %d\n", count);
201 g_print ("Free memory: %d\n", still_free);
206 #ifndef MALLOC_ALLOCATION
207 #ifdef TRACE_ALLOCATIONS
208 #include <execinfo.h>
209 #include "metadata/appdomain.h"
210 #include "metadata/metadata-internals.h"
212 static mono_mutex_t mempool_tracing_lock;
213 #define BACKTRACE_DEPTH 7
215 mono_backtrace (int size)
217 void *array[BACKTRACE_DEPTH];
220 static gboolean inited;
223 mono_mutex_init_recursive (&mempool_tracing_lock);
227 mono_mutex_lock (&mempool_tracing_lock);
228 g_print ("Allocating %d bytes\n", size);
229 symbols = backtrace (array, BACKTRACE_DEPTH);
230 names = backtrace_symbols (array, symbols);
231 for (i = 1; i < symbols; ++i) {
232 g_print ("\t%s\n", names [i]);
235 mono_mutex_unlock (&mempool_tracing_lock);
241 get_next_size (MonoMemPool *pool, int size)
243 int target = pool->next? pool->next->size: pool->size;
244 size += SIZEOF_MEM_POOL;
245 /* increase the size */
246 target += target / 2;
247 while (target < size) {
248 target += target / 2;
250 if (target > MONO_MEMPOOL_PAGESIZE && size <= MONO_MEMPOOL_PAGESIZE)
251 target = MONO_MEMPOOL_PAGESIZE;
257 * mono_mempool_alloc:
258 * @pool: the momory pool to use
259 * @size: size of the momory block
261 * Allocates a new block of memory in @pool.
263 * Returns: the address of a newly allocated memory block.
266 mono_mempool_alloc (MonoMemPool *pool, guint size)
270 size = ALIGN_SIZE (size);
272 #ifdef MALLOC_ALLOCATION
274 Chunk *c = g_malloc (size + sizeof (Chunk));
276 c->next = pool->chunks;
278 c->size = size - sizeof(Chunk);
280 pool->allocated += size;
282 rval = ((guint8*)c) + sizeof (Chunk);
286 pool->pos = (guint8*)rval + size;
288 #ifdef TRACE_ALLOCATIONS
289 if (pool == mono_get_corlib ()->mempool) {
290 mono_backtrace (size);
293 if (G_UNLIKELY (pool->pos >= pool->end)) {
296 MonoMemPool *np = g_malloc (SIZEOF_MEM_POOL + size);
297 np->next = pool->next;
299 np->pos = (guint8*)np + SIZEOF_MEM_POOL;
300 np->size = SIZEOF_MEM_POOL + size;
301 np->end = np->pos + np->size - SIZEOF_MEM_POOL;
302 pool->d.allocated += SIZEOF_MEM_POOL + size;
303 total_bytes_allocated += SIZEOF_MEM_POOL + size;
304 return (guint8*)np + SIZEOF_MEM_POOL;
306 int new_size = get_next_size (pool, size);
307 MonoMemPool *np = g_malloc (new_size);
308 np->next = pool->next;
310 pool->pos = (guint8*)np + SIZEOF_MEM_POOL;
311 np->pos = (guint8*)np + SIZEOF_MEM_POOL;
314 pool->end = pool->pos + new_size - SIZEOF_MEM_POOL;
315 pool->d.allocated += new_size;
316 total_bytes_allocated += new_size;
328 * mono_mempool_alloc0:
330 * same as mono_mempool_alloc, but fills memory with zero.
333 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
337 #ifdef MALLOC_ALLOCATION
338 rval = mono_mempool_alloc (pool, size);
340 size = ALIGN_SIZE (size);
343 pool->pos = (guint8*)rval + size;
345 if (G_UNLIKELY (pool->pos >= pool->end)) {
346 rval = mono_mempool_alloc (pool, size);
348 #ifdef TRACE_ALLOCATIONS
349 else if (pool == mono_get_corlib ()->mempool) {
350 mono_backtrace (size);
355 memset (rval, 0, size);
360 * mono_mempool_contains_addr:
362 * Determines whenever ADDR is inside the memory used by the mempool.
365 mono_mempool_contains_addr (MonoMemPool *pool,
368 #ifdef MALLOC_ALLOCATION
373 guint8 *p = ((guint8*)c) + sizeof (Chunk);
375 if (addr >= (gpointer)p && addr < (gpointer)(p + c->size))
385 if (addr > (gpointer)p && addr <= (gpointer)((guint8*)p + p->size))
395 * mono_mempool_strdup:
397 * Same as strdup, but allocates memory from the mempool.
398 * Returns: a pointer to the newly allocated string data inside the mempool.
401 mono_mempool_strdup (MonoMemPool *pool,
411 res = mono_mempool_alloc (pool, l + 1);
412 memcpy (res, s, l + 1);
418 * mono_mempool_get_allocated:
420 * Return the amount of memory allocated for this mempool.
423 mono_mempool_get_allocated (MonoMemPool *pool)
425 #ifdef MALLOC_ALLOCATION
426 return pool->allocated;
428 return pool->d.allocated;
433 * mono_mempool_get_bytes_allocated:
435 * Return the number of bytes currently allocated for mempools.
438 mono_mempool_get_bytes_allocated (void)
440 return total_bytes_allocated;