]> mj.ucw.cz Git - libucw.git/blob - lib/pools.h
Added mp_strcat() and mp_multicat().
[libucw.git] / lib / pools.h
1 /*
2  *      Sherlock Library -- Memory Pools
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _SHERLOCK_POOLS_H
11 #define _SHERLOCK_POOLS_H
12
13 #ifndef POOL_ALIGN
14 #define POOL_ALIGN CPU_STRUCT_ALIGN
15 #endif
16
17 struct mempool {
18   byte *free, *last;
19   struct memchunk *first, *current, **plast;
20   struct memchunk *first_large;
21   uns chunk_size, threshold;
22 };
23
24 struct mempool *mp_new(uns);
25 void mp_delete(struct mempool *);
26 void mp_flush(struct mempool *);
27 void *mp_alloc(struct mempool *, uns);
28 void *mp_alloc_zero(struct mempool *, uns);
29
30 static inline void *mp_alloc_fast(struct mempool *p, uns l)
31 {
32   byte *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
33   byte *ee = f + l;
34   if (ee > p->last)
35     return mp_alloc(p, l);
36   p->free = ee;
37   return f;
38 }
39
40 static inline void *mp_alloc_fast_noalign(struct mempool *p, uns l)
41 {
42   byte *f = p->free;
43   byte *ee = f + l;
44   if (ee > p->last)
45     return mp_alloc(p, l);
46   p->free = ee;
47   return f;
48 }
49
50 static inline void *
51 mp_start_string(struct mempool *p, uns l)
52 {
53   ASSERT(l <= p->chunk_size);
54   return mp_alloc(p, l);
55 }
56
57 static inline void
58 mp_end_string(struct mempool *p, void *stop)
59 {
60   p->free = stop;
61 }
62
63 /* pool-str.c */
64
65 char *mp_strdup(struct mempool *, char *);
66 char *mp_multicat(struct mempool *, ...);
67 static inline char *
68 mp_strcat(struct mempool *mp, char *x, char *y)
69 {
70   mp_multicat(mp, x, y, NULL);
71 }
72
73 #endif