]> mj.ucw.cz Git - libucw.git/blob - lib/pools.h
See ChangeLogs.
[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 #ifndef POOL_ALIGN
8 #define POOL_ALIGN CPU_STRUCT_ALIGN
9 #endif
10
11 struct mempool {
12   struct memchunk *chunks;
13   byte *free, *last;
14   uns chunk_size, threshold;
15 };
16
17 struct mempool *new_pool(uns);
18 void free_pool(struct mempool *);
19 void *pool_alloc(struct mempool *, uns);
20
21 extern inline void *fast_alloc(struct mempool *p, uns l)
22 {
23   void *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
24   byte *ee = f + l;
25   if (ee > p->last)
26     return pool_alloc(p, l);
27   p->free = ee;
28   return f;
29 }
30
31 extern inline void *fast_alloc_noalign(struct mempool *p, uns l)
32 {
33   void *f = p->free;
34   byte *ee = f + l;
35   if (ee > p->last)
36     return pool_alloc(p, l);
37   p->free = ee;
38   return f;
39 }