]> mj.ucw.cz Git - moe.git/blob - lib/eltpool.h
Default Pascal flags no longer enable range and stack checking.
[moe.git] / lib / eltpool.h
1 /*
2  *      UCW Library -- Fast Allocator for Fixed-Size Elements
3  *
4  *      (c) 2007 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_ELTPOOL_H
11 #define _UCW_ELTPOOL_H
12
13 struct eltpool {
14   struct eltpool_chunk *first_chunk;
15   struct eltpool_free *first_free;
16   uns elt_size;
17   uns chunk_size;
18   uns elts_per_chunk;
19   uns num_allocated;            // Just for debugging
20 };
21
22 struct eltpool_chunk {
23   struct eltpool_chunk *next;
24   /* Chunk data continue here */
25 };
26
27 struct eltpool_free {
28   struct eltpool_free *next;
29 };
30
31 struct eltpool *ep_new(uns elt_size, uns elts_per_chunk);
32 void ep_delete(struct eltpool *pool);
33 void *ep_alloc_slow(struct eltpool *pool);
34
35 static inline void *
36 ep_alloc(struct eltpool *pool)
37 {
38   pool->num_allocated++;
39 #ifdef CONFIG_FAKE_ELTPOOL
40   return xmalloc(pool->elt_size);
41 #else
42   struct eltpool_free *elt;
43   if (elt = pool->first_free)
44     pool->first_free = elt->next;
45   else
46     elt = ep_alloc_slow(pool);
47   return elt;
48 #endif
49 }
50
51 static inline void
52 ep_free(struct eltpool *pool, void *p)
53 {
54   pool->num_allocated--;
55 #ifdef CONFIG_FAKE_ELTPOOL
56   (void) pool;
57   xfree(p);
58 #else
59   struct eltpool_free *elt = p;
60   elt->next = pool->first_free;
61   pool->first_free = elt;
62 #endif
63 }
64
65 #endif