]> mj.ucw.cz Git - libucw.git/blob - lib/pools.h
ea3fbddf0d50f94008b1da79227d12e41172b925
[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 _SHERLOCK_POOLS_H
8 #define _SHERLOCK_POOLS_H
9
10 #ifndef POOL_ALIGN
11 #define POOL_ALIGN CPU_STRUCT_ALIGN
12 #endif
13
14 struct mempool {
15   byte *free, *last;
16   struct memchunk *first, *current, **plast;
17   struct memchunk *first_large;
18   uns chunk_size, threshold;
19 };
20
21 struct mempool *mp_new(uns);
22 void mp_delete(struct mempool *);
23 void mp_flush(struct mempool *);
24 void *mp_alloc(struct mempool *, uns);
25 void *mp_alloc_zero(struct mempool *, uns);
26
27 static inline void *mp_alloc_fast(struct mempool *p, uns l)
28 {
29   byte *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
30   byte *ee = f + l;
31   if (ee > p->last)
32     return mp_alloc(p, l);
33   p->free = ee;
34   return f;
35 }
36
37 static inline void *mp_alloc_fast_noalign(struct mempool *p, uns l)
38 {
39   byte *f = p->free;
40   byte *ee = f + l;
41   if (ee > p->last)
42     return mp_alloc(p, l);
43   p->free = ee;
44   return f;
45 }
46
47 #endif