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.
19 * MonoMemPool is for fast allocation of memory. We free
20 * all memory when the pool is destroyed.
23 #if SIZEOF_VOID_P > SIZEOF_LONG
24 #define MEM_ALIGN SIZEOF_VOID_P
26 #define MEM_ALIGN SIZEOF_LONG
29 #define MONO_MEMPOOL_PAGESIZE 8192
40 * Returns: a new memory pool.
46 MonoMemPool *pool = g_malloc (MONO_MEMPOOL_PAGESIZE);
48 pool->pos = (gpointer)pool + sizeof (MonoMemPool);
49 pool->rest = MONO_MEMPOOL_PAGESIZE - sizeof (MonoMemPool);
54 * mono_mempool_destroy:
55 * @pool: the momory pool to destroy
57 * Free all memory associated with this pool.
60 mono_mempool_destroy (MonoMemPool *pool)
74 * @pool: the momory pool to destroy
75 * @size: size of the momory block
77 * Allocates a new block of memory in @pool. @size must
78 * be smaller than 256.
80 * Returns: the address of a newly allocated memory block.
83 mono_mempool_alloc (MonoMemPool *pool, guint size)
87 g_assert (pool != NULL);
89 /* we should also handle large blocks */
90 g_assert (size < 4096);
92 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
94 if (pool->rest < size) {
95 MonoMemPool *np = g_malloc (MONO_MEMPOOL_PAGESIZE);
96 np->next = pool->next;
98 pool->pos = (gpointer)np + sizeof (MonoMemPool);
99 pool->rest = MONO_MEMPOOL_PAGESIZE - sizeof (MonoMemPool);
110 * mono_mempool_alloc0:
112 * same as mono_mempool_alloc, but fills memory with zero.
115 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
117 gpointer rval = mono_mempool_alloc (pool, size);
118 memset (rval, 0, size);