]> mj.ucw.cz Git - libucw.git/blob - lib/eltpool.h
Save cache misses by keeping a copy of the hash value next to the pointer.
[libucw.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 };
20
21 struct eltpool_chunk {
22   struct eltpool_chunk *next;
23   /* Chunk data continue here */
24 };
25
26 struct eltpool_free {
27   struct eltpool_free *next;
28 };
29
30 struct eltpool *ep_new(uns elt_size, uns elts_per_chunk);
31 void ep_delete(struct eltpool *pool);
32 void *ep_alloc_slow(struct eltpool *pool);
33
34 static inline void *
35 ep_alloc(struct eltpool *pool)
36 {
37 #ifdef CONFIG_FAKE_ELTPOOL
38   return xmalloc(pool->elt_size);
39 #else
40   struct eltpool_free *elt;
41   if (elt = pool->first_free)
42     pool->first_free = elt->next;
43   else
44     elt = ep_alloc_slow(pool);
45   return elt;
46 #endif
47 }
48
49 static inline void
50 ep_free(struct eltpool *pool, void *p)
51 {
52 #ifdef CONFIG_FAKE_ELTPOOL
53   (void) pool;
54   xfree(p);
55 #else
56   struct eltpool_free *elt = p;
57   elt->next = pool->first_free;
58   pool->first_free = elt;
59 #endif
60 }
61
62 #endif