Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

readme.md

pool.h

Header ../../src/pool.h depends on ../../src/heap.h and ../../src/array.h; examples ../../test/test_pool.c; article ../pool/pool.pdf.

Stable pool

Example of Pool

<t>pool is a memory pool that stores only one size—<pT>type—using slab allocation. As Bonwick, 1994, Slab, it helps reduce internal fragmentation from repeated allocation and destruction by caching contiguous blocks. Pointers to valid items in the pool are stable. Instead of freeing memory, a free-heap in the active-slab allows lazily reusing the same space. If removal is ongoing and uniformly sampled while reaching a steady-state size, it will eventually settle in one contiguous region.

  • Parameter: POOL_NAME, POOL_TYPE
    <t> that satisfies C naming conventions when mangled and a valid tag type, <pT>type, associated therewith; required.
  • Parameter: POOL_DECLARE_ONLY
    For headers in different compilation units.
  • Standard:
    C89. However, when compiling for segmented memory models, C99 with uintptr_t is recommended because of it's implementation-defined instead of undefined-behaviour when comparing pointers from different objects in the heap of memory addresses. Still, this is not guaranteed to produce meaningful results on all systems.
  • Dependancies:
    array, heap, box

typedef POOL_TYPE <pT>type;

A valid tag type set by POOL_TYPE.

typedef void(*<pT>to_string_fn)(const <pT>type , char()[12]);

Type of the required <tr>to_string. Responsible for turning the read-only argument into a 12-max-char output string.

struct <t>pool { struct <pT>slot_array slots; struct poolfree_heap free0; size_t capacity0; };

A zeroed pool is a valid state. See <t>pool.

States.

struct table_stats { size_t n, max; double mean, ssdm; };

Welford1962Note: population variance: ssdm/n, sample variance: ssdm/(n-1).

ModifiersFunction NameArgument List
struct <T>cursor<T>begin<t>pool
int<T>exists<T>cursor
<pT>type *<T>entry<T>cursor
void<T>next<T>cursor
struct <t>pool<t>pool
void<t>pool_<t>pool
int<T>buffer<t>pool, size_t
<pT>type *<T>new<t>pool
int<T>remove<t>pool, <pT>type
void<T>clear<t>pool
static struct <t>pool<t>pool
static void<t>pool_pool
static int<T>bufferpool, n
static <pT>type *<T>newpool
static int<T>removepool, data
static void<T>clearpool
void<T>graph<pT>box
int<T>graph_fn<pT>box, char

struct <T>cursor <T>begin(const struct <t>pool *);

int <T>exists(const struct <T>cursor *);

<pT>type *<T>entry(struct <T>cursor *);

void <T>next(struct <T>cursor *);

struct <t>pool <t>pool(void);

void <t>pool_(struct <t>pool *);

int <T>buffer(struct <t>pool *, size_t);

<pT>type *<T>new(struct <t>pool *);

int <T>remove(struct <t>pool *, <pT>type *);

void <T>clear(struct <t>pool *);

static struct <t>pool <t>pool(void)

  • Return:
    An idle pool is zeroed.
  • Order:
    Θ(1)

static void <t>pool_(struct <t>pool *const pool)

Destroys pool and returns it to idle.

  • Order:
    Ο(log data)

static int <T>buffer(struct <t>pool *const pool, const size_t n)

Ensure capacity of at least n further items in pool. Pre-sizing is better for contiguous blocks, but takes up that memory.

  • Return:
    Success.
  • Exceptional return: ERANGE, malloc

static <pT>type *<T>new(struct <t>pool *const pool)

This pointer is constant until it gets <T>remove.

  • Return:
    A pointer to a new uninitialized element from pool.
  • Exceptional return: ERANGE, malloc
  • Order:
    amortised O(1)

static int <T>remove(struct <t>pool *const pool, <pT>type *const data)

Deletes data from pool. (Do not remove data that is not in pool.)

  • Return:
    Success.
  • Exceptional return: malloc
    Because of lazy deletion, remove can actually demand memory when data requires adding to the free-heap.
  • Order:
    Ο(log (slab0-free-heap | slabs))

static void <T>clear(struct <t>pool *const pool)

Removes all from pool, but keeps it's active state, only freeing the smaller blocks.

  • Order:
    Ο(log items)

void <T>graph(const <pT>box *, FILE *);

int <T>graph_fn(const <pT>box *, const char *);

2021 Neil Edelman, distributed under the terms of the MIT License.