Skip to content

File syn_pool.h

FileList > src > syntropic > util > syn_pool.h

Go to the source code of this file

Fixed-size block memory pool — zero-heap, header-only. More...

  • #include "../util/syn_assert.h"
  • #include <stddef.h>
  • #include <stdint.h>
  • #include <string.h>

Classes

Type Name
struct SYN_Pool
Fixed-size block memory pool.

Public Static Functions

Type Name
void * syn_pool_alloc (SYN_Pool * pool)
Allocate a block from the pool.
size_t syn_pool_available (const SYN_Pool * pool)
Get the number of available (free) blocks.
size_t syn_pool_block_count (const SYN_Pool * pool)
Get the total block count.
size_t syn_pool_block_size (const SYN_Pool * pool)
Get the aligned block size.
void syn_pool_free (SYN_Pool * pool, void * block)
Return a block to the pool.
size_t syn_pool_high_watermark (const SYN_Pool * pool)
Get the peak allocation count (high watermark).
void syn_pool_init (SYN_Pool * pool, void * buf, size_t buf_size, size_t block_size)
Initialize a memory pool.
size_t syn_pool_used (const SYN_Pool * pool)
Get the number of currently allocated blocks.

Macros

Type Name
define SYN_POOL_ALIGN4 (x) (((x) + 3u) & ~3u)
Round x up to the next multiple of 4.
define SYN_POOL_BUF_SIZE (block_size, count) ([**SYN\_POOL\_ALIGN4**](syn__pool_8h.md#define-syn_pool_align4)(block\_size) \* (count))
Compute the buffer size required for a pool.

Detailed Description

A pool allocator backed by a caller-provided byte array. All blocks are the same size. Allocation and free are O(1) via a freelist embedded in the free blocks themselves.

Block size is automatically rounded up to 4-byte alignment. Minimum block size is sizeof(void*) (4 bytes on 32-bit targets).

** **

// Allocate a pool of 8 blocks, each 64 bytes
static uint8_t backing[SYN_POOL_BUF_SIZE(64, 8)];
static SYN_Pool pool;

syn_pool_init(&pool, backing, sizeof(backing), 64);

void *blk = syn_pool_alloc(&pool);
// ... use blk ...
syn_pool_free(&pool, blk);

Public Static Functions Documentation

function syn_pool_alloc

Allocate a block from the pool.

static inline void * syn_pool_alloc (
    SYN_Pool * pool
) 

Parameters:

  • pool Pool instance.

Returns:

Pointer to allocated block, or NULL if pool is exhausted.


function syn_pool_available

Get the number of available (free) blocks.

static inline size_t syn_pool_available (
    const SYN_Pool * pool
) 

Parameters:

  • pool Pool instance.

Returns:

Free block count.


function syn_pool_block_count

Get the total block count.

static inline size_t syn_pool_block_count (
    const SYN_Pool * pool
) 

Parameters:

  • pool Pool instance.

Returns:

Total number of blocks.


function syn_pool_block_size

Get the aligned block size.

static inline size_t syn_pool_block_size (
    const SYN_Pool * pool
) 

Parameters:

  • pool Pool instance.

Returns:

Block size in bytes (4-byte aligned).


function syn_pool_free

Return a block to the pool.

static inline void syn_pool_free (
    SYN_Pool * pool,
    void * block
) 

The block must have been previously allocated from this pool.

Parameters:

  • pool Pool instance.
  • block Pointer to block to free (NULL is safely ignored).

function syn_pool_high_watermark

Get the peak allocation count (high watermark).

static inline size_t syn_pool_high_watermark (
    const SYN_Pool * pool
) 

Parameters:

  • pool Pool instance.

Returns:

Maximum simultaneous allocations observed.


function syn_pool_init

Initialize a memory pool.

static inline void syn_pool_init (
    SYN_Pool * pool,
    void * buf,
    size_t buf_size,
    size_t block_size
) 

Partitions the backing buffer into fixed-size blocks and threads a freelist through all of them.

Parameters:

  • pool Pool instance (caller-owned).
  • buf Backing storage (caller-owned, must be 4-byte aligned).
  • buf_size Size of backing storage in bytes.
  • block_size Desired block size. Rounded up to 4-byte alignment. Must be >= sizeof(void*).

function syn_pool_used

Get the number of currently allocated blocks.

static inline size_t syn_pool_used (
    const SYN_Pool * pool
) 

Parameters:

  • pool Pool instance.

Returns:

Used block count.


Macro Definition Documentation

define SYN_POOL_ALIGN4

Round x up to the next multiple of 4.

#define SYN_POOL_ALIGN4 (
    x
) `(((x) + 3u) & ~3u)`


define SYN_POOL_BUF_SIZE

Compute the buffer size required for a pool.

#define SYN_POOL_BUF_SIZE (
    block_size,
    count
) `( SYN_POOL_ALIGN4 (block_size) * (count))`

Parameters:

  • block_size Desired block size (will be aligned to 4).
  • count Number of blocks.

Returns:

Required buffer size in bytes.



The documentation for this class was generated from the following file src/syntropic/util/syn_pool.h