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 u64 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);
49 u64 mp_total_size(struct mempool *pool);
52 /*** Allocation routines ***/
54 /* For internal use only, do not call directly */
55 void *mp_alloc_internal(struct mempool *pool, uns size) LIKE_MALLOC;
57 /* The function allocates new <size> bytes on a given memory pool.
58 * If the <size> is zero, the resulting pointer is undefined,
59 * but it may be safely reallocated or used as the parameter
60 * to other functions below.
62 * The resulting pointer is always aligned to a multiple of
63 * CPU_STRUCT_ALIGN bytes and this condition remains true also
64 * after future reallocations.
66 void *mp_alloc(struct mempool *pool, uns size);
68 /* The same as mp_alloc, but the result may not be aligned */
69 void *mp_alloc_noalign(struct mempool *pool, uns size);
71 /* The same as mp_alloc, but fills the newly allocated data with zeroes */
72 void *mp_alloc_zero(struct mempool *pool, uns size);
74 /* Inlined version of mp_alloc() */
76 mp_alloc_fast(struct mempool *pool, uns size)
78 uns avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
81 pool->state.free[0] = avail - size;
82 return pool->state.last[0] - avail;
85 return mp_alloc_internal(pool, size);
88 /* Inlined version of mp_alloc_noalign() */
90 mp_alloc_fast_noalign(struct mempool *pool, uns size)
92 if (size <= pool->state.free[0])
94 void *ptr = pool->state.last[0] - pool->state.free[0];
95 pool->state.free[0] -= size;
99 return mp_alloc_internal(pool, size);
103 /*** Usage as a growing buffer ***/
105 /* For internal use only, do not call directly */
106 void *mp_start_internal(struct mempool *pool, uns size) LIKE_MALLOC;
107 void *mp_grow_internal(struct mempool *pool, uns size);
108 void *mp_spread_internal(struct mempool *pool, void *p, uns size);
111 mp_idx(struct mempool *pool, void *ptr)
113 return ptr == pool->last_big;
116 /* Open a new growing buffer (at least <size> bytes long).
117 * If the <size> is zero, the resulting pointer is undefined,
118 * but it may be safely reallocated or used as the parameter
119 * to other functions below.
121 * The resulting pointer is always aligned to a multiple of
122 * CPU_STRUCT_ALIGN bytes and this condition remains true also
123 * after future reallocations. There is an unaligned version as well.
125 * Keep in mind that you can't make any other <pool> allocations
126 * before you "close" the growing buffer with mp_end().
128 void *mp_start(struct mempool *pool, uns size);
129 void *mp_start_noalign(struct mempool *pool, uns size);
131 /* Inlined version of mp_start() */
133 mp_start_fast(struct mempool *pool, uns size)
135 uns avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
139 pool->state.free[0] = avail;
140 return pool->state.last[0] - avail;
143 return mp_start_internal(pool, size);
146 /* Inlined version of mp_start_noalign() */
148 mp_start_fast_noalign(struct mempool *pool, uns size)
150 if (size <= pool->state.free[0])
153 return pool->state.last[0] - pool->state.free[0];
156 return mp_start_internal(pool, size);
159 /* Return start pointer of the growing buffer allocated by mp_start() or a similar function */
161 mp_ptr(struct mempool *pool)
163 return pool->state.last[pool->idx] - pool->state.free[pool->idx];
166 /* Return the number of bytes available for extending the growing buffer */
168 mp_avail(struct mempool *pool)
170 return pool->state.free[pool->idx];
173 /* Grow the buffer allocated by mp_start() to be at least <size> bytes long
174 * (<size> may be less than mp_avail(), even zero). Reallocated buffer may
175 * change its starting position. The content will be unchanged to the minimum
176 * of the old and new sizes; newly allocated memory will be uninitialized.
177 * Multiple calls to mp_grow have amortized linear cost wrt. the maximum value of <size>. */
179 mp_grow(struct mempool *pool, uns size)
181 return (size <= mp_avail(pool)) ? mp_ptr(pool) : mp_grow_internal(pool, size);
184 /* Grow the buffer by at least one byte -- equivalent to mp_grow(pool, mp_avail(pool) + 1) */
186 mp_expand(struct mempool *pool)
188 return mp_grow_internal(pool, mp_avail(pool) + 1);
191 /* Ensure that there is at least <size> bytes free after <p>, if not, reallocate and adjust <p>. */
193 mp_spread(struct mempool *pool, void *p, uns size)
195 return (((uns)(pool->state.last[pool->idx] - p) >= size) ? p : mp_spread_internal(pool, p, size));
198 /* Close the growing buffer. The <end> must point just behind the data, you want to keep
199 * allocated (so it can be in the interval [mp_ptr(pool), mp_ptr(pool) + mp_avail(pool)]).
200 * Returns a pointer to the beginning of the just closed block. */
202 mp_end(struct mempool *pool, void *end)
204 void *p = mp_ptr(pool);
205 pool->state.free[pool->idx] = pool->state.last[pool->idx] - end;
209 /* Return size in bytes of the last allocated memory block (with mp_alloc*() or mp_end()). */
211 mp_size(struct mempool *pool, void *ptr)
213 uns idx = mp_idx(pool, ptr);
214 return pool->state.last[idx] - ptr - pool->state.free[idx];
217 /* Open the last memory block (allocated with mp_alloc*() or mp_end())
218 * for growing and return its size in bytes. The contents and the start pointer
219 * remain unchanged. Do not forget to call mp_end() to close it. */
220 uns mp_open(struct mempool *pool, void *ptr);
222 /* Inlined version of mp_open() */
224 mp_open_fast(struct mempool *pool, void *ptr)
226 pool->idx = mp_idx(pool, ptr);
227 uns size = pool->state.last[pool->idx] - ptr - pool->state.free[pool->idx];
228 pool->state.free[pool->idx] += size;
232 /* Reallocate the last memory block (allocated with mp_alloc*() or mp_end())
233 * to the new <size>. Behavior is similar to mp_grow(), but the resulting
234 * block is closed. */
235 void *mp_realloc(struct mempool *pool, void *ptr, uns size);
237 /* The same as mp_realloc(), but fills the additional bytes (if any) with zeroes */
238 void *mp_realloc_zero(struct mempool *pool, void *ptr, uns size);
240 /* Inlined version of mp_realloc() */
242 mp_realloc_fast(struct mempool *pool, void *ptr, uns size)
244 mp_open_fast(pool, ptr);
245 ptr = mp_grow(pool, size);
246 mp_end(pool, ptr + size);
251 /*** Usage as a stack ***/
253 /* Save the current state of a memory pool.
254 * Do not call this function with an opened growing buffer. */
256 mp_save(struct mempool *pool, struct mempool_state *state)
258 *state = pool->state;
259 pool->state.next = state;
262 /* Save the current state to a newly allocated mempool_state structure.
263 * Do not call this function with an opened growing buffer. */
264 struct mempool_state *mp_push(struct mempool *pool);
266 /* Restore the state saved by mp_save() or mp_push() and free all
267 * data allocated after that point (including the state structure itself).
268 * You can't reallocate the last memory block from the saved state. */
269 void mp_restore(struct mempool *pool, struct mempool_state *state);
271 /* Restore the state saved by the last call to mp_push().
272 * mp_pop() and mp_push() works as a stack so you can push more states safely. */
273 void mp_pop(struct mempool *pool);
276 /*** mempool-str.c ***/
278 char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC;
279 void *mp_memdup(struct mempool *, const void *, uns) LIKE_MALLOC;
280 char *mp_multicat(struct mempool *, ...) LIKE_MALLOC SENTINEL_CHECK;
281 static inline char * LIKE_MALLOC
282 mp_strcat(struct mempool *mp, const char *x, const char *y)
284 return mp_multicat(mp, x, y, NULL);
286 char *mp_strjoin(struct mempool *p, char **a, uns n, uns sep) LIKE_MALLOC;
289 /*** mempool-fmt.c ***/
291 char *mp_printf(struct mempool *mp, const char *fmt, ...) FORMAT_CHECK(printf,2,3) LIKE_MALLOC;
292 char *mp_vprintf(struct mempool *mp, const char *fmt, va_list args) LIKE_MALLOC;
293 char *mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...) FORMAT_CHECK(printf,3,4);
294 char *mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args);