42#define CHILLBUFF_SUCCESS 0
47#define CHILLBUFF_OUT_OF_MEM 100
52#define CHILLBUFF_NULL_ARG 200
57#define CHILLBUFF_INVALID_ARG 300
62#define CHILLBUFF_OVERFLOW 400
65static void (*_chillbuff_error_callback)(
const char*) = NULL;
127static inline void _chillbuff_printerr(
const char* error,
const char* origin)
129 const size_t error_length = 64 + strlen(error) + strlen(origin);
130 char* error_msg = (
char*)chillbuff_malloc(error_length *
sizeof(
char));
131 if (error_msg != NULL)
133 snprintf(error_msg, error_length,
"\nCHILLBUFF ERROR: (%s) %s\n", origin, error);
134 if (_chillbuff_error_callback != NULL)
136 _chillbuff_error_callback(error_msg);
150 if (error_callback == NULL)
152 _chillbuff_printerr(
"The passed error callback is empty; Operation cancelled!", __func__);
156 _chillbuff_error_callback = error_callback;
165 _chillbuff_error_callback = NULL;
180 _chillbuff_printerr(
"Tried to init a NULL chillbuff instance; wouldn't end well. Cancelled...", __func__);
184 if (element_size == 0)
186 _chillbuff_printerr(
"Storing elements of size \"0\" makes no sense...", __func__);
190 if (growth_method < 0 || growth_method > 3)
192 _chillbuff_printerr(
"Invalid grow method! Please use the appropriate chillbuff_growth_method enum!", __func__);
199 buff->
capacity = initial_capacity == 0 ? 16 : initial_capacity;
202 if (buff->
array == NULL)
204 _chillbuff_printerr(
"OUT OF MEMORY!", __func__);
257 _chillbuff_printerr(
"Tried to append to a NULL chillbuff instance!", __func__);
261 if (elements == NULL)
263 _chillbuff_printerr(
"Tried to append NULL element(s) to a chillbuff instance!", __func__);
267 if (elements_count == 0)
269 _chillbuff_printerr(
"The passed \"elements_count\" argument is zero; nothing to append!", __func__);
273 for (
size_t i = 0; i < elements_count; i++)
282 _chillbuff_printerr(
"Invalid grow method! Please use the appropriate chillbuff_growth_method enum!", __func__);
285 new_capacity = (buff->
capacity * 2);
288 new_capacity = (buff->
capacity * 3);
298 if (new_capacity <= buff->capacity || new_capacity >= UINT64_MAX / buff->
element_size)
300 _chillbuff_printerr(
"Couldn't push back due to buffer OVERFLOW!", __func__);
304 void* new_array = chillbuff_realloc(buff->
array, new_capacity * buff->
element_size);
305 if (new_array == NULL)
307 _chillbuff_printerr(
"Couldn't resize chillbuff underlying array; OUT OF MEMORY!", __func__);
312 buff->
array = new_array;
#define CHILLBUFF_INVALID_ARG
Definition: chillbuff.h:57
struct chillbuff chillbuff
static void chillbuff_free(chillbuff *buff)
Definition: chillbuff.h:215
#define CHILLBUFF_OVERFLOW
Definition: chillbuff.h:62
static int chillbuff_init(chillbuff *buff, const size_t initial_capacity, const size_t element_size, const chillbuff_growth_method growth_method)
Definition: chillbuff.h:176
static void chillbuff_clear(chillbuff *buff)
Definition: chillbuff.h:234
chillbuff_growth_method
Definition: chillbuff.h:72
@ CHILLBUFF_GROW_EXPONENTIAL
Definition: chillbuff.h:91
@ CHILLBUFF_GROW_TRIPLICATIVE
Definition: chillbuff.h:81
@ CHILLBUFF_GROW_LINEAR
Definition: chillbuff.h:86
@ CHILLBUFF_GROW_DUPLICATIVE
Definition: chillbuff.h:76
static int chillbuff_set_error_callback(void(*error_callback)(const char *))
Definition: chillbuff.h:148
#define CHILLBUFF_NULL_ARG
Definition: chillbuff.h:52
#define CHILLBUFF_OUT_OF_MEM
Definition: chillbuff.h:47
static void chillbuff_unset_error_callback()
Definition: chillbuff.h:163
static int chillbuff_push_back(chillbuff *buff, const void *elements, const size_t elements_count)
Definition: chillbuff.h:253
#define CHILLBUFF_SUCCESS
Definition: chillbuff.h:42
Definition: chillbuff.h:99
chillbuff_growth_method growth_method
Definition: chillbuff.h:123
size_t length
Definition: chillbuff.h:108
void * array
Definition: chillbuff.h:103
size_t element_size
Definition: chillbuff.h:118
size_t capacity
Definition: chillbuff.h:113