1#ifndef NAUGHTY_BUFFERS_BUFFER_H
2#define NAUGHTY_BUFFERS_BUFFER_H
34#include "naughty-buffers/naughty-buffers-export.h"
48typedef void * (*nb_alloc_fn)(
size_t size,
void * context);
55typedef void * (*nb_realloc_fn)(
void * ptr,
size_t new_size,
void * context);
62typedef void * (*nb_copy_fn)(
void * destination,
const void * source,
size_t size,
void * context);
69typedef void * (*nb_move_fn)(
void * destination,
const void * source,
size_t size,
void * context);
159 size_t block_capacity;
323NAUGHTY_BUFFERS_EXPORT
void *
nb_at(
const struct nb_buffer * buffer,
size_t index);
void(* nb_free_fn)(void *ptr, void *context)
Type of a function able to release memory pointed by ptr, to be called when the buffer gets released....
Definition: buffer.h:76
void nb_remove_back(struct nb_buffer *buffer)
Removes the block at the last index from the array.
size_t nb_block_count(const struct nb_buffer *buffer)
Returns the block count of the buffer.
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.
NB_PUSH_RESULT
Result of calling nb_push.
Definition: buffer.h:170
NB_ASSIGN_RESULT
Result of calling nb_assign.
Definition: buffer.h:176
void nb_release(struct nb_buffer *buffer)
Releases all allocated memory by the buffer and resets all internal metadata effectively making it an...
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.
void nb_sort(struct nb_buffer *buffer, nb_compare_fn compare_fn)
Sorts the buffer using stdlib's qsort function.
void nb_remove_at(struct nb_buffer *buffer, size_t index)
Removes the block at the specified index.
void nb_init(struct nb_buffer *buffer, size_t block_size)
Initializes a nb_buffer struct with default values and pointers.
void *(* nb_move_fn)(void *destination, const void *source, size_t size, void *context)
Type of a function able to move a chunk of memory from source to destination, to be called when inser...
Definition: buffer.h:69
void *(* nb_copy_fn)(void *destination, const void *source, size_t size, void *context)
Type of a function able to copy a chunk of memory from source to destination, to be called when inser...
Definition: buffer.h:62
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.
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.
void *(* nb_realloc_fn)(void *ptr, size_t new_size, void *context)
Type of a function able to realloc a memory block, to be called when a buffer needs to enlarge its in...
Definition: buffer.h:55
enum NB_ASSIGN_RESULT nb_assign(struct nb_buffer *buffer, size_t index, void *data)
Copies data to the block at index index.
NB_INSERT_RESULT
Result of calling nb_insert.
Definition: buffer.h:182
struct nb_buffer_iterator nb_iterator(const struct nb_buffer *buffer)
Creates and returns an iterator that allows for performance-friendly traversal.
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.
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...
void nb_remove_front(struct nb_buffer *buffer)
Removes the block at index 0 from the array moving all other blocks to the beginning.
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_a...
int(* nb_compare_fn)(const void *ptr_a, const void *ptr_b)
Type of a function that can compare two blocks, returning < 0 if *ptr_a < *ptr_b, 0 if *ptr_a == *ptr...
Definition: buffer.h:83
void *(* nb_alloc_fn)(size_t size, void *context)
Type of a function able to allocate a block of memory that will be called when the buffer gets initia...
Definition: buffer.h:48
Structure returned by nb_iterator containing enough information to control a for-loop.
Definition: buffer.h:133
void * begin
Definition: buffer.h:135
size_t increment
Definition: buffer.h:141
void * end
Definition: buffer.h:138
A structure containing memory-related pointers.
Definition: buffer.h:98
nb_alloc_fn alloc_fn
Definition: buffer.h:100
nb_realloc_fn realloc_fn
Definition: buffer.h:103
nb_copy_fn copy_fn
Definition: buffer.h:115
void * context
Definition: buffer.h:124
nb_free_fn free_fn
Definition: buffer.h:109
nb_move_fn move_fn
Definition: buffer.h:121
a structure holding the buffer data and metadata about the blocks.
Definition: buffer.h:156