]> mj.ucw.cz Git - libucw.git/blob - ucw/eltpool.h
xtypes: bool now supports yes/no strings
[libucw.git] / ucw / 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 #ifdef CONFIG_UCW_CLEAN_ABI
14 #define ep_alloc_slow ucw_ep_alloc_slow
15 #define ep_delete ucw_ep_delete
16 #define ep_new ucw_ep_new
17 #define ep_total_size ucw_ep_total_size
18 #endif
19
20 /***
21  * [[defs]]
22  * Definitions
23  * -----------
24  ***/
25
26 /**
27  * Memory pool of fixed-sized elements.
28  * You should use this one as an opaque handle only, the insides are internal.
29  **/
30 struct eltpool {
31   struct eltpool_chunk *first_chunk;
32   struct eltpool_free *first_free;
33   uint elt_size;
34   uint chunk_size;
35   uint elts_per_chunk;
36   uint num_allocated;           // Just for debugging
37   uint num_chunks;
38 };
39
40 struct eltpool_chunk {
41   struct eltpool_chunk *next;
42   /* Chunk data continue here */
43 };
44
45 struct eltpool_free {
46   struct eltpool_free *next;
47 };
48
49 /***
50  * [[basic]]
51  * Basic manipulation
52  * ------------------
53  ***/
54
55 /**
56  * Create a new memory pool for elements of @elt_size bytes.
57  * The pool will allocate chunks of at least @elts_per_chunk elements.
58  * Higher numbers lead to better allocation times but also to bigger
59  * unused memory blocks. Call @ep_delete() to free all pool's resources.
60  *
61  * Element pools can be treated as <<trans:respools,resources>>, see <<trans:res_eltpool()>>.
62  **/
63 struct eltpool *ep_new(uint elt_size, uint elts_per_chunk);
64
65 /**
66  * Release a memory pool created by @ep_new() including all
67  * elements allocated from that pool.
68  **/
69 void ep_delete(struct eltpool *pool);
70
71 /**
72  * Return the total number of bytes allocated by a given
73  * memory pool including all internals.
74  **/
75 u64 ep_total_size(struct eltpool *pool);
76
77 /***
78  * [[alloc]]
79  * Allocation routines
80  * -------------------
81  ***/
82
83 void *ep_alloc_slow(struct eltpool *pool); /* Internal. Do not call directly. */
84 /**
85  * Allocate a new element on a given memory pool.
86  * The results is always aligned to a multiple of the element's size.
87  **/
88 static inline void *ep_alloc(struct eltpool *pool)
89 {
90   pool->num_allocated++;
91 #ifdef CONFIG_UCW_FAKE_ELTPOOL
92   return xmalloc(pool->elt_size);
93 #else
94   struct eltpool_free *elt;
95   if (elt = pool->first_free)
96     pool->first_free = elt->next;
97   else
98     elt = ep_alloc_slow(pool);
99   return elt;
100 #endif
101 }
102
103 /**
104  * Release an element previously allocated by @ep_alloc().
105  * Note thet the memory is not really freed (until @mp_delete()),
106  * but it can be reused by future @ep_alloc()'s.
107  **/
108 static inline void ep_free(struct eltpool *pool, void *p)
109 {
110   pool->num_allocated--;
111 #ifdef CONFIG_UCW_FAKE_ELTPOOL
112   (void) pool;
113   xfree(p);
114 #else
115   struct eltpool_free *elt = p;
116   elt->next = pool->first_free;
117   pool->first_free = elt;
118 #endif
119 }
120
121 #endif