#include <string.h>
#define MP_CHUNK_TAIL ALIGN_TO(sizeof(struct mempool_chunk), CPU_STRUCT_ALIGN)
-#define MP_SIZE_MAX (~0U - MP_CHUNK_TAIL - CPU_PAGE_SIZE)
+#define MP_SIZE_MAX (SIZE_MAX - MP_CHUNK_TAIL - CPU_PAGE_SIZE)
struct mempool_chunk {
#ifdef CONFIG_DEBUG
struct mempool *pool; // Can be useful when analysing coredump for memory leaks
#endif
struct mempool_chunk *next;
- uns size;
+ size_t size;
};
-static uns
-mp_align_size(uns size)
+static size_t
+mp_align_size(size_t size)
{
#ifdef CONFIG_UCW_POOL_IS_MMAP
return ALIGN_TO(size + MP_CHUNK_TAIL, CPU_PAGE_SIZE) - MP_CHUNK_TAIL;
}
void
-mp_init(struct mempool *pool, uns chunk_size)
+mp_init(struct mempool *pool, size_t chunk_size)
{
chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
*pool = (struct mempool) {
}
static void *
-mp_new_big_chunk(struct mempool *pool, uns size)
+mp_new_big_chunk(struct mempool *pool, size_t size)
{
struct mempool_chunk *chunk;
chunk = xmalloc(size + MP_CHUNK_TAIL) + size;
}
static void *
-mp_new_chunk(struct mempool *pool, uns size)
+mp_new_chunk(struct mempool *pool, size_t size)
{
#ifdef CONFIG_UCW_POOL_IS_MMAP
struct mempool_chunk *chunk;
}
struct mempool *
-mp_new(uns chunk_size)
+mp_new(size_t chunk_size)
{
chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
struct mempool_chunk *chunk = mp_new_chunk(NULL, chunk_size);
}
void *
-mp_alloc_internal(struct mempool *pool, uns size)
+mp_alloc_internal(struct mempool *pool, size_t size)
{
struct mempool_chunk *chunk;
if (size <= pool->threshold)
else if (likely(size <= MP_SIZE_MAX))
{
pool->idx = 1;
- uns aligned = ALIGN_TO(size, CPU_STRUCT_ALIGN);
+ size_t aligned = ALIGN_TO(size, CPU_STRUCT_ALIGN);
chunk = mp_new_big_chunk(pool, aligned);
chunk->next = pool->state.last[1];
#ifdef CONFIG_DEBUG
return pool->last_big = (void *)chunk - aligned;
}
else
- die("Cannot allocate %u bytes from a mempool", size);
+ die("Cannot allocate %zu bytes from a mempool", size);
}
void *
-mp_alloc(struct mempool *pool, uns size)
+mp_alloc(struct mempool *pool, size_t size)
{
return mp_alloc_fast(pool, size);
}
void *
-mp_alloc_noalign(struct mempool *pool, uns size)
+mp_alloc_noalign(struct mempool *pool, size_t size)
{
return mp_alloc_fast_noalign(pool, size);
}
void *
-mp_alloc_zero(struct mempool *pool, uns size)
+mp_alloc_zero(struct mempool *pool, size_t size)
{
void *ptr = mp_alloc_fast(pool, size);
bzero(ptr, size);
}
void *
-mp_start_internal(struct mempool *pool, uns size)
+mp_start_internal(struct mempool *pool, size_t size)
{
void *ptr = mp_alloc_internal(pool, size);
pool->state.free[pool->idx] += size;
}
void *
-mp_start(struct mempool *pool, uns size)
+mp_start(struct mempool *pool, size_t size)
{
return mp_start_fast(pool, size);
}
void *
-mp_start_noalign(struct mempool *pool, uns size)
+mp_start_noalign(struct mempool *pool, size_t size)
{
return mp_start_fast_noalign(pool, size);
}
void *
-mp_grow_internal(struct mempool *pool, uns size)
+mp_grow_internal(struct mempool *pool, size_t size)
{
if (unlikely(size > MP_SIZE_MAX))
- die("Cannot allocate %u bytes of memory", size);
- uns avail = mp_avail(pool);
+ die("Cannot allocate %zu bytes of memory", size);
+ size_t avail = mp_avail(pool);
void *ptr = mp_ptr(pool);
if (pool->idx)
{
- uns amortized = likely(avail <= MP_SIZE_MAX / 2) ? avail * 2 : MP_SIZE_MAX;
+ size_t amortized = likely(avail <= MP_SIZE_MAX / 2) ? avail * 2 : MP_SIZE_MAX;
amortized = MAX(amortized, size);
amortized = ALIGN_TO(amortized, CPU_STRUCT_ALIGN);
struct mempool_chunk *chunk = pool->state.last[1], *next = chunk->next;
}
}
-uns
+size_t
mp_open(struct mempool *pool, void *ptr)
{
return mp_open_fast(pool, ptr);
}
void *
-mp_realloc(struct mempool *pool, void *ptr, uns size)
+mp_realloc(struct mempool *pool, void *ptr, size_t size)
{
return mp_realloc_fast(pool, ptr, size);
}
void *
-mp_realloc_zero(struct mempool *pool, void *ptr, uns size)
+mp_realloc_zero(struct mempool *pool, void *ptr, size_t size)
{
- uns old_size = mp_open_fast(pool, ptr);
+ size_t old_size = mp_open_fast(pool, ptr);
ptr = mp_grow(pool, size);
if (size > old_size)
bzero(ptr + old_size, size - old_size);
}
void *
-mp_spread_internal(struct mempool *pool, void *p, uns size)
+mp_spread_internal(struct mempool *pool, void *p, size_t size)
{
void *old = mp_ptr(pool);
void *new = mp_grow_internal(pool, p-old+size);
* You should use this one as an opaque handle only, the insides are internal.
**/
struct mempool_state {
- uns free[2];
+ size_t free[2];
void *last[2];
struct mempool_state *next;
};
struct ucw_allocator allocator;
struct mempool_state state;
void *unused, *last_big;
- uns chunk_size, threshold, idx;
+ size_t chunk_size, threshold;
+ uns idx;
u64 total_size;
};
/**
* 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 <<trans:respools,resources>>, see <<trans:res_mempool()>>.
**/
-void mp_init(struct mempool *pool, uns chunk_size);
+void mp_init(struct mempool *pool, size_t chunk_size);
/**
* Allocate and initialize a new memory pool.
*
* Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
**/
-struct mempool *mp_new(uns chunk_size);
+struct mempool *mp_new(size_t chunk_size);
/**
* Cleanup mempool initialized by mp_init or mp_new.
***/
/* For internal use only, do not call directly */
-void *mp_alloc_internal(struct mempool *pool, uns 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.
* `CPU_STRUCT_ALIGN` bytes and this condition remains true also
* after future reallocations.
**/
-void *mp_alloc(struct mempool *pool, uns 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, uns 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, uns 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, uns size)
+static inline void *mp_alloc_fast(struct mempool *pool, size_t size)
{
- uns 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;
/**
* Inlined version of @mp_alloc_noalign().
**/
-static inline void *mp_alloc_fast_noalign(struct mempool *pool, uns size)
+static inline void *mp_alloc_fast_noalign(struct mempool *pool, size_t size)
{
if (size <= pool->state.free[0])
{
***/
/* For internal use only, do not call directly */
-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);
+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 uns 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, uns size);
-void *mp_start_noalign(struct mempool *pool, uns 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, uns size)
+static inline void *mp_start_fast(struct mempool *pool, size_t size)
{
- uns 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;
/**
* Inlined version of @mp_start_noalign().
**/
-static inline void *mp_start_fast_noalign(struct mempool *pool, uns size)
+static inline void *mp_start_fast_noalign(struct mempool *pool, size_t size)
{
if (size <= pool->state.free[0])
{
* Return the number of bytes available for extending the growing buffer.
* (Before a reallocation will be needed).
**/
-static inline uns mp_avail(struct mempool *pool)
+static inline size_t mp_avail(struct mempool *pool)
{
return pool->state.free[pool->idx];
}
* 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, uns 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);
}
* 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, uns size)
+static inline void *mp_spread(struct mempool *pool, void *p, size_t size)
{
- return (((uns)((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));
}
/**
* 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, uns 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);
/**
* Return size in bytes of the last allocated memory block (with @mp_alloc() or @mp_end()).
**/
-static inline uns mp_size(struct mempool *pool, void *ptr)
+static inline size_t mp_size(struct mempool *pool, void *ptr)
{
uns idx = mp_idx(pool, ptr);
return ((byte *)pool->state.last[idx] - (byte *)ptr) - pool->state.free[idx];
* 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.
**/
-uns mp_open(struct mempool *pool, void *ptr);
+size_t mp_open(struct mempool *pool, void *ptr);
/**
* Inlined version of @mp_open().
**/
-static inline uns 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);
- uns 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;
}
* to the new @size. Behavior is similar to @mp_grow(), but the resulting
* block is closed.
**/
-void *mp_realloc(struct mempool *pool, void *ptr, uns 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, uns 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, uns 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);
***/
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. **/
+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:
* 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;
+char *mp_str_from_mem(struct mempool *p, const void *mem, size_t len) LIKE_MALLOC;
/***