2 * UCW Library -- Memory Pools
4 * (c) 1997--2005 Martin Mares <mj@ucw.cz>
5 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 /* Memory pool state (see mp_push(), ...) */
15 struct mempool_state {
18 struct mempool_state *next;
23 struct mempool_state state;
24 void *unused, *last_big;
25 uns chunk_size, threshold, idx;
28 /* Statistics (see mp_stats()) */
29 struct mempool_stats {
30 uns total_size; /* Real allocated size in bytes */
31 uns chain_count[3]; /* Number of allocated chunks in small/big/unused chains */
32 uns chain_size[3]; /* Size of allocated chunks in small/big/unused chains */
35 /* Initialize a given mempool structure. Chunk size must be in the interval [1, UINT_MAX / 2] */
36 void mp_init(struct mempool *pool, uns chunk_size);
38 /* Allocate and initialize a new memory pool. See mp_init for chunk size limitations. */
39 struct mempool *mp_new(uns chunk_size);
41 /* Cleanup mempool initialized by mp_init or mp_new */
42 void mp_delete(struct mempool *pool);
44 /* Free all data on a memory pool (saves some empty chunks for later allocations) */
45 void mp_flush(struct mempool *pool);
47 /* Compute some statistics for debug purposes. See the definition of the mempool_stats structure. */
48 void mp_stats(struct mempool *pool, struct mempool_stats *stats);
51 /*** Allocation routines ***/
53 /* For internal use only, do not call directly */
54 void *mp_alloc_internal(struct mempool *pool, uns size) LIKE_MALLOC;
56 /* The function allocates new <size> bytes on a given memory pool.
57 * If the <size> is zero, the resulting pointer is undefined,
58 * but it may be safely reallocated or used as the parameter
59 * to other functions below.
61 * The resulting pointer is always aligned to a multiple of
62 * CPU_STRUCT_ALIGN bytes and this condition remains true also
63 * after future reallocations.
65 void *mp_alloc(struct mempool *pool, uns size);
67 /* The same as mp_alloc, but the result may not be aligned */
68 void *mp_alloc_noalign(struct mempool *pool, uns size);
70 /* The same as mp_alloc, but fills the newly allocated data with zeroes */
71 void *mp_alloc_zero(struct mempool *pool, uns size);
73 /* Inlined version of mp_alloc() */
75 mp_alloc_fast(struct mempool *pool, uns size)
77 uns avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
80 pool->state.free[0] = avail - size;
81 return pool->state.last[0] - avail;
84 return mp_alloc_internal(pool, size);
87 /* Inlined version of mp_alloc_noalign() */
89 mp_alloc_fast_noalign(struct mempool *pool, uns size)
91 if (size <= pool->state.free[0])
93 void *ptr = pool->state.last[0] - pool->state.free[0];
94 pool->state.free[0] -= size;
98 return mp_alloc_internal(pool, size);
102 /*** Usage as a growing buffer ***/
104 /* For internal use only, do not call directly */
105 void *mp_start_internal(struct mempool *pool, uns size) LIKE_MALLOC;
106 void *mp_grow_internal(struct mempool *pool, uns size);
107 void *mp_spread_internal(struct mempool *pool, void *p, uns size);
110 mp_idx(struct mempool *pool, void *ptr)
112 return ptr == pool->last_big;
115 /* Open a new growing buffer (at least <size> bytes long).
116 * If the <size> is zero, the resulting pointer is undefined,
117 * but it may be safely reallocated or used as the parameter
118 * to other functions below.
120 * The resulting pointer is always aligned to a multiple of
121 * CPU_STRUCT_ALIGN bytes and this condition remains true also
122 * after future reallocations. There is an unaligned version as well.
124 * Keep in mind that you can't make any other <pool> allocations
125 * before you "close" the growing buffer with mp_end().
127 void *mp_start(struct mempool *pool, uns size);
128 void *mp_start_noalign(struct mempool *pool, uns size);
130 /* Inlined version of mp_start() */
132 mp_start_fast(struct mempool *pool, uns size)
134 uns avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
138 pool->state.free[0] = avail;
139 return pool->state.last[0] - avail;
142 return mp_start_internal(pool, size);
145 /* Inlined version of mp_start_noalign() */
147 mp_start_fast_noalign(struct mempool *pool, uns size)
149 if (size <= pool->state.free[0])
152 return pool->state.last[0] - pool->state.free[0];
155 return mp_start_internal(pool, size);
158 /* Return start pointer of the growing buffer allocated by mp_start() or a similar function */
160 mp_ptr(struct mempool *pool)
162 return pool->state.last[pool->idx] - pool->state.free[pool->idx];
165 /* Return the number of bytes available for extending the growing buffer */
167 mp_avail(struct mempool *pool)
169 return pool->state.free[pool->idx];
172 /* Grow the buffer allocated by mp_start() to be at least <size> bytes long
173 * (<size> may be less than mp_avail(), even zero). Reallocated buffer may
174 * change its starting position. The content will be unchanged to the minimum
175 * of the old and new sizes; newly allocated memory will be uninitialized.
176 * Multiple calls to mp_grow have amortized linear cost wrt. the maximum value of <size>. */
178 mp_grow(struct mempool *pool, uns size)
180 return (size <= mp_avail(pool)) ? mp_ptr(pool) : mp_grow_internal(pool, size);
183 /* Grow the buffer by at least one byte -- equivalent to mp_grow(pool, mp_avail(pool) + 1) */
185 mp_expand(struct mempool *pool)
187 return mp_grow_internal(pool, mp_avail(pool) + 1);
190 /* Ensure that there is at least <size> bytes free after <p>, if not, reallocate and adjust <p>. */
192 mp_spread(struct mempool *pool, void *p, uns size)
194 return (((uns)(pool->state.last[pool->idx] - p) >= size) ? p : mp_spread_internal(pool, p, size));
197 /* Close the growing buffer. The <end> must point just behind the data, you want to keep
198 * allocated (so it can be in the interval [mp_ptr(pool), mp_ptr(pool) + mp_avail(pool)]).
199 * Returns a pointer to the beginning of the just closed block. */
201 mp_end(struct mempool *pool, void *end)
203 void *p = mp_ptr(pool);
204 pool->state.free[pool->idx] = pool->state.last[pool->idx] - end;
208 /* Return size in bytes of the last allocated memory block (with mp_alloc*() or mp_end()). */
210 mp_size(struct mempool *pool, void *ptr)
212 uns idx = mp_idx(pool, ptr);
213 return pool->state.last[idx] - ptr - pool->state.free[idx];
216 /* Open the last memory block (allocated with mp_alloc*() or mp_end())
217 * for growing and return its size in bytes. The contents and the start pointer
218 * remain unchanged. Do not forget to call mp_end() to close it. */
219 uns mp_open(struct mempool *pool, void *ptr);
221 /* Inlined version of mp_open() */
223 mp_open_fast(struct mempool *pool, void *ptr)
225 pool->idx = mp_idx(pool, ptr);
226 uns size = pool->state.last[pool->idx] - ptr - pool->state.free[pool->idx];
227 pool->state.free[pool->idx] += size;
231 /* Reallocate the last memory block (allocated with mp_alloc*() or mp_end())
232 * to the new <size>. Behavior is similar to mp_grow(), but the resulting
233 * block is closed. */
234 void *mp_realloc(struct mempool *pool, void *ptr, uns size);
236 /* The same as mp_realloc(), but fills the additional bytes (if any) with zeroes */
237 void *mp_realloc_zero(struct mempool *pool, void *ptr, uns size);
239 /* Inlined version of mp_realloc() */
241 mp_realloc_fast(struct mempool *pool, void *ptr, uns size)
243 mp_open_fast(pool, ptr);
244 ptr = mp_grow(pool, size);
245 mp_end(pool, ptr + size);
250 /*** Usage as a stack ***/
252 /* Save the current state of a memory pool.
253 * Do not call this function with an opened growing buffer. */
255 mp_save(struct mempool *pool, struct mempool_state *state)
257 *state = pool->state;
258 pool->state.next = state;
261 /* Save the current state to a newly allocated mempool_state structure.
262 * Do not call this function with an opened growing buffer. */
263 struct mempool_state *mp_push(struct mempool *pool);
265 /* Restore the state saved by mp_save() or mp_push() and free all
266 * data allocated after that point (including the state structure itself).
267 * You can't reallocate the last memory block from the saved state. */
268 void mp_restore(struct mempool *pool, struct mempool_state *state);
270 /* Restore the state saved by the last call to mp_push().
271 * mp_pop() and mp_push() works as a stack so you can push more states safely. */
272 void mp_pop(struct mempool *pool);
275 /*** mempool-str.c ***/
277 char *mp_strdup(struct mempool *, char *) LIKE_MALLOC;
278 void *mp_memdup(struct mempool *, void *, uns) LIKE_MALLOC;
279 char *mp_multicat(struct mempool *, ...) LIKE_MALLOC SENTINEL_CHECK;
280 static inline char * LIKE_MALLOC
281 mp_strcat(struct mempool *mp, char *x, char *y)
283 return mp_multicat(mp, x, y, NULL);
285 char *mp_strjoin(struct mempool *p, char **a, uns n, uns sep) LIKE_MALLOC;
288 /*** mempool-fmt.c ***/
290 char *mp_printf(struct mempool *mp, const char *fmt, ...) FORMAT_CHECK(printf,2,3) LIKE_MALLOC;
291 char *mp_vprintf(struct mempool *mp, const char *fmt, va_list args) LIKE_MALLOC;
292 char *mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...) FORMAT_CHECK(printf,3,4);
293 char *mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args);