2 * UCW Library -- Fast Allocator for Fixed-Size Elements
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #ifndef _UCW_ELTPOOL_H
11 #define _UCW_ELTPOOL_H
20 * Memory pool of fixed-sized elements.
21 * You should use this one as an opaque handle only, the insides are internal.
24 struct eltpool_chunk *first_chunk;
25 struct eltpool_free *first_free;
29 uns num_allocated; // Just for debugging
33 struct eltpool_chunk {
34 struct eltpool_chunk *next;
35 /* Chunk data continue here */
39 struct eltpool_free *next;
49 * Create a new memory pool for elements of @elt_size bytes.
50 * The pool will allocate chunks of at least @elts_per_chunk elements.
51 * Higher numbers lead to better allocation times but also to bigger
52 * unused memory blocks. Call @ep_delete() to free all pool's resources.
54 struct eltpool *ep_new(uns elt_size, uns elts_per_chunk);
57 * Release a memory pool created by @ep_new() including all
58 * elements allocated from that pool.
60 void ep_delete(struct eltpool *pool);
63 * Return the total number of bytes allocated by a given
64 * memory pool including all internals.
66 u64 ep_total_size(struct eltpool *pool);
74 void *ep_alloc_slow(struct eltpool *pool); /* Internal. Do not call directly. */
76 * Allocate a new element on a given memory pool.
77 * The results is always aligned to a multiple of the element's size.
79 static inline void *ep_alloc(struct eltpool *pool)
81 pool->num_allocated++;
82 #ifdef CONFIG_FAKE_ELTPOOL
83 return xmalloc(pool->elt_size);
85 struct eltpool_free *elt;
86 if (elt = pool->first_free)
87 pool->first_free = elt->next;
89 elt = ep_alloc_slow(pool);
95 * Release an element previously allocated by @ep_alloc().
96 * Note thet the memory is not really freed (until @mp_delete()),
97 * but it can be reused by future @ep_alloc()'s.
99 static inline void ep_free(struct eltpool *pool, void *p)
101 pool->num_allocated--;
102 #ifdef CONFIG_FAKE_ELTPOOL
106 struct eltpool_free *elt = p;
107 elt->next = pool->first_free;
108 pool->first_free = elt;