X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fmempool.h;h=ee6db64d6b89783928e267cb3fe0f7d4f97fee91;hb=f6e91541c3d3818541f506d57d253f7d9afbeab3;hp=28ce680af772e9075d8537ec1dc27c8cf9e25069;hpb=c8ffd6e3b4fc8fd6437c04282c357b2dc290bbbd;p=libucw.git diff --git a/ucw/mempool.h b/ucw/mempool.h index 28ce680a..ee6db64d 100644 --- a/ucw/mempool.h +++ b/ucw/mempool.h @@ -35,6 +35,7 @@ #define mp_realloc ucw_mp_realloc #define mp_realloc_zero ucw_mp_realloc_zero #define mp_restore ucw_mp_restore +#define mp_shrink ucw_mp_shrink #define mp_spread_internal ucw_mp_spread_internal #define mp_start ucw_mp_start #define mp_start_internal ucw_mp_start_internal @@ -58,7 +59,7 @@ * You should use this one as an opaque handle only, the insides are internal. **/ struct mempool_state { - uint free[2]; + size_t free[2]; void *last[2]; struct mempool_state *next; }; @@ -71,7 +72,8 @@ struct mempool { struct ucw_allocator allocator; struct mempool_state state; void *unused, *last_big; - uint chunk_size, threshold, idx; + size_t chunk_size, threshold; + uint idx; u64 total_size; }; @@ -90,13 +92,13 @@ struct mempool_stats { /** Mempool statistics. See @mp_stats(). **/ /** * Initialize a given mempool structure. - * @chunk_size must be in the interval `[1, UINT_MAX / 2]`. + * @chunk_size must be in the interval `[1, SIZE_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, uint chunk_size); +void mp_init(struct mempool *pool, size_t chunk_size); /** * Allocate and initialize a new memory pool. @@ -106,7 +108,7 @@ void mp_init(struct mempool *pool, uint chunk_size); * * Memory pools can be treated as <>, see <>. **/ -struct mempool *mp_new(uint chunk_size); +struct mempool *mp_new(size_t chunk_size); /** * Cleanup mempool initialized by mp_init or mp_new. @@ -150,7 +152,7 @@ void mp_shrink(struct mempool *pool, u64 min_total_size); ***/ /* For internal use only, do not call directly */ -void *mp_alloc_internal(struct mempool *pool, uint size) LIKE_MALLOC; +void *mp_alloc_internal(struct mempool *pool, size_t size) LIKE_MALLOC; /** * The function allocates new @size bytes on a given memory pool. @@ -162,24 +164,24 @@ void *mp_alloc_internal(struct mempool *pool, uint size) LIKE_MALLOC; * `CPU_STRUCT_ALIGN` bytes and this condition remains true also * after future reallocations. **/ -void *mp_alloc(struct mempool *pool, uint size); +void *mp_alloc(struct mempool *pool, size_t size); /** * The same as @mp_alloc(), but the result may be unaligned. **/ -void *mp_alloc_noalign(struct mempool *pool, uint size); +void *mp_alloc_noalign(struct mempool *pool, size_t size); /** * The same as @mp_alloc(), but fills the newly allocated memory with zeroes. **/ -void *mp_alloc_zero(struct mempool *pool, uint size); +void *mp_alloc_zero(struct mempool *pool, size_t size); /** * Inlined version of @mp_alloc(). **/ -static inline void *mp_alloc_fast(struct mempool *pool, uint size) +static inline void *mp_alloc_fast(struct mempool *pool, size_t size) { - uint avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1); + size_t avail = pool->state.free[0] & ~(size_t)(CPU_STRUCT_ALIGN - 1); if (size <= avail) { pool->state.free[0] = avail - size; @@ -192,7 +194,7 @@ static inline void *mp_alloc_fast(struct mempool *pool, uint size) /** * Inlined version of @mp_alloc_noalign(). **/ -static inline void *mp_alloc_fast_noalign(struct mempool *pool, uint size) +static inline void *mp_alloc_fast_noalign(struct mempool *pool, size_t size) { if (size <= pool->state.free[0]) { @@ -225,9 +227,9 @@ static inline struct ucw_allocator *mp_get_allocator(struct mempool *mp) ***/ /* For internal use only, do not call directly */ -void *mp_start_internal(struct mempool *pool, uint size) LIKE_MALLOC; -void *mp_grow_internal(struct mempool *pool, uint size); -void *mp_spread_internal(struct mempool *pool, void *p, uint size); +void *mp_start_internal(struct mempool *pool, size_t size) LIKE_MALLOC; +void *mp_grow_internal(struct mempool *pool, size_t size); +void *mp_spread_internal(struct mempool *pool, void *p, size_t size); static inline uint mp_idx(struct mempool *pool, void *ptr) { @@ -247,15 +249,15 @@ static inline uint mp_idx(struct mempool *pool, void *ptr) * Keep in mind that you can't make any other pool allocations * before you "close" the growing buffer with @mp_end(). */ -void *mp_start(struct mempool *pool, uint size); -void *mp_start_noalign(struct mempool *pool, uint size); +void *mp_start(struct mempool *pool, size_t size); +void *mp_start_noalign(struct mempool *pool, size_t size); /** * Inlined version of @mp_start(). **/ -static inline void *mp_start_fast(struct mempool *pool, uint size) +static inline void *mp_start_fast(struct mempool *pool, size_t size) { - uint avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1); + size_t avail = pool->state.free[0] & ~(size_t)(CPU_STRUCT_ALIGN - 1); if (size <= avail) { pool->idx = 0; @@ -269,7 +271,7 @@ static inline void *mp_start_fast(struct mempool *pool, uint size) /** * Inlined version of @mp_start_noalign(). **/ -static inline void *mp_start_fast_noalign(struct mempool *pool, uint size) +static inline void *mp_start_fast_noalign(struct mempool *pool, size_t size) { if (size <= pool->state.free[0]) { @@ -292,7 +294,7 @@ static inline void *mp_ptr(struct mempool *pool) * Return the number of bytes available for extending the growing buffer. * (Before a reallocation will be needed). **/ -static inline uint mp_avail(struct mempool *pool) +static inline size_t mp_avail(struct mempool *pool) { return pool->state.free[pool->idx]; } @@ -303,7 +305,7 @@ static inline uint mp_avail(struct mempool *pool) * change its starting position. The content will be unchanged to the minimum * of the old and new sizes; newly allocated memory will be uninitialized. * Multiple calls to mp_grow() have amortized linear cost wrt. the maximum value of @size. */ -static inline void *mp_grow(struct mempool *pool, uint size) +static inline void *mp_grow(struct mempool *pool, size_t size) { return (size <= mp_avail(pool)) ? mp_ptr(pool) : mp_grow_internal(pool, size); } @@ -320,9 +322,9 @@ static inline void *mp_expand(struct mempool *pool) * Ensure that there is at least @size bytes free after @p, * if not, reallocate and adjust @p. **/ -static inline void *mp_spread(struct mempool *pool, void *p, uint size) +static inline void *mp_spread(struct mempool *pool, void *p, size_t size) { - return (((uint)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size)); + return (((size_t)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size)); } /** @@ -342,7 +344,7 @@ static inline char *mp_append_char(struct mempool *pool, char *p, uint c) * the last byte in the buffer, returns a pointer after the last byte * of the new (possibly reallocated) buffer. **/ -static inline void *mp_append_block(struct mempool *pool, void *p, const void *block, uint size) +static inline void *mp_append_block(struct mempool *pool, void *p, const void *block, size_t size) { char *q = mp_spread(pool, p, size); memcpy(q, block, size); @@ -383,7 +385,7 @@ static inline char *mp_end_string(struct mempool *pool, void *end) /** * Return size in bytes of the last allocated memory block (with @mp_alloc() or @mp_end()). **/ -static inline uint mp_size(struct mempool *pool, void *ptr) +static inline size_t mp_size(struct mempool *pool, void *ptr) { uint idx = mp_idx(pool, ptr); return ((byte *)pool->state.last[idx] - (byte *)ptr) - pool->state.free[idx]; @@ -394,15 +396,15 @@ static inline uint mp_size(struct mempool *pool, void *ptr) * for growing and return its size in bytes. The contents and the start pointer * remain unchanged. Do not forget to call @mp_end() to close it. **/ -uint mp_open(struct mempool *pool, void *ptr); +size_t mp_open(struct mempool *pool, void *ptr); /** * Inlined version of @mp_open(). **/ -static inline uint mp_open_fast(struct mempool *pool, void *ptr) +static inline size_t mp_open_fast(struct mempool *pool, void *ptr) { pool->idx = mp_idx(pool, ptr); - uint size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx]; + size_t size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx]; pool->state.free[pool->idx] += size; return size; } @@ -412,17 +414,17 @@ static inline uint mp_open_fast(struct mempool *pool, void *ptr) * to the new @size. Behavior is similar to @mp_grow(), but the resulting * block is closed. **/ -void *mp_realloc(struct mempool *pool, void *ptr, uint size); +void *mp_realloc(struct mempool *pool, void *ptr, size_t size); /** * The same as @mp_realloc(), but fills the additional bytes (if any) with zeroes. **/ -void *mp_realloc_zero(struct mempool *pool, void *ptr, uint size); +void *mp_realloc_zero(struct mempool *pool, void *ptr, size_t size); /** * Inlined version of @mp_realloc(). **/ -static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uint size) +static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, size_t size) { mp_open_fast(pool, ptr); ptr = mp_grow(pool, size); @@ -490,7 +492,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. Returns NULL for NULL string. **/ -void *mp_memdup(struct mempool *, const void *, uint) LIKE_MALLOC; /** Makes a copy of a memory block on a mempool. **/ +void *mp_memdup(struct mempool *, const void *, size_t) LIKE_MALLOC; /** Makes a copy of a memory block on a mempool. **/ /** * Concatenates all passed strings. The last parameter must be NULL. * This will concatenate two strings: @@ -515,7 +517,7 @@ char *mp_strjoin(struct mempool *p, char **a, uint n, uint 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, uint len) LIKE_MALLOC; +char *mp_str_from_mem(struct mempool *p, const void *mem, size_t len) LIKE_MALLOC; /***