Array. Dynamic size. Push back 'n' chill. Buffer stuff. Dynamic stuff that's buff. Dynamically reallocating buff.. Yeah!
More...
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
Go to the source code of this file.
Array. Dynamic size. Push back 'n' chill. Buffer stuff. Dynamic stuff that's buff. Dynamically reallocating buff.. Yeah!
- Author
- Raphael Beck
- Date
- 27. December 2019
- See also
- https://github.com/GlitchedPolygons/chillbuff
◆ CHILLBUFF_INVALID_ARG
#define CHILLBUFF_INVALID_ARG 300 |
This error code is returned by a chillbuff function if you passed an invalid parameter into it.
◆ CHILLBUFF_NULL_ARG
#define CHILLBUFF_NULL_ARG 200 |
Error code returned by a chillbuff function if you passed a NULL argument that shouldn't have been NULL.
◆ CHILLBUFF_OUT_OF_MEM
#define CHILLBUFF_OUT_OF_MEM 100 |
Chill time is over, you're out of memory... Time to reconsider memory usage.
◆ CHILLBUFF_OVERFLOW
#define CHILLBUFF_OVERFLOW 400 |
◆ CHILLBUFF_SUCCESS
#define CHILLBUFF_SUCCESS 0 |
Returned from a chillbuff function when everything went smooth 'n' chill. Time to get Schwifty!
◆ chillbuff
Self-reallocating dynamic size array of no strictly defined type. Easy 'n' "chill" (hope you like segmentation fault errors).
◆ chillbuff_growth_method
How should the chillbuff's underlying array grow in size once its maximum capacity is reached during a push_back?
◆ chillbuff_growth_method
How should the chillbuff's underlying array grow in size once its maximum capacity is reached during a push_back?
Enumerator |
---|
CHILLBUFF_GROW_DUPLICATIVE | Double the capacity.
|
CHILLBUFF_GROW_TRIPLICATIVE | Triple the capacity.
|
CHILLBUFF_GROW_LINEAR | Grow by the same capacity every time the buffer is full.
|
CHILLBUFF_GROW_EXPONENTIAL | Multiplies the capacity by itself. Not the greatest idea... Use carefully!
|
◆ chillbuff_clear()
static void chillbuff_clear |
( |
chillbuff * |
buff | ) |
|
|
inlinestatic |
Clears a chillbuff's data.
Deletes all of the underlying array's elements and resets the length to 0
.
Leaves the array allocated at the current capacity.
- Parameters
-
buff | The chillbuff to clear. If this is NULL , nothing happens at all. |
◆ chillbuff_free()
static void chillbuff_free |
( |
chillbuff * |
buff | ) |
|
|
inlinestatic |
Frees a chillbuff instance.
- Parameters
-
buff | The chillbuff to deallocate. If this is NULL , nothing happens at all. |
◆ chillbuff_init()
Initializes a chillbuff instance and makes it ready to accept data.
- Parameters
-
buff | The chillbuff instance to init (or rather, a pointer to it). |
initial_capacity | The initial capacity of the underlying array. If you pass 0 here, 16 is used by default. |
element_size | How big should every array element be? E.g. if you're storing int you should pass sizeof(int) . |
growth_method | How should the buffer grow once its maximum capacity is reached? |
- See also
- chillbuff_growth_method
- Returns
- Chillbuff exit code as defined at the top of the chillbuff.h header file.
0
means success.
◆ chillbuff_push_back()
static int chillbuff_push_back |
( |
chillbuff * |
buff, |
|
|
const void * |
elements, |
|
|
const size_t |
elements_count |
|
) |
| |
|
static |
Appends one or more elements to the buffer. If the buffer is full, it will be expanded automatically.
- Parameters
-
buff | The buffer into which to insert the elements. |
elements | The array of elements to insert (pointer to the first element). |
elements_count | Amount of elements to add (for example: if your buffer stores the type uint32_t , you'd pass sizeof(elements_to_add) / sizeof(uint32_t) here). If you're only adding a single element, pass 1 . |
- Returns
- Chillbuff exit code that describes the insertion's outcome.
◆ chillbuff_set_error_callback()
static int chillbuff_set_error_callback |
( |
void(*)(const char *) |
error_callback | ) |
|
|
inlinestatic |
Sets the chillbuff error callback.
If errors occur, they'll be passed as a string into the provided callback function.
- Parameters
-
error_callback | The function to call when errors occur. |
- Returns
- Whether the callback was set up correctly or not (chillbuff exit code, see top of chillbuff.h file for more details).
◆ chillbuff_unset_error_callback()
static void chillbuff_unset_error_callback |
( |
| ) |
|
|
inlinestatic |
Clears the chillbuff error callback (errors won't be printed anymore).