1.2.1
buffer.h
1#ifndef NAUGHTY_BUFFERS_BUFFER_H
2#define NAUGHTY_BUFFERS_BUFFER_H
3
34#include "naughty-buffers/naughty-buffers-export.h"
35
36#include <stddef.h>
37#include <stdint.h>
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
48typedef void * (*nb_alloc_fn)(size_t size, void * context);
49
55typedef void * (*nb_realloc_fn)(void * ptr, size_t new_size, void * context);
56
62typedef void * (*nb_copy_fn)(void * destination, const void * source, size_t size, void * context);
63
69typedef void * (*nb_move_fn)(void * destination, const void * source, size_t size, void * context);
70
76typedef void (*nb_free_fn)(void * ptr, void * context);
77
83typedef int (*nb_compare_fn)(const void * ptr_a, const void * ptr_b);
84
101
104
110
116
122
124 void * context;
125};
126
135 void * begin;
136
138 void * end;
139
141 size_t increment;
142};
143
156struct nb_buffer {
157 size_t block_size;
158 size_t block_count;
159 size_t block_capacity;
160
161 struct nb_buffer_memory_context * memory_context;
162
163 void * data;
164};
165
170enum NB_PUSH_RESULT { NB_PUSH_OUT_OF_MEMORY, NB_PUSH_OK };
171
176enum NB_ASSIGN_RESULT { NB_ASSIGN_OUT_OF_MEMORY, NB_ASSIGN_OK };
177
182enum NB_INSERT_RESULT { NB_INSERT_OUT_OF_MEMORY, NB_INSERT_OK };
183
205NAUGHTY_BUFFERS_EXPORT void nb_init(struct nb_buffer * buffer, size_t block_size);
206
252NAUGHTY_BUFFERS_EXPORT void nb_init_advanced(
253 struct nb_buffer * buffer,
254 size_t block_size,
255 struct nb_buffer_memory_context * memory_context
256);
257
284NAUGHTY_BUFFERS_EXPORT enum NB_PUSH_RESULT nb_push(struct nb_buffer * buffer, void * data);
285
296NAUGHTY_BUFFERS_EXPORT size_t nb_block_count(const struct nb_buffer * buffer);
297
323NAUGHTY_BUFFERS_EXPORT void * nb_at(const struct nb_buffer * buffer, size_t index);
324
334NAUGHTY_BUFFERS_EXPORT void * nb_front(const struct nb_buffer * buffer);
335
345NAUGHTY_BUFFERS_EXPORT void * nb_back(const struct nb_buffer * buffer);
346
377NAUGHTY_BUFFERS_EXPORT enum NB_ASSIGN_RESULT nb_assign(struct nb_buffer * buffer, size_t index, void * data);
378
379
420 NAUGHTY_BUFFERS_EXPORT enum NB_ASSIGN_RESULT nb_assign_many(struct nb_buffer * buffer, size_t index, void * data, size_t block_count);
421
454NAUGHTY_BUFFERS_EXPORT enum NB_INSERT_RESULT nb_insert(struct nb_buffer * buffer, size_t index, void * data);
455
467NAUGHTY_BUFFERS_EXPORT void nb_remove_front(struct nb_buffer * buffer);
468
480NAUGHTY_BUFFERS_EXPORT void nb_remove_back(struct nb_buffer * buffer);
481
514NAUGHTY_BUFFERS_EXPORT void nb_remove_at(struct nb_buffer * buffer, size_t index);
515
524NAUGHTY_BUFFERS_EXPORT void nb_sort(struct nb_buffer * buffer, nb_compare_fn compare_fn);
525
534NAUGHTY_BUFFERS_EXPORT void nb_release(struct nb_buffer * buffer);
535
570NAUGHTY_BUFFERS_EXPORT struct nb_buffer_iterator nb_iterator(const struct nb_buffer * buffer);
571
572#ifdef __cplusplus
573};
574#endif
575
576#endif // NAUGHTY_BUFFERS_BUFFER_H
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