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 * (C) 2001 Ximian, Inc.
18 #include "mempool-internals.h"
20 #if USE_MALLOC_FOR_MEMPOOLS
21 #define MALLOC_ALLOCATION
25 * MonoMemPool is for fast allocation of memory. We free
26 * all memory when the pool is destroyed.
31 #define MONO_MEMPOOL_PAGESIZE 8192
32 #define MONO_MEMPOOL_MINSIZE 512
35 #define G_LIKELY(a) (a)
36 #define G_UNLIKELY(a) (a)
39 #ifdef MALLOC_ALLOCATION
40 typedef struct _Chunk {
56 double pad; /* to assure proper alignment */
65 * Returns: a new memory pool.
68 mono_mempool_new (void)
70 return mono_mempool_new_size (MONO_MEMPOOL_PAGESIZE);
74 mono_mempool_new_size (int initial_size)
76 #ifdef MALLOC_ALLOCATION
77 return g_new0 (MonoMemPool, 1);
80 if (initial_size < MONO_MEMPOOL_MINSIZE)
81 initial_size = MONO_MEMPOOL_MINSIZE;
82 pool = g_malloc (initial_size);
85 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
86 pool->end = pool->pos + initial_size - sizeof (MonoMemPool);
87 pool->d.allocated = pool->size = initial_size;
93 * mono_mempool_destroy:
94 * @pool: the memory pool to destroy
96 * Free all memory associated with this pool.
99 mono_mempool_destroy (MonoMemPool *pool)
101 #ifdef MALLOC_ALLOCATION
102 mono_mempool_empty (pool);
118 * mono_mempool_invalidate:
119 * @pool: the memory pool to invalidate
121 * Fill the memory associated with this pool to 0x2a (42). Useful for debugging.
124 mono_mempool_invalidate (MonoMemPool *pool)
126 #ifdef MALLOC_ALLOCATION
127 g_assert_not_reached ();
134 memset (p, 42, p->size);
141 mono_mempool_empty (MonoMemPool *pool)
143 #ifdef MALLOC_ALLOCATION
156 pool->pos = (guint8*)pool + sizeof (MonoMemPool);
157 pool->end = pool->pos + pool->size - sizeof (MonoMemPool);
162 * mono_mempool_stats:
163 * @pool: the momory pool we need stats for
165 * Print a few stats about the mempool
168 mono_mempool_stats (MonoMemPool *pool)
170 #ifdef MALLOC_ALLOCATION
171 g_assert_not_reached ();
175 guint32 still_free = 0;
179 still_free += p->end - p->pos;
184 g_print ("Mempool %p stats:\n", pool);
185 g_print ("Total mem allocated: %d\n", pool->d.allocated);
186 g_print ("Num chunks: %d\n", count);
187 g_print ("Free memory: %d\n", still_free);
192 #ifndef MALLOC_ALLOCATION
193 #ifdef TRACE_ALLOCATIONS
194 #include <execinfo.h>
195 #include "metadata/appdomain.h"
196 #include "metadata/metadata-internals.h"
198 static CRITICAL_SECTION mempool_tracing_lock;
199 #define BACKTRACE_DEPTH 7
201 mono_backtrace (int size)
203 void *array[BACKTRACE_DEPTH];
206 static gboolean inited;
209 InitializeCriticalSection (&mempool_tracing_lock);
213 EnterCriticalSection (&mempool_tracing_lock);
214 g_print ("Allocating %d bytes\n", size);
215 symbols = backtrace (array, BACKTRACE_DEPTH);
216 names = backtrace_symbols (array, symbols);
217 for (i = 1; i < symbols; ++i) {
218 g_print ("\t%s\n", names [i]);
221 LeaveCriticalSection (&mempool_tracing_lock);
227 get_next_size (MonoMemPool *pool, int size)
229 int target = pool->next? pool->next->size: pool->size;
230 size += sizeof (MonoMemPool);
231 /* increase the size */
232 target += target / 2;
233 while (target < size) {
234 target += target / 2;
236 if (target > MONO_MEMPOOL_PAGESIZE)
237 target = MONO_MEMPOOL_PAGESIZE;
238 /* we are called with size smaller than 4096 */
239 g_assert (size <= MONO_MEMPOOL_PAGESIZE);
245 * mono_mempool_alloc:
246 * @pool: the momory pool to use
247 * @size: size of the momory block
249 * Allocates a new block of memory in @pool.
251 * Returns: the address of a newly allocated memory block.
254 mono_mempool_alloc (MonoMemPool *pool, guint size)
258 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
260 #ifdef MALLOC_ALLOCATION
262 Chunk *c = g_malloc (sizeof (Chunk) + size);
264 c->next = pool->chunks;
268 pool->allocated += size;
270 rval = ((guint8*)c) + sizeof (Chunk);
274 pool->pos = (guint8*)rval + size;
276 #ifdef TRACE_ALLOCATIONS
277 if (pool == mono_get_corlib ()->mempool) {
278 mono_backtrace (size);
281 if (G_UNLIKELY (pool->pos >= pool->end)) {
284 MonoMemPool *np = g_malloc (sizeof (MonoMemPool) + size);
285 np->next = pool->next;
287 np->pos = (guint8*)np + sizeof (MonoMemPool);
288 np->size = sizeof (MonoMemPool) + size;
289 np->end = np->pos + np->size - sizeof (MonoMemPool);
290 pool->d.allocated += sizeof (MonoMemPool) + size;
291 return (guint8*)np + sizeof (MonoMemPool);
293 int new_size = get_next_size (pool, size);
294 MonoMemPool *np = g_malloc (new_size);
295 np->next = pool->next;
297 pool->pos = (guint8*)np + sizeof (MonoMemPool);
298 np->pos = (guint8*)np + sizeof (MonoMemPool);
301 pool->end = pool->pos + new_size - sizeof (MonoMemPool);
302 pool->d.allocated += new_size;
314 * mono_mempool_alloc0:
316 * same as mono_mempool_alloc, but fills memory with zero.
319 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
323 #ifdef MALLOC_ALLOCATION
324 rval = mono_mempool_alloc (pool, size);
326 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
329 pool->pos = (guint8*)rval + size;
331 if (G_UNLIKELY (pool->pos >= pool->end)) {
332 rval = mono_mempool_alloc (pool, size);
334 #ifdef TRACE_ALLOCATIONS
335 else if (pool == mono_get_corlib ()->mempool) {
336 mono_backtrace (size);
341 memset (rval, 0, size);
346 * mono_mempool_contains_addr:
348 * Determines whenever ADDR is inside the memory used by the mempool.
351 mono_mempool_contains_addr (MonoMemPool *pool,
354 #ifdef MALLOC_ALLOCATION
359 guint8 *p = ((guint8*)c) + sizeof (Chunk);
361 if (addr >= (gpointer)p && addr < (gpointer)(p + c->size))
371 if (addr > (gpointer)p && addr <= (gpointer)((guint8*)p + p->size))
381 * mono_mempool_strdup:
383 * Same as strdup, but allocates memory from the mempool.
384 * Returns: a pointer to the newly allocated string data inside the mempool.
387 mono_mempool_strdup (MonoMemPool *pool,
397 res = mono_mempool_alloc (pool, l + 1);
398 memcpy (res, s, l + 1);
404 * mono_mempool_get_allocated:
406 * Return the amount of memory allocated for this mempool.
409 mono_mempool_get_allocated (MonoMemPool *pool)
411 #ifdef MALLOC_ALLOCATION
412 return pool->allocated;
414 return pool->d.allocated;
419 g_list_prepend_mempool (MonoMemPool *mp, GList *list, gpointer data)
423 new_list = mono_mempool_alloc (mp, sizeof (GList));
424 new_list->data = data;
425 new_list->prev = list ? list->prev : NULL;
426 new_list->next = list;
429 new_list->prev->next = new_list;
431 list->prev = new_list;
437 g_slist_prepend_mempool (MonoMemPool *mp, GSList *list, gpointer data)
441 new_list = mono_mempool_alloc (mp, sizeof (GSList));
442 new_list->data = data;
443 new_list->next = list;
449 g_slist_append_mempool (MonoMemPool *mp, GSList *list, gpointer data)
454 new_list = mono_mempool_alloc (mp, sizeof (GSList));
455 new_list->data = data;
456 new_list->next = NULL;
462 last->next = new_list;