X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fmempool.h;h=f5aff6f56ae2b6bfb479e11ded6e2bb1528fe4c6;hb=0b7df598070533b746e6162e53f90af764bd8f83;hp=f566b79cd2a5fb1e4cf3e285b23af81b01ba8532;hpb=b35de5e2fa2dd2bee944eee56d585a9b6d8dbe6d;p=libucw.git diff --git a/ucw/mempool.h b/ucw/mempool.h index f566b79c..f5aff6f5 100644 --- a/ucw/mempool.h +++ b/ucw/mempool.h @@ -11,6 +11,39 @@ #ifndef _UCW_POOLS_H #define _UCW_POOLS_H +#ifdef CONFIG_UCW_CLEAN_ABI +#define mp_alloc ucw_mp_alloc +#define mp_alloc_internal ucw_mp_alloc_internal +#define mp_alloc_noalign ucw_mp_alloc_noalign +#define mp_alloc_zero ucw_mp_alloc_zero +#define mp_delete ucw_mp_delete +#define mp_flush ucw_mp_flush +#define mp_grow_internal ucw_mp_grow_internal +#define mp_init ucw_mp_init +#define mp_memdup ucw_mp_memdup +#define mp_multicat ucw_mp_multicat +#define mp_new ucw_mp_new +#define mp_open ucw_mp_open +#define mp_pop ucw_mp_pop +#define mp_printf ucw_mp_printf +#define mp_printf_append ucw_mp_printf_append +#define mp_push ucw_mp_push +#define mp_realloc ucw_mp_realloc +#define mp_realloc_zero ucw_mp_realloc_zero +#define mp_restore ucw_mp_restore +#define mp_spread_internal ucw_mp_spread_internal +#define mp_start ucw_mp_start +#define mp_start_internal ucw_mp_start_internal +#define mp_start_noalign ucw_mp_start_noalign +#define mp_stats ucw_mp_stats +#define mp_str_from_mem ucw_mp_str_from_mem +#define mp_strdup ucw_mp_strdup +#define mp_strjoin ucw_mp_strjoin +#define mp_total_size ucw_mp_total_size +#define mp_vprintf ucw_mp_vprintf +#define mp_vprintf_append ucw_mp_vprintf_append +#endif + /*** * [[defs]] * Definitions @@ -54,6 +87,8 @@ struct mempool_stats { /** Mempool statistics. See @mp_stats(). **/ * @chunk_size must be in the interval `[1, UINT_MAX / 2]`. * It will allocate memory by this large chunks and take * memory to satisfy requests from them. + * + * Memory pools can be treated as <>, see <>. **/ void mp_init(struct mempool *pool, uns chunk_size); @@ -62,6 +97,8 @@ void mp_init(struct mempool *pool, uns chunk_size); * See @mp_init() for @chunk_size limitations. * * The new mempool structure is allocated on the new mempool. + * + * Memory pools can be treated as <>, see <>. **/ struct mempool *mp_new(uns chunk_size); @@ -128,7 +165,7 @@ static inline void *mp_alloc_fast(struct mempool *pool, uns size) if (size <= avail) { pool->state.free[0] = avail - size; - return pool->state.last[0] - avail; + return (byte *)pool->state.last[0] - avail; } else return mp_alloc_internal(pool, size); @@ -141,7 +178,7 @@ static inline void *mp_alloc_fast_noalign(struct mempool *pool, uns size) { if (size <= pool->state.free[0]) { - void *ptr = pool->state.last[0] - pool->state.free[0]; + void *ptr = (byte *)pool->state.last[0] - pool->state.free[0]; pool->state.free[0] -= size; return ptr; } @@ -157,6 +194,8 @@ static inline void *mp_alloc_fast_noalign(struct mempool *pool, uns size) * You do not need to know, how a buffer will need to be large, * you can grow it incrementally to needed size. You can grow only * one buffer at a time on a given mempool. + * + * Similar functionality is provided by <> module. ***/ /* For internal use only, do not call directly */ @@ -164,8 +203,7 @@ void *mp_start_internal(struct mempool *pool, uns size) LIKE_MALLOC; void *mp_grow_internal(struct mempool *pool, uns size); void *mp_spread_internal(struct mempool *pool, void *p, uns size); -static inline uns -mp_idx(struct mempool *pool, void *ptr) +static inline uns mp_idx(struct mempool *pool, void *ptr) { return ptr == pool->last_big; } @@ -196,7 +234,7 @@ static inline void *mp_start_fast(struct mempool *pool, uns size) { pool->idx = 0; pool->state.free[0] = avail; - return pool->state.last[0] - avail; + return (byte *)pool->state.last[0] - avail; } else return mp_start_internal(pool, size); @@ -210,7 +248,7 @@ static inline void *mp_start_fast_noalign(struct mempool *pool, uns size) if (size <= pool->state.free[0]) { pool->idx = 0; - return pool->state.last[0] - pool->state.free[0]; + return (byte *)pool->state.last[0] - pool->state.free[0]; } else return mp_start_internal(pool, size); @@ -221,7 +259,7 @@ static inline void *mp_start_fast_noalign(struct mempool *pool, uns size) **/ static inline void *mp_ptr(struct mempool *pool) { - return pool->state.last[pool->idx] - pool->state.free[pool->idx]; + return (byte *)pool->state.last[pool->idx] - pool->state.free[pool->idx]; } /** @@ -258,7 +296,7 @@ static inline void *mp_expand(struct mempool *pool) **/ static inline void *mp_spread(struct mempool *pool, void *p, uns size) { - return (((uns)(pool->state.last[pool->idx] - p) >= size) ? p : mp_spread_internal(pool, p, size)); + return (((uns)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size)); } /** @@ -269,7 +307,7 @@ static inline void *mp_spread(struct mempool *pool, void *p, uns size) static inline void *mp_end(struct mempool *pool, void *end) { void *p = mp_ptr(pool); - pool->state.free[pool->idx] = pool->state.last[pool->idx] - end; + pool->state.free[pool->idx] = (byte *)pool->state.last[pool->idx] - (byte *)end; return p; } @@ -279,7 +317,7 @@ static inline void *mp_end(struct mempool *pool, void *end) static inline uns mp_size(struct mempool *pool, void *ptr) { uns idx = mp_idx(pool, ptr); - return pool->state.last[idx] - ptr - pool->state.free[idx]; + return ((byte *)pool->state.last[idx] - (byte *)ptr) - pool->state.free[idx]; } /** @@ -295,7 +333,7 @@ uns mp_open(struct mempool *pool, void *ptr); static inline uns mp_open_fast(struct mempool *pool, void *ptr) { pool->idx = mp_idx(pool, ptr); - uns size = pool->state.last[pool->idx] - ptr - pool->state.free[pool->idx]; + uns size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx]; pool->state.free[pool->idx] += size; return size; } @@ -319,7 +357,7 @@ static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uns size) { mp_open_fast(pool, ptr); ptr = mp_grow(pool, size); - mp_end(pool, ptr + size); + mp_end(pool, (byte *)ptr + size); return ptr; } @@ -334,7 +372,7 @@ static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uns size) /** * Save the current state of a memory pool. - * Do not call this function with an opened growing buffer. + * Do not call this function with an opened growing buffer. **/ static inline void mp_save(struct mempool *pool, struct mempool_state *state) { @@ -382,7 +420,7 @@ void mp_pop(struct mempool *pool); * ----------------- ***/ -char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC; /** Makes a copy of a string on a mempool. **/ +char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC; /** Makes a copy of a string on a mempool. Returns NULL for NULL string. **/ void *mp_memdup(struct mempool *, const void *, uns) LIKE_MALLOC; /** Makes a copy of a memory block on a mempool. **/ /** * Concatenates all passed strings. The last parameter must be NULL. @@ -404,6 +442,11 @@ static inline char *LIKE_MALLOC mp_strcat(struct mempool *mp, const char *x, con * tells how many there is of them. **/ char *mp_strjoin(struct mempool *p, char **a, uns n, uns sep) LIKE_MALLOC; +/** + * Convert memory block to a string. Makes a copy of the given memory block + * in the mempool @p, adding an extra terminating zero byte at the end. + **/ +char *mp_str_from_mem(struct mempool *p, const void *mem, uns len) LIKE_MALLOC; /***