]> mj.ucw.cz Git - libucw.git/blob - lib/pools.h
Moved tests to extra file.
[libucw.git] / lib / pools.h
1 /*
2  *      Sherlock Library -- Memory Pools
3  *
4  *      (c) 1997--1999 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   byte *free, *last;
13   struct memchunk *first, *current, **plast;
14   struct memchunk *first_large;
15   uns chunk_size, threshold;
16 };
17
18 struct mempool *new_pool(uns);
19 void free_pool(struct mempool *);
20 void flush_pool(struct mempool *);
21 void *pool_alloc(struct mempool *, uns);
22
23 extern inline void *fast_alloc(struct mempool *p, uns l)
24 {
25   byte *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
26   byte *ee = f + l;
27   if (ee > p->last)
28     return pool_alloc(p, l);
29   p->free = ee;
30   return f;
31 }
32
33 extern inline void *fast_alloc_noalign(struct mempool *p, uns l)
34 {
35   byte *f = p->free;
36   byte *ee = f + l;
37   if (ee > p->last)
38     return pool_alloc(p, l);
39   p->free = ee;
40   return f;
41 }