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.
20 * MonoMemPool is for fast allocation of memory. We free
21 * all memory when the pool is destroyed.
24 #if SIZEOF_VOID_P > SIZEOF_LONG
25 #define MEM_ALIGN SIZEOF_VOID_P
27 #define MEM_ALIGN SIZEOF_LONG
30 #define MONO_MEMPOOL_PAGESIZE 8192
41 * Returns: a new memory pool.
47 MonoMemPool *pool = g_malloc (MONO_MEMPOOL_PAGESIZE);
49 pool->pos = (gpointer)pool + sizeof (MonoMemPool);
50 pool->rest = MONO_MEMPOOL_PAGESIZE - sizeof (MonoMemPool);
55 * mono_mempool_destroy:
56 * @pool: the momory pool to destroy
58 * Free all memory associated with this pool.
61 mono_mempool_destroy (MonoMemPool *pool)
75 * @pool: the momory pool to destroy
76 * @size: size of the momory block
78 * Allocates a new block of memory in @pool. @size must
79 * be smaller than 256.
81 * Returns: the address of a newly allocated memory block.
84 mono_mempool_alloc (MonoMemPool *pool, guint size)
88 g_assert (pool != NULL);
91 MonoMemPool *np = g_malloc (sizeof (MonoMemPool) + size);
92 np->next = pool->next;
94 return (gpointer)np + sizeof (MonoMemPool);
97 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
99 if (pool->rest < size) {
100 MonoMemPool *np = g_malloc (MONO_MEMPOOL_PAGESIZE);
101 np->next = pool->next;
103 pool->pos = (gpointer)np + sizeof (MonoMemPool);
104 pool->rest = MONO_MEMPOOL_PAGESIZE - sizeof (MonoMemPool);
115 * mono_mempool_alloc0:
117 * same as mono_mempool_alloc, but fills memory with zero.
120 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
122 gpointer rval = mono_mempool_alloc (pool, size);
123 memset (rval, 0, size);