🌊 Sailfish pool (sfpool)
Secure and Fast memory pool, in 300 lines of portable, header-only C code.
Loading...
Searching...
No Matches
Functions
High-Level API

Functions

size_t sfpool_init (sfpool_t *pool, size_t nmemb, size_t blocksize)
 Initializes a memory pool.
 
void sfpool_teardown (sfpool_t *restrict pool)
 Tears down a memory pool.
 
void * sfpool_malloc (void *restrict opaque, const size_t size)
 Allocates memory from the pool.
 
void sfpool_free (void *restrict opaque, void *ptr)
 Frees memory allocated from the pool.
 
void * sfpool_realloc (void *restrict opaque, void *ptr, const size_t size)
 Reallocates memory from the pool.
 
int sfpool_contains (void *restrict opaque, const void *ptr)
 Checks if a pointer is within the memory pool.
 
void sfpool_status (sfpool_t *restrict p)
 Prints the status of the memory pool.
 

Detailed Description

Function Documentation

◆ sfpool_contains()

int sfpool_contains ( void *restrict  opaque,
const void *  ptr 
)

Checks if a pointer is within the memory pool.

This function checks if the given pointer is within the memory pool.

Parameters
opaquePointer to the memory pool structure.
ptrPointer to check.
Returns
1 if the pointer is within the pool, 0 otherwise.

◆ sfpool_free()

void sfpool_free ( void *restrict  opaque,
void *  ptr 
)

Frees memory allocated from the pool.

This function frees memory that was allocated from the pool. If the memory was not allocated from the pool, it falls back to system free (if enabled).

Parameters
opaquePointer to the memory pool structure.
ptrPointer to the memory block to free.

◆ sfpool_init()

size_t sfpool_init ( sfpool_t *  pool,
size_t  nmemb,
size_t  blocksize 
)

Initializes a memory pool.

This function initializes a memory pool with a specified number of blocks and block size. The block size must be a power of two.

Parameters
poolPointer to the memory pool structure to initialize.
nmembNumber of blocks in the pool.
blocksizeSize of each block in bytes.
Returns
Total size of the memory pool in bytes, or 0 on failure.

◆ sfpool_malloc()

void * sfpool_malloc ( void *restrict  opaque,
const size_t  size 
)

Allocates memory from the pool.

This function allocates memory from the pool if the requested size is within the block size. Otherwise, it falls back to system malloc.

Parameters
opaquePointer to the memory pool structure.
sizeSize of the memory block to allocate.
Returns
Pointer to the allocated memory block, or NULL on failure.

◆ sfpool_realloc()

void * sfpool_realloc ( void *restrict  opaque,
void *  ptr,
const size_t  size 
)

Reallocates memory from the pool.

This function reallocates memory from the pool. If the new size is larger than the block size, it allocates new memory using system malloc and copies the old data.

Parameters
opaquePointer to the memory pool structure.
ptrPointer to the memory block to reallocate.
sizeNew size of the memory block.
Returns
Pointer to the reallocated memory block, or NULL on failure.

◆ sfpool_status()

void sfpool_status ( sfpool_t *restrict  p)

Prints the status of the memory pool.

This function prints the current status of the memory pool, including the number of blocks, block size, and profiling information (if enabled).

Parameters
pPointer to the memory pool structure.

◆ sfpool_teardown()

void sfpool_teardown ( sfpool_t *restrict  pool)

Tears down a memory pool.

This function releases all resources associated with the memory pool.

Parameters
poolPointer to the memory pool structure to tear down.