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"
199 mono_backtrace (int limit)
204 backtrace (array, limit);
205 names = backtrace_symbols (array, limit);
206 for (i = 1; i < limit; ++i) {
207 g_print ("\t%s\n", names [i]);
215 get_next_size (MonoMemPool *pool, int size)
217 int target = pool->next? pool->next->size: pool->size;
218 size += sizeof (MonoMemPool);
219 /* increase the size */
220 target += target / 2;
221 while (target < size) {
222 target += target / 2;
224 if (target > MONO_MEMPOOL_PAGESIZE)
225 target = MONO_MEMPOOL_PAGESIZE;
226 /* we are called with size smaller than 4096 */
227 g_assert (size <= MONO_MEMPOOL_PAGESIZE);
233 * mono_mempool_alloc:
234 * @pool: the momory pool to use
235 * @size: size of the momory block
237 * Allocates a new block of memory in @pool.
239 * Returns: the address of a newly allocated memory block.
242 mono_mempool_alloc (MonoMemPool *pool, guint size)
246 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
248 #ifdef MALLOC_ALLOCATION
250 Chunk *c = g_malloc (sizeof (Chunk) + size);
252 c->next = pool->chunks;
256 pool->allocated += size;
258 rval = ((guint8*)c) + sizeof (Chunk);
262 pool->pos = (guint8*)rval + size;
264 #ifdef TRACE_ALLOCATIONS
265 if (pool == mono_get_corlib ()->mempool) {
266 g_print ("Allocating %d bytes\n", size);
270 if (G_UNLIKELY (pool->pos >= pool->end)) {
273 MonoMemPool *np = g_malloc (sizeof (MonoMemPool) + size);
274 np->next = pool->next;
276 np->pos = (guint8*)np + sizeof (MonoMemPool);
277 np->size = sizeof (MonoMemPool) + size;
278 np->end = np->pos + np->size - sizeof (MonoMemPool);
279 pool->d.allocated += sizeof (MonoMemPool) + size;
280 return (guint8*)np + sizeof (MonoMemPool);
282 int new_size = get_next_size (pool, size);
283 MonoMemPool *np = g_malloc (new_size);
284 np->next = pool->next;
286 pool->pos = (guint8*)np + sizeof (MonoMemPool);
287 np->pos = (guint8*)np + sizeof (MonoMemPool);
290 pool->end = pool->pos + new_size - sizeof (MonoMemPool);
291 pool->d.allocated += new_size;
303 * mono_mempool_alloc0:
305 * same as mono_mempool_alloc, but fills memory with zero.
308 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
312 #ifdef MALLOC_ALLOCATION
313 rval = mono_mempool_alloc (pool, size);
315 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
318 pool->pos = (guint8*)rval + size;
320 if (G_UNLIKELY (pool->pos >= pool->end)) {
321 rval = mono_mempool_alloc (pool, size);
325 memset (rval, 0, size);
330 * mono_mempool_contains_addr:
332 * Determines whenever ADDR is inside the memory used by the mempool.
335 mono_mempool_contains_addr (MonoMemPool *pool,
338 #ifdef MALLOC_ALLOCATION
343 guint8 *p = ((guint8*)c) + sizeof (Chunk);
345 if (addr >= (gpointer)p && addr < (gpointer)(p + c->size))
355 if (addr > (gpointer)p && addr <= (gpointer)((guint8*)p + p->size))
365 * mono_mempool_strdup:
367 * Same as strdup, but allocates memory from the mempool.
368 * Returns: a pointer to the newly allocated string data inside the mempool.
371 mono_mempool_strdup (MonoMemPool *pool,
381 res = mono_mempool_alloc (pool, l + 1);
382 memcpy (res, s, l + 1);
388 * mono_mempool_get_allocated:
390 * Return the amount of memory allocated for this mempool.
393 mono_mempool_get_allocated (MonoMemPool *pool)
395 #ifdef MALLOC_ALLOCATION
396 return pool->allocated;
398 return pool->d.allocated;
403 g_list_prepend_mempool (MonoMemPool *mp, GList *list, gpointer data)
407 new_list = mono_mempool_alloc (mp, sizeof (GList));
408 new_list->data = data;
409 new_list->prev = list ? list->prev : NULL;
410 new_list->next = list;
413 new_list->prev->next = new_list;
415 list->prev = new_list;
421 g_slist_prepend_mempool (MonoMemPool *mp, GSList *list, gpointer data)
425 new_list = mono_mempool_alloc (mp, sizeof (GSList));
426 new_list->data = data;
427 new_list->next = list;