🌊 Sailfish pool (sfpool)
Secure and Fast memory pool, in 300 lines of portable, header-only C code.
|
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. | |
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.
opaque | Pointer to the memory pool structure. |
ptr | Pointer to check. |
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).
opaque | Pointer to the memory pool structure. |
ptr | Pointer to the memory block to free. |
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.
pool | Pointer to the memory pool structure to initialize. |
nmemb | Number of blocks in the pool. |
blocksize | Size of each block in bytes. |
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.
opaque | Pointer to the memory pool structure. |
size | Size of the memory block to allocate. |
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.
opaque | Pointer to the memory pool structure. |
ptr | Pointer to the memory block to reallocate. |
size | New size of the memory block. |
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).
p | Pointer to the memory pool structure. |
void sfpool_teardown | ( | sfpool_t *restrict | pool | ) |
Tears down a memory pool.
This function releases all resources associated with the memory pool.
pool | Pointer to the memory pool structure to tear down. |