]> mj.ucw.cz Git - libucw.git/blob - lib/pools.h
Adding all files developed during last week...
[libucw.git] / lib / pools.h
1 /*
2  *      Sherlock Library -- Memory Pools
3  *
4  *      (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #define POOL_ALIGN 4
8
9 struct mempool {
10   struct memchunk *chunks;
11   byte *free, *last;
12   uns chunk_size, threshold;
13 };
14
15 struct mempool *new_pool(uns);
16 void free_pool(struct mempool *);
17 void *pool_alloc(struct mempool *, uns);
18
19 extern inline void *fast_alloc(struct mempool *p, uns l)
20 {
21   void *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
22   byte *ee = f + l;
23   if (ee > p->last)
24     return pool_alloc(p, l);
25   p->free = ee;
26   return f;
27 }
28
29 extern inline void *fast_alloc_noalign(struct mempool *p, uns l)
30 {
31   void *f = p->free;
32   byte *ee = f + l;
33   if (ee > p->last)
34     return pool_alloc(p, l);
35   p->free = ee;
36   return f;
37 }