1.2.1
|
Classes | |
struct | nb_buffer_memory_context |
A structure containing memory-related pointers. More... | |
struct | nb_buffer_iterator |
Structure returned by nb_iterator containing enough information to control a for-loop. More... | |
struct | nb_buffer |
a structure holding the buffer data and metadata about the blocks. More... | |
Enumerations | |
enum | NB_PUSH_RESULT { NB_PUSH_OUT_OF_MEMORY , NB_PUSH_OK } |
Result of calling nb_push. | |
enum | NB_ASSIGN_RESULT { NB_ASSIGN_OUT_OF_MEMORY , NB_ASSIGN_OK } |
Result of calling nb_assign. | |
enum | NB_INSERT_RESULT { NB_INSERT_OUT_OF_MEMORY , NB_INSERT_OK } |
Result of calling nb_insert. | |
Functions | |
void | nb_init (struct nb_buffer *buffer, size_t block_size) |
Initializes a nb_buffer struct with default values and pointers. More... | |
void | nb_init_advanced (struct nb_buffer *buffer, size_t block_size, struct nb_buffer_memory_context *memory_context) |
Initializes a nb_buffer struct with custom memory functions and memory_context. More... | |
enum NB_PUSH_RESULT | nb_push (struct nb_buffer *buffer, void *data) |
Copies data to the end of the buffer, possibly reallocating it if more space needed. More... | |
size_t | nb_block_count (const struct nb_buffer *buffer) |
Returns the block count of the buffer. More... | |
void * | nb_at (const struct nb_buffer *buffer, size_t index) |
Returns a pointer to the block at position index or NULL if the index is out of bounds. More... | |
void * | nb_front (const struct nb_buffer *buffer) |
Returns a pointer to the first block or NULL if the buffer is empty. This is equivalent to calling nb_at with index 0. More... | |
void * | nb_back (const struct nb_buffer *buffer) |
Returns a pointer to the last block or NULL if the buffer is empty This is equivalent to calling nb_at with index nb_block_count(&buffer) -1 More... | |
enum NB_ASSIGN_RESULT | nb_assign (struct nb_buffer *buffer, size_t index, void *data) |
Copies data to the block at index index . More... | |
enum NB_ASSIGN_RESULT | nb_assign_many (struct nb_buffer *buffer, size_t index, void *data, size_t block_count) |
Copies block_count blocks from data to the buffer starting at index . More... | |
enum NB_INSERT_RESULT | nb_insert (struct nb_buffer *buffer, size_t index, void *data) |
Inserts data to the block at index index moving all blocks past the index forward one position. More... | |
void | nb_remove_front (struct nb_buffer *buffer) |
Removes the block at index 0 from the array moving all other blocks to the beginning. More... | |
void | nb_remove_back (struct nb_buffer *buffer) |
Removes the block at the last index from the array. More... | |
void | nb_remove_at (struct nb_buffer *buffer, size_t index) |
Removes the block at the specified index. More... | |
void | nb_sort (struct nb_buffer *buffer, nb_compare_fn compare_fn) |
Sorts the buffer using stdlib's qsort function. More... | |
void | nb_release (struct nb_buffer *buffer) |
Releases all allocated memory by the buffer and resets all internal metadata effectively making it an uninitialized buffer. More... | |
struct nb_buffer_iterator | nb_iterator (const struct nb_buffer *buffer) |
Creates and returns an iterator that allows for performance-friendly traversal. More... | |
All the functions and types to manage a buffer are here. Buffers can automatically grow to handle pushes and insertions. They start with enough space for 2 blocks and grow by powers of 2 (4, 8, 16, 32, etc)
void nb_init | ( | struct nb_buffer * | buffer, |
size_t | block_size | ||
) |
Initializes a nb_buffer struct with default values and pointers.
It will allocate enough memory to contain two blocks. All memory functions will be set to end up calling the default ones (malloc/realloc/etc).
buffer | A pointer to a nb_buffer struct to be initialized |
block_size | The size, in bytes, for each buffer block |
Example
void nb_init_advanced | ( | struct nb_buffer * | buffer, |
size_t | block_size, | ||
struct nb_buffer_memory_context * | memory_context | ||
) |
Initializes a nb_buffer struct with custom memory functions and memory_context.
You need to provide all custom memory functions and optionally a memory memory_context.
It will allocate enough memory to contain two blocks with alloc_fn
buffer | A pointer to a nb_buffer struct to be initialized |
block_size | Size, in bytes, for each buffer block |
memory_context | A pointer to a struct nb_buffer_memory_context that will be used when memory management is needed. |
Example
enum NB_PUSH_RESULT nb_push | ( | struct nb_buffer * | buffer, |
void * | data | ||
) |
Copies data to the end of the buffer, possibly reallocating it if more space needed.
buffer | A pointer to a nb_buffer struct |
data | The data to copy. |
NB_PUSH_OK
if successful, NB_PUSH_OUT_OF_MEMORY
if no more memory could be allocated. Example
size_t nb_block_count | ( | const struct nb_buffer * | buffer | ) |
void * nb_at | ( | const struct nb_buffer * | buffer, |
size_t | index | ||
) |
Returns a pointer to the block at position index
or NULL if the index is out of bounds.
buffer | A pointer to a nb_buffer struct |
index | The index to read |
Example
void * nb_front | ( | const struct nb_buffer * | buffer | ) |
void * nb_back | ( | const struct nb_buffer * | buffer | ) |
enum NB_ASSIGN_RESULT nb_assign | ( | struct nb_buffer * | buffer, |
size_t | index, | ||
void * | data | ||
) |
Copies data
to the block at index index
.
If index
is larger than the buffer capacity the buffer data will be reallocated to accommodate the new data. Uninitialized data will be present in the blocks preceding the new data if they were not present before.
buffer | A pointer to a nb_buffer struct |
index | The block index to assign the data to |
data | A pointer to the data to be copied in the buffer at the specified index. |
Example
enum NB_ASSIGN_RESULT nb_assign_many | ( | struct nb_buffer * | buffer, |
size_t | index, | ||
void * | data, | ||
size_t | block_count | ||
) |
Copies block_count
blocks from data
to the buffer starting at index
.
If index
or index + block_count
is larger than the buffer capacity the buffer data will be reallocated to accommodate the new data. Uninitialized data will be present in the blocks preceding the new data if they were not present before.
Caller is expected to ensure that data
points at the first block to be copied from and that it has at least block_count * block_size
bytes.
buffer | A pointer to a nb_buffer struct |
index | The block index to assign the data to |
data | A pointer to the data to be copied in the buffer at the specified index. |
block_count | Amount of blocks to copy from data into the buffer |
Example
enum NB_INSERT_RESULT nb_insert | ( | struct nb_buffer * | buffer, |
size_t | index, | ||
void * | data | ||
) |
Inserts data
to the block at index index
moving all blocks past the index forward one position.
If index
is larger than the buffer capacity the buffer data will be reallocated to accommodate the new data. Uninitialized data will be present in the blocks preceding the new data if they were not present before.
All blocks in positions greater or equal than the one specified with index
will be moved forward by 1 position.
buffer | A pointer to a nb_buffer struct |
index | The block index to assign the data to |
data | A pointer to the data to be copied in the buffer at the specified index. |
Example
void nb_remove_front | ( | struct nb_buffer * | buffer | ) |
Removes the block at index 0 from the array moving all other blocks to the beginning.
This is equivalent of calling nb_remove_at with index nb_block_count(&buffer) -1
.
nb_at
buffer | A pointer to a nb_buffer struct |
void nb_remove_back | ( | struct nb_buffer * | buffer | ) |
Removes the block at the last index from the array.
This is equivalent of calling nb_remove_at with index nb_block_count(&buffer) -1
.
nb_at
buffer | A pointer to a nb_buffer struct |
void nb_remove_at | ( | struct nb_buffer * | buffer, |
size_t | index | ||
) |
Removes the block at the specified index.
buffer | A pointer to a nb_buffer struct |
index | The block index to remove |
Example
void nb_sort | ( | struct nb_buffer * | buffer, |
nb_compare_fn | compare_fn | ||
) |
Sorts the buffer using stdlib's qsort function.
buffer | A pointer to a nb_buffer struct |
compare_fn | A comparison fuction returnin < 0 if the first element should come before the second, 0 if they're equal and > 0 if the first element should come after the second |
void nb_release | ( | struct nb_buffer * | buffer | ) |
Releases all allocated memory by the buffer and resets all internal metadata effectively making it an uninitialized buffer.
You can reuse the same buffer after another call to nb_init or nb_init_advanced.
buffer | A pointer to a nb_buffer struct |
struct nb_buffer_iterator nb_iterator | ( | const struct nb_buffer * | buffer | ) |
Creates and returns an iterator that allows for performance-friendly traversal.
If you intend to access a lot of elements in sequence, this is the preferred method to do so as it offers better performance in comparison to nb_at
buffer | A pointer to a nb_buffer struct |
nb_buffer_iterator
struct with values that can be used to control a for-loop.Example