]> mj.ucw.cz Git - netgrind.git/blob - lib/pools.h
TODO: A note on IPv6
[netgrind.git] / lib / pools.h
1 /*
2  *      Sherlock Library -- Memory Pools
3  *
4  *      (c) 1997--2003 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 _SHERLOCK_POOLS_H
11 #define _SHERLOCK_POOLS_H
12
13 #ifndef POOL_ALIGN
14 #define POOL_ALIGN CPU_STRUCT_ALIGN
15 #endif
16
17 #include <stdint.h>
18
19 struct mempool {
20   byte *free, *last;
21   struct memchunk *first, *current, **plast;
22   struct memchunk *first_large;
23   uns chunk_size, threshold;
24 };
25
26 struct mempool *mp_new(uns);
27 void mp_delete(struct mempool *);
28 void mp_flush(struct mempool *);
29 void *mp_alloc(struct mempool *, uns);
30 void *mp_alloc_zero(struct mempool *, uns);
31
32 static inline void *mp_alloc_fast(struct mempool *p, uns l)
33 {
34   byte *f = (void *) (((uintptr_t) p->free + POOL_ALIGN - 1) & ~((uintptr_t) POOL_ALIGN - 1));
35   byte *ee = f + l;
36   if (ee > p->last)
37     return mp_alloc(p, l);
38   p->free = ee;
39   return f;
40 }
41
42 static inline void *mp_alloc_fast_noalign(struct mempool *p, uns l)
43 {
44   byte *f = p->free;
45   byte *ee = f + l;
46   if (ee > p->last)
47     return mp_alloc(p, l);
48   p->free = ee;
49   return f;
50 }
51
52 static inline void *
53 mp_start_string(struct mempool *p, uns l)
54 {
55   ASSERT(l <= p->chunk_size);
56   return mp_alloc(p, l);
57 }
58
59 static inline void
60 mp_end_string(struct mempool *p, void *stop)
61 {
62   p->free = stop;
63 }
64
65 #endif