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 #ifdef CONFIG_UCW_CLEAN_ABI
15 #define mp_alloc ucw_mp_alloc
16 #define mp_alloc_internal ucw_mp_alloc_internal
17 #define mp_alloc_noalign ucw_mp_alloc_noalign
18 #define mp_alloc_zero ucw_mp_alloc_zero
19 #define mp_delete ucw_mp_delete
20 #define mp_flush ucw_mp_flush
21 #define mp_grow_internal ucw_mp_grow_internal
22 #define mp_init ucw_mp_init
23 #define mp_memdup ucw_mp_memdup
24 #define mp_multicat ucw_mp_multicat
25 #define mp_new ucw_mp_new
26 #define mp_open ucw_mp_open
27 #define mp_pop ucw_mp_pop
28 #define mp_printf ucw_mp_printf
29 #define mp_printf_append ucw_mp_printf_append
30 #define mp_push ucw_mp_push
31 #define mp_realloc ucw_mp_realloc
32 #define mp_realloc_zero ucw_mp_realloc_zero
33 #define mp_restore ucw_mp_restore
34 #define mp_spread_internal ucw_mp_spread_internal
35 #define mp_start ucw_mp_start
36 #define mp_start_internal ucw_mp_start_internal
37 #define mp_start_noalign ucw_mp_start_noalign
38 #define mp_stats ucw_mp_stats
39 #define mp_str_from_mem ucw_mp_str_from_mem
40 #define mp_strdup ucw_mp_strdup
41 #define mp_strjoin ucw_mp_strjoin
42 #define mp_total_size ucw_mp_total_size
43 #define mp_vprintf ucw_mp_vprintf
44 #define mp_vprintf_append ucw_mp_vprintf_append
54 * Memory pool state (see @mp_push(), ...).
55 * You should use this one as an opaque handle only, the insides are internal.
57 struct mempool_state {
60 struct mempool_state *next;
65 * You should use this one as an opaque handle only, the insides are internal.
68 struct mempool_state state;
69 void *unused, *last_big;
70 uns chunk_size, threshold, idx;
73 struct mempool_stats { /** Mempool statistics. See @mp_stats(). **/
74 u64 total_size; /* Real allocated size in bytes */
75 uns chain_count[3]; /* Number of allocated chunks in small/big/unused chains */
76 uns chain_size[3]; /* Size of allocated chunks in small/big/unused chains */
86 * Initialize a given mempool structure.
87 * @chunk_size must be in the interval `[1, UINT_MAX / 2]`.
88 * It will allocate memory by this large chunks and take
89 * memory to satisfy requests from them.
91 * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
93 void mp_init(struct mempool *pool, uns chunk_size);
96 * Allocate and initialize a new memory pool.
97 * See @mp_init() for @chunk_size limitations.
99 * The new mempool structure is allocated on the new mempool.
101 * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
103 struct mempool *mp_new(uns chunk_size);
106 * Cleanup mempool initialized by mp_init or mp_new.
107 * Frees all the memory allocated by this mempool and,
108 * if created by @mp_new(), the @pool itself.
110 void mp_delete(struct mempool *pool);
113 * Frees all data on a memory pool, but leaves it working.
114 * It can keep some of the chunks allocated to serve
115 * further allocation requests. Leaves the @pool alive,
116 * even if it was created with @mp_new().
118 void mp_flush(struct mempool *pool);
121 * Compute some statistics for debug purposes.
122 * See the definition of the <<struct_mempool_stats,mempool_stats structure>>.
124 void mp_stats(struct mempool *pool, struct mempool_stats *stats);
125 u64 mp_total_size(struct mempool *pool); /** How many bytes were allocated by the pool. **/
130 * Allocation routines
131 * -------------------
134 /* For internal use only, do not call directly */
135 void *mp_alloc_internal(struct mempool *pool, uns size) LIKE_MALLOC;
138 * The function allocates new @size bytes on a given memory pool.
139 * If the @size is zero, the resulting pointer is undefined,
140 * but it may be safely reallocated or used as the parameter
141 * to other functions below.
143 * The resulting pointer is always aligned to a multiple of
144 * `CPU_STRUCT_ALIGN` bytes and this condition remains true also
145 * after future reallocations.
147 void *mp_alloc(struct mempool *pool, uns size);
150 * The same as @mp_alloc(), but the result may be unaligned.
152 void *mp_alloc_noalign(struct mempool *pool, uns size);
155 * The same as @mp_alloc(), but fills the newly allocated memory with zeroes.
157 void *mp_alloc_zero(struct mempool *pool, uns size);
160 * Inlined version of @mp_alloc().
162 static inline void *mp_alloc_fast(struct mempool *pool, uns size)
164 uns avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
167 pool->state.free[0] = avail - size;
168 return (byte *)pool->state.last[0] - avail;
171 return mp_alloc_internal(pool, size);
175 * Inlined version of @mp_alloc_noalign().
177 static inline void *mp_alloc_fast_noalign(struct mempool *pool, uns size)
179 if (size <= pool->state.free[0])
181 void *ptr = (byte *)pool->state.last[0] - pool->state.free[0];
182 pool->state.free[0] -= size;
186 return mp_alloc_internal(pool, size);
194 * You do not need to know, how a buffer will need to be large,
195 * you can grow it incrementally to needed size. You can grow only
196 * one buffer at a time on a given mempool.
198 * Similar functionality is provided by <<growbuf:,growing buffes>> module.
201 /* For internal use only, do not call directly */
202 void *mp_start_internal(struct mempool *pool, uns size) LIKE_MALLOC;
203 void *mp_grow_internal(struct mempool *pool, uns size);
204 void *mp_spread_internal(struct mempool *pool, void *p, uns size);
206 static inline uns mp_idx(struct mempool *pool, void *ptr)
208 return ptr == pool->last_big;
212 * Open a new growing buffer (at least @size bytes long).
213 * If the @size is zero, the resulting pointer is undefined,
214 * but it may be safely reallocated or used as the parameter
215 * to other functions below.
217 * The resulting pointer is always aligned to a multiple of
218 * `CPU_STRUCT_ALIGN` bytes and this condition remains true also
219 * after future reallocations. There is an unaligned version as well.
221 * Keep in mind that you can't make any other pool allocations
222 * before you "close" the growing buffer with @mp_end().
224 void *mp_start(struct mempool *pool, uns size);
225 void *mp_start_noalign(struct mempool *pool, uns size);
228 * Inlined version of @mp_start().
230 static inline void *mp_start_fast(struct mempool *pool, uns size)
232 uns avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
236 pool->state.free[0] = avail;
237 return (byte *)pool->state.last[0] - avail;
240 return mp_start_internal(pool, size);
244 * Inlined version of @mp_start_noalign().
246 static inline void *mp_start_fast_noalign(struct mempool *pool, uns size)
248 if (size <= pool->state.free[0])
251 return (byte *)pool->state.last[0] - pool->state.free[0];
254 return mp_start_internal(pool, size);
258 * Return start pointer of the growing buffer allocated by latest @mp_start() or a similar function.
260 static inline void *mp_ptr(struct mempool *pool)
262 return (byte *)pool->state.last[pool->idx] - pool->state.free[pool->idx];
266 * Return the number of bytes available for extending the growing buffer.
267 * (Before a reallocation will be needed).
269 static inline uns mp_avail(struct mempool *pool)
271 return pool->state.free[pool->idx];
275 * Grow the buffer allocated by @mp_start() to be at least @size bytes long
276 * (@size may be less than @mp_avail(), even zero). Reallocated buffer may
277 * change its starting position. The content will be unchanged to the minimum
278 * of the old and new sizes; newly allocated memory will be uninitialized.
279 * Multiple calls to mp_grow() have amortized linear cost wrt. the maximum value of @size. */
280 static inline void *mp_grow(struct mempool *pool, uns size)
282 return (size <= mp_avail(pool)) ? mp_ptr(pool) : mp_grow_internal(pool, size);
286 * Grow the buffer by at least one byte -- equivalent to <<mp_grow(),`mp_grow`>>`(@pool, @mp_avail(pool) + 1)`.
288 static inline void *mp_expand(struct mempool *pool)
290 return mp_grow_internal(pool, mp_avail(pool) + 1);
294 * Ensure that there is at least @size bytes free after @p,
295 * if not, reallocate and adjust @p.
297 static inline void *mp_spread(struct mempool *pool, void *p, uns size)
299 return (((uns)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size));
303 * Close the growing buffer. The @end must point just behind the data, you want to keep
304 * allocated (so it can be in the interval `[@mp_ptr(@pool), @mp_ptr(@pool) + @mp_avail(@pool)]`).
305 * Returns a pointer to the beginning of the just closed block.
307 static inline void *mp_end(struct mempool *pool, void *end)
309 void *p = mp_ptr(pool);
310 pool->state.free[pool->idx] = (byte *)pool->state.last[pool->idx] - (byte *)end;
315 * Return size in bytes of the last allocated memory block (with @mp_alloc() or @mp_end()).
317 static inline uns mp_size(struct mempool *pool, void *ptr)
319 uns idx = mp_idx(pool, ptr);
320 return ((byte *)pool->state.last[idx] - (byte *)ptr) - pool->state.free[idx];
324 * Open the last memory block (allocated with @mp_alloc() or @mp_end())
325 * for growing and return its size in bytes. The contents and the start pointer
326 * remain unchanged. Do not forget to call @mp_end() to close it.
328 uns mp_open(struct mempool *pool, void *ptr);
331 * Inlined version of mp_open().
333 static inline uns mp_open_fast(struct mempool *pool, void *ptr)
335 pool->idx = mp_idx(pool, ptr);
336 uns size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx];
337 pool->state.free[pool->idx] += size;
342 * Reallocate the last memory block (allocated with @mp_alloc() or @mp_end())
343 * to the new @size. Behavior is similar to @mp_grow(), but the resulting
346 void *mp_realloc(struct mempool *pool, void *ptr, uns size);
349 * The same as @mp_realloc(), but fills the additional bytes (if any) with zeroes.
351 void *mp_realloc_zero(struct mempool *pool, void *ptr, uns size);
354 * Inlined version of mp_realloc().
356 static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uns size)
358 mp_open_fast(pool, ptr);
359 ptr = mp_grow(pool, size);
360 mp_end(pool, (byte *)ptr + size);
366 * Storing and restoring state
367 * ---------------------------
369 * Mempools can remember history of what was allocated and return back
374 * Save the current state of a memory pool.
375 * Do not call this function with an opened growing buffer.
377 static inline void mp_save(struct mempool *pool, struct mempool_state *state)
379 *state = pool->state;
380 pool->state.next = state;
384 * Save the current state to a newly allocated mempool_state structure.
385 * Do not call this function with an opened growing buffer.
387 struct mempool_state *mp_push(struct mempool *pool);
390 * Restore the state saved by @mp_save() or @mp_push() and free all
391 * data allocated after that point (including the state structure itself).
392 * You can't reallocate the last memory block from the saved state.
394 void mp_restore(struct mempool *pool, struct mempool_state *state);
397 * Inlined version of @mp_restore().
399 static inline void mp_restore_fast(struct mempool *pool, struct mempool_state *state)
401 if (pool->state.last[0] != state->last[0] || pool->state.last[1] != state->last[1])
402 mp_restore(pool, state);
405 pool->state = *state;
406 pool->last_big = &pool->last_big;
411 * Restore the state saved by the last call to @mp_push().
412 * @mp_pop() and @mp_push() works as a stack so you can push more states safely.
414 void mp_pop(struct mempool *pool);
423 char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC; /** Makes a copy of a string on a mempool. Returns NULL for NULL string. **/
424 void *mp_memdup(struct mempool *, const void *, uns) LIKE_MALLOC; /** Makes a copy of a memory block on a mempool. **/
426 * Concatenates all passed strings. The last parameter must be NULL.
427 * This will concatenate two strings:
429 * char *message = mp_multicat(pool, "hello ", "world", NULL);
431 char *mp_multicat(struct mempool *, ...) LIKE_MALLOC SENTINEL_CHECK;
433 * Concatenates two strings and stores result on @mp.
435 static inline char *LIKE_MALLOC mp_strcat(struct mempool *mp, const char *x, const char *y)
437 return mp_multicat(mp, x, y, NULL);
440 * Join strings and place @sep between each two neighboring.
441 * @p is the mempool to provide memory, @a is array of strings and @n
442 * tells how many there is of them.
444 char *mp_strjoin(struct mempool *p, char **a, uns n, uns sep) LIKE_MALLOC;
446 * Convert memory block to a string. Makes a copy of the given memory block
447 * in the mempool @p, adding an extra terminating zero byte at the end.
449 char *mp_str_from_mem(struct mempool *p, const void *mem, uns len) LIKE_MALLOC;
459 * printf() into a in-memory string, allocated on the memory pool.
461 char *mp_printf(struct mempool *mp, const char *fmt, ...) FORMAT_CHECK(printf,2,3) LIKE_MALLOC;
463 * Like @mp_printf(), but uses `va_list` for parameters.
465 char *mp_vprintf(struct mempool *mp, const char *fmt, va_list args) LIKE_MALLOC;
467 * Like @mp_printf(), but it appends the data at the end of string
468 * pointed to by @ptr. The string is @mp_open()ed, so you have to
469 * provide something that can be.
471 * Returns pointer to the beginning of the string (the pointer may have
472 * changed due to reallocation).
474 char *mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...) FORMAT_CHECK(printf,3,4);
476 * Like @mp_printf_append(), but uses `va_list` for parameters.
478 char *mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args);