]> mj.ucw.cz Git - libucw.git/blob - lib/pools.h
Introduced mp_alloc_zero().
[libucw.git] / lib / pools.h
1 /*
2  *      Sherlock Library -- Memory Pools
3  *
4  *      (c) 1997--2001 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef POOL_ALIGN
8 #define POOL_ALIGN CPU_STRUCT_ALIGN
9 #endif
10
11 struct mempool {
12   byte *free, *last;
13   struct memchunk *first, *current, **plast;
14   struct memchunk *first_large;
15   uns chunk_size, threshold;
16 };
17
18 struct mempool *mp_new(uns);
19 void mp_delete(struct mempool *);
20 void mp_flush(struct mempool *);
21 void *mp_alloc(struct mempool *, uns);
22 void *mp_alloc_zero(struct mempool *, uns);
23
24 static inline void *mp_alloc_fast(struct mempool *p, uns l)
25 {
26   byte *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
27   byte *ee = f + l;
28   if (ee > p->last)
29     return mp_alloc(p, l);
30   p->free = ee;
31   return f;
32 }
33
34 static inline void *mp_alloc_fast_noalign(struct mempool *p, uns l)
35 {
36   byte *f = p->free;
37   byte *ee = f + l;
38   if (ee > p->last)
39     return mp_alloc(p, l);
40   p->free = ee;
41   return f;
42 }