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.
26 #define MONO_MEMPOOL_PAGESIZE 8192
32 double pad; /* to assure proper alignment */
38 * Returns: a new memory pool.
43 MonoMemPool *pool = g_malloc (MONO_MEMPOOL_PAGESIZE);
46 pool->pos = (char *)pool + sizeof (MonoMemPool);
47 pool->rest = MONO_MEMPOOL_PAGESIZE - sizeof (MonoMemPool);
52 * mono_mempool_destroy:
53 * @pool: the momory pool to destroy
55 * Free all memory associated with this pool.
58 mono_mempool_destroy (MonoMemPool *pool)
72 * @pool: the momory pool to destroy
73 * @size: size of the momory block
75 * Allocates a new block of memory in @pool. @size must
76 * be smaller than 256.
78 * Returns: the address of a newly allocated memory block.
81 mono_mempool_alloc (MonoMemPool *pool, guint size)
85 g_assert (pool != NULL);
87 size = (size + MEM_ALIGN - 1) & ~(MEM_ALIGN - 1);
89 if (pool->rest < size) {
91 MonoMemPool *np = g_malloc (sizeof (MonoMemPool) + size);
92 np->next = pool->next;
94 return (char *)np + sizeof (MonoMemPool);
96 MonoMemPool *np = g_malloc (MONO_MEMPOOL_PAGESIZE);
97 np->next = pool->next;
99 pool->pos = (char *)np + sizeof (MonoMemPool);
100 pool->rest = MONO_MEMPOOL_PAGESIZE - sizeof (MonoMemPool);
106 pool->pos = (char *)pool->pos + size;
112 * mono_mempool_alloc0:
114 * same as mono_mempool_alloc, but fills memory with zero.
117 mono_mempool_alloc0 (MonoMemPool *pool, guint size)
119 gpointer rval = mono_mempool_alloc (pool, size);
120 memset (rval, 0, size);