Skip to content
Snippets Groups Projects
Commit 2c5a7a3d authored by Marek Vavrusa's avatar Marek Vavrusa Committed by Gerrit Code Review
Browse files

Imported mempool from libucw.

Mempool is a type of memory allocator that allows to
free all allocated elements at once.
Removed unnecessary string and state operations.
Licensed under LGPL, same as lists and heap.

Change-Id: I8b8f84f41dcc321f37e51894b5c121f463c4f111
parent e2e2152f
Branches
Tags
No related merge requests found
......@@ -71,6 +71,8 @@ src/common/log.c
src/common/log.h
src/common/mempattern.c
src/common/mempattern.h
src/common/mempool.c
src/common/mempool.h
src/common/print.c
src/common/print.h
src/common/prng.c
......
......@@ -259,6 +259,8 @@ libknots_la_SOURCES = \
common/getline.c \
common/log.c \
common/log.h \
common/mempool.c \
common/mempool.h \
common/hattrie/ahtable.c \
common/hattrie/ahtable.h \
common/hattrie/hat-trie.c \
......
/*
* UCW Library -- Memory Pools (One-Time Allocation)
*
* (c) 1997--2001 Martin Mares <mj@ucw.cz>
* (c) 2007 Pavel Charvat <pchar@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
*/
#undef LOCAL_DEBUG
#include "mempool.h"
#include "common/mempattern.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
/** \todo This shouldn't be precalculated, but computed on load. */
#define CPU_PAGE_SIZE 4096
/** Align an integer @s to the nearest higher multiple of @a (which should be a power of two) **/
#define ALIGN_TO(s, a) (((s)+a-1)&~(a-1))
#define MP_CHUNK_TAIL ALIGN_TO(sizeof(struct mempool_chunk), CPU_STRUCT_ALIGN)
#define MP_SIZE_MAX (~0U - MP_CHUNK_TAIL - CPU_PAGE_SIZE)
#ifndef MAX
#define MAX(a, b) \
({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
#endif
#define DBG(s...)
struct mempool_chunk {
struct mempool_chunk *next;
unsigned size;
};
static unsigned
mp_align_size(unsigned size)
{
#ifdef CONFIG_UCW_POOL_IS_MMAP
return ALIGN_TO(size + MP_CHUNK_TAIL, CPU_PAGE_SIZE) - MP_CHUNK_TAIL;
#else
return ALIGN_TO(size, CPU_STRUCT_ALIGN);
#endif
}
void
mp_init(struct mempool *pool, unsigned chunk_size)
{
chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
*pool = (struct mempool) {
.chunk_size = chunk_size,
.threshold = chunk_size >> 1,
.last_big = &pool->last_big };
}
static void *
mp_new_big_chunk(unsigned size)
{
struct mempool_chunk *chunk;
chunk = xmalloc(size + MP_CHUNK_TAIL) + size;
chunk->size = size;
return chunk;
}
static void
mp_free_big_chunk(struct mempool_chunk *chunk)
{
free((void *)chunk - chunk->size);
}
static void *
mp_new_chunk(unsigned size)
{
#ifdef CONFIG_UCW_POOL_IS_MMAP
struct mempool_chunk *chunk;
chunk = page_alloc(size + MP_CHUNK_TAIL) + size;
chunk->size = size;
return chunk;
#else
return mp_new_big_chunk(size);
#endif
}
static void
mp_free_chunk(struct mempool_chunk *chunk)
{
#ifdef CONFIG_UCW_POOL_IS_MMAP
page_free((void *)chunk - chunk->size, chunk->size + MP_CHUNK_TAIL);
#else
mp_free_big_chunk(chunk);
#endif
}
struct mempool *
mp_new(unsigned chunk_size)
{
chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
struct mempool_chunk *chunk = mp_new_chunk(chunk_size);
struct mempool *pool = (void *)chunk - chunk_size;
DBG("Creating mempool %p with %u bytes long chunks", pool, chunk_size);
chunk->next = NULL;
*pool = (struct mempool) {
.state = { .free = { chunk_size - sizeof(*pool) }, .last = { chunk } },
.chunk_size = chunk_size,
.threshold = chunk_size >> 1,
.last_big = &pool->last_big };
return pool;
}
static void
mp_free_chain(struct mempool_chunk *chunk)
{
while (chunk)
{
struct mempool_chunk *next = chunk->next;
mp_free_chunk(chunk);
chunk = next;
}
}
static void
mp_free_big_chain(struct mempool_chunk *chunk)
{
while (chunk)
{
struct mempool_chunk *next = chunk->next;
mp_free_big_chunk(chunk);
chunk = next;
}
}
void
mp_delete(struct mempool *pool)
{
DBG("Deleting mempool %p", pool);
mp_free_big_chain(pool->state.last[1]);
mp_free_chain(pool->unused);
mp_free_chain(pool->state.last[0]); // can contain the mempool structure
}
void
mp_flush(struct mempool *pool)
{
mp_free_big_chain(pool->state.last[1]);
struct mempool_chunk *chunk, *next;
for (chunk = pool->state.last[0]; chunk && (void *)chunk - chunk->size != pool; chunk = next)
{
next = chunk->next;
chunk->next = pool->unused;
pool->unused = chunk;
}
pool->state.last[0] = chunk;
pool->state.free[0] = chunk ? chunk->size - sizeof(*pool) : 0;
pool->state.last[1] = NULL;
pool->state.free[1] = 0;
pool->state.next = NULL;
pool->last_big = &pool->last_big;
}
static void
mp_stats_chain(struct mempool_chunk *chunk, struct mempool_stats *stats, unsigned idx)
{
while (chunk)
{
stats->chain_size[idx] += chunk->size + sizeof(*chunk);
stats->chain_count[idx]++;
chunk = chunk->next;
}
stats->total_size += stats->chain_size[idx];
}
void
mp_stats(struct mempool *pool, struct mempool_stats *stats)
{
bzero(stats, sizeof(*stats));
mp_stats_chain(pool->state.last[0], stats, 0);
mp_stats_chain(pool->state.last[1], stats, 1);
mp_stats_chain(pool->unused, stats, 2);
}
uint64_t
mp_total_size(struct mempool *pool)
{
struct mempool_stats stats;
mp_stats(pool, &stats);
return stats.total_size;
}
void *
mp_alloc_internal(struct mempool *pool, unsigned size)
{
struct mempool_chunk *chunk;
if (size <= pool->threshold)
{
pool->idx = 0;
if (pool->unused)
{
chunk = pool->unused;
pool->unused = chunk->next;
}
else
chunk = mp_new_chunk(pool->chunk_size);
chunk->next = pool->state.last[0];
pool->state.last[0] = chunk;
pool->state.free[0] = pool->chunk_size - size;
return (void *)chunk - pool->chunk_size;
}
else if (size <= MP_SIZE_MAX)
{
pool->idx = 1;
unsigned aligned = ALIGN_TO(size, CPU_STRUCT_ALIGN);
chunk = mp_new_big_chunk(aligned);
chunk->next = pool->state.last[1];
pool->state.last[1] = chunk;
pool->state.free[1] = aligned - size;
return pool->last_big = (void *)chunk - aligned;
}
else {
fprintf(stderr, "Cannot allocate %u bytes from a mempool", size);
assert(0);
}
}
void *
mp_alloc(struct mempool *pool, unsigned size)
{
return mp_alloc_fast(pool, size);
}
void *
mp_alloc_noalign(struct mempool *pool, unsigned size)
{
return mp_alloc_fast_noalign(pool, size);
}
void *
mp_alloc_zero(struct mempool *pool, unsigned size)
{
void *ptr = mp_alloc_fast(pool, size);
bzero(ptr, size);
return ptr;
}
void *
mp_start_internal(struct mempool *pool, unsigned size)
{
void *ptr = mp_alloc_internal(pool, size);
pool->state.free[pool->idx] += size;
return ptr;
}
void *
mp_start(struct mempool *pool, unsigned size)
{
return mp_start_fast(pool, size);
}
void *
mp_start_noalign(struct mempool *pool, unsigned size)
{
return mp_start_fast_noalign(pool, size);
}
void *
mp_grow_internal(struct mempool *pool, unsigned size)
{
if (size > MP_SIZE_MAX) {
fprintf(stderr, "Cannot allocate %u bytes of memory", size);
assert(0);
}
unsigned avail = mp_avail(pool);
void *ptr = mp_ptr(pool);
if (pool->idx)
{
unsigned amortized = (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;
ptr = xrealloc(ptr, amortized + MP_CHUNK_TAIL);
chunk = ptr + amortized;
chunk->next = next;
chunk->size = amortized;
pool->state.last[1] = chunk;
pool->state.free[1] = amortized;
pool->last_big = ptr;
return ptr;
}
else
{
void *p = mp_start_internal(pool, size);
memcpy(p, ptr, avail);
return p;
}
}
unsigned
mp_open(struct mempool *pool, void *ptr)
{
return mp_open_fast(pool, ptr);
}
void *
mp_realloc(struct mempool *pool, void *ptr, unsigned size)
{
return mp_realloc_fast(pool, ptr, size);
}
void *
mp_realloc_zero(struct mempool *pool, void *ptr, unsigned size)
{
unsigned old_size = mp_open_fast(pool, ptr);
ptr = mp_grow(pool, size);
if (size > old_size)
bzero(ptr + old_size, size - old_size);
mp_end(pool, ptr + size);
return ptr;
}
void *
mp_spread_internal(struct mempool *pool, void *p, unsigned size)
{
void *old = mp_ptr(pool);
void *new = mp_grow_internal(pool, p-old+size);
return p-old+new;
}
/*
* UCW Library -- Memory Pools
*
* (c) 1997--2005 Martin Mares <mj@ucw.cz>
* (c) 2007 Pavel Charvat <pchar@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
*/
#ifndef _UCW_POOLS_H
#define _UCW_POOLS_H
#include <string.h>
#include <stdint.h>
#include "config.h"
#if __GNUC__ >= 4
#define LIKE_MALLOC __attribute__((malloc)) /** Function returns a "new" pointer **/
#define SENTINEL_CHECK __attribute__((sentinel)) /** The last argument must be NULL **/
#else
#define LIKE_MALLOC
#define SENTINEL_CHECK
#endif
#define CPU_STRUCT_ALIGN (sizeof(void*))
/***
* [[defs]]
* Definitions
* -----------
***/
/**
* Memory pool state (see @mp_push(), ...).
* You should use this one as an opaque handle only, the insides are internal.
**/
struct mempool_state {
unsigned free[2];
void *last[2];
struct mempool_state *next;
};
/**
* Memory pool.
* You should use this one as an opaque handle only, the insides are internal.
**/
struct mempool {
struct mempool_state state;
void *unused, *last_big;
unsigned chunk_size, threshold, idx;
};
struct mempool_stats { /** Mempool statistics. See @mp_stats(). **/
uint64_t total_size; /* Real allocated size in bytes */
unsigned chain_count[3]; /* Number of allocated chunks in small/big/unused chains */
unsigned chain_size[3]; /* Size of allocated chunks in small/big/unused chains */
};
/***
* [[basic]]
* Basic manipulation
* ------------------
***/
/**
* Initialize a given mempool structure.
* @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 <<trans:respools,resources>>, see <<trans:res_mempool()>>.
**/
void mp_init(struct mempool *pool, unsigned chunk_size);
/**
* Allocate and initialize a new memory pool.
* See @mp_init() for @chunk_size limitations.
*
* The new mempool structure is allocated on the new mempool.
*
* Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
**/
struct mempool *mp_new(unsigned chunk_size);
/**
* Cleanup mempool initialized by mp_init or mp_new.
* Frees all the memory allocated by this mempool and,
* if created by @mp_new(), the @pool itself.
**/
void mp_delete(struct mempool *pool);
/**
* Frees all data on a memory pool, but leaves it working.
* It can keep some of the chunks allocated to serve
* further allocation requests. Leaves the @pool alive,
* even if it was created with @mp_new().
**/
void mp_flush(struct mempool *pool);
/**
* Compute some statistics for debug purposes.
* See the definition of the <<struct_mempool_stats,mempool_stats structure>>.
**/
void mp_stats(struct mempool *pool, struct mempool_stats *stats);
uint64_t mp_total_size(struct mempool *pool); /** How many bytes were allocated by the pool. **/
/***
* [[alloc]]
* Allocation routines
* -------------------
***/
/* For internal use only, do not call directly */
void *mp_alloc_internal(struct mempool *pool, unsigned size) LIKE_MALLOC;
/**
* The function allocates new @size bytes on a given memory pool.
* If the @size is zero, the resulting pointer is undefined,
* but it may be safely reallocated or used as the parameter
* to other functions below.
*
* The resulting pointer is always aligned to a multiple of
* `CPU_STRUCT_ALIGN` bytes and this condition remains true also
* after future reallocations.
**/
void *mp_alloc(struct mempool *pool, unsigned size);
/**
* The same as @mp_alloc(), but the result may be unaligned.
**/
void *mp_alloc_noalign(struct mempool *pool, unsigned size);
/**
* The same as @mp_alloc(), but fills the newly allocated memory with zeroes.
**/
void *mp_alloc_zero(struct mempool *pool, unsigned size);
/**
* Inlined version of @mp_alloc().
**/
static inline void *mp_alloc_fast(struct mempool *pool, unsigned size)
{
unsigned avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
if (size <= avail)
{
pool->state.free[0] = avail - size;
return (uint8_t*)pool->state.last[0] - avail;
}
else
return mp_alloc_internal(pool, size);
}
/**
* Inlined version of @mp_alloc_noalign().
**/
static inline void *mp_alloc_fast_noalign(struct mempool *pool, unsigned size)
{
if (size <= pool->state.free[0])
{
void *ptr = (uint8_t*)pool->state.last[0] - pool->state.free[0];
pool->state.free[0] -= size;
return ptr;
}
else
return mp_alloc_internal(pool, size);
}
/***
* [[gbuf]]
* Growing buffers
* ---------------
*
* 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 <<growbuf:,growing buffes>> module.
***/
/* For internal use only, do not call directly */
void *mp_start_internal(struct mempool *pool, unsigned size) LIKE_MALLOC;
void *mp_grow_internal(struct mempool *pool, unsigned size);
void *mp_spread_internal(struct mempool *pool, void *p, unsigned size);
static inline unsigned
mp_idx(struct mempool *pool, void *ptr)
{
return ptr == pool->last_big;
}
/**
* Open a new growing buffer (at least @size bytes long).
* If the @size is zero, the resulting pointer is undefined,
* but it may be safely reallocated or used as the parameter
* to other functions below.
*
* The resulting pointer is always aligned to a multiple of
* `CPU_STRUCT_ALIGN` bytes and this condition remains true also
* after future reallocations. There is an unaligned version as well.
*
* 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, unsigned size);
void *mp_start_noalign(struct mempool *pool, unsigned size);
/**
* Inlined version of @mp_start().
**/
static inline void *mp_start_fast(struct mempool *pool, unsigned size)
{
unsigned avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
if (size <= avail)
{
pool->idx = 0;
pool->state.free[0] = avail;
return (uint8_t*)pool->state.last[0] - avail;
}
else
return mp_start_internal(pool, size);
}
/**
* Inlined version of @mp_start_noalign().
**/
static inline void *mp_start_fast_noalign(struct mempool *pool, unsigned size)
{
if (size <= pool->state.free[0])
{
pool->idx = 0;
return (uint8_t*)pool->state.last[0] - pool->state.free[0];
}
else
return mp_start_internal(pool, size);
}
/**
* Return start pointer of the growing buffer allocated by latest @mp_start() or a similar function.
**/
static inline void *mp_ptr(struct mempool *pool)
{
return (uint8_t*)pool->state.last[pool->idx] - pool->state.free[pool->idx];
}
/**
* Return the number of bytes available for extending the growing buffer.
* (Before a reallocation will be needed).
**/
static inline unsigned mp_avail(struct mempool *pool)
{
return pool->state.free[pool->idx];
}
/**
* Grow the buffer allocated by @mp_start() to be at least @size bytes long
* (@size may be less than @mp_avail(), even zero). Reallocated buffer may
* 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, unsigned size)
{
return (size <= mp_avail(pool)) ? mp_ptr(pool) : mp_grow_internal(pool, size);
}
/**
* Grow the buffer by at least one uint8_t-- equivalent to <<mp_grow(),`mp_grow`>>`(@pool, @mp_avail(pool) + 1)`.
**/
static inline void *mp_expand(struct mempool *pool)
{
return mp_grow_internal(pool, mp_avail(pool) + 1);
}
/**
* 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, unsigned size)
{
return (((unsigned)((uint8_t*)pool->state.last[pool->idx] - (uint8_t*)p) >= size) ? p : mp_spread_internal(pool, p, size));
}
/**
* Close the growing buffer. The @end must point just behind the data, you want to keep
* allocated (so it can be in the interval `[@mp_ptr(@pool), @mp_ptr(@pool) + @mp_avail(@pool)]`).
* Returns a pointer to the beginning of the just closed block.
**/
static inline void *mp_end(struct mempool *pool, void *end)
{
void *p = mp_ptr(pool);
pool->state.free[pool->idx] = (uint8_t*)pool->state.last[pool->idx] - (uint8_t*)end;
return p;
}
/**
* Return size in bytes of the last allocated memory block (with @mp_alloc() or @mp_end()).
**/
static inline unsigned mp_size(struct mempool *pool, void *ptr)
{
unsigned idx = mp_idx(pool, ptr);
return ((uint8_t*)pool->state.last[idx] - (uint8_t*)ptr) - pool->state.free[idx];
}
/**
* Open the last memory block (allocated with @mp_alloc() or @mp_end())
* 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.
**/
unsigned mp_open(struct mempool *pool, void *ptr);
/**
* Inlined version of mp_open().
**/
static inline unsigned mp_open_fast(struct mempool *pool, void *ptr)
{
pool->idx = mp_idx(pool, ptr);
unsigned size = ((uint8_t*)pool->state.last[pool->idx] - (uint8_t*)ptr) - pool->state.free[pool->idx];
pool->state.free[pool->idx] += size;
return size;
}
/**
* Reallocate the last memory block (allocated with @mp_alloc() or @mp_end())
* to the new @size. Behavior is similar to @mp_grow(), but the resulting
* block is closed.
**/
void *mp_realloc(struct mempool *pool, void *ptr, unsigned size);
/**
* The same as @mp_realloc(), but fills the additional bytes (if any) with zeroes.
**/
void *mp_realloc_zero(struct mempool *pool, void *ptr, unsigned size);
/**
* Inlined version of mp_realloc().
**/
static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, unsigned size)
{
mp_open_fast(pool, ptr);
ptr = mp_grow(pool, size);
mp_end(pool, (uint8_t*)ptr + size);
return ptr;
}
#endif
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment