chillbuff
Classes | Macros | Typedefs | Enumerations | Functions
chillbuff.h File Reference

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>
Include dependency graph for chillbuff.h:

Go to the source code of this file.

Classes

struct  chillbuff
 

Macros

#define CHILLBUFF_SUCCESS   0
 
#define CHILLBUFF_OUT_OF_MEM   100
 
#define CHILLBUFF_NULL_ARG   200
 
#define CHILLBUFF_INVALID_ARG   300
 
#define CHILLBUFF_OVERFLOW   400
 

Typedefs

typedef enum chillbuff_growth_method chillbuff_growth_method
 
typedef struct chillbuff chillbuff
 

Enumerations

enum  chillbuff_growth_method { CHILLBUFF_GROW_DUPLICATIVE = 0 , CHILLBUFF_GROW_TRIPLICATIVE = 1 , CHILLBUFF_GROW_LINEAR = 2 , CHILLBUFF_GROW_EXPONENTIAL = 3 }
 

Functions

static int chillbuff_set_error_callback (void(*error_callback)(const char *))
 
static void chillbuff_unset_error_callback ()
 
static int chillbuff_init (chillbuff *buff, const size_t initial_capacity, const size_t element_size, const chillbuff_growth_method growth_method)
 
static void chillbuff_free (chillbuff *buff)
 
static void chillbuff_clear (chillbuff *buff)
 
static int chillbuff_push_back (chillbuff *buff, const void *elements, const size_t elements_count)
 

Detailed Description

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

Macro Definition Documentation

◆ 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

Not good...

◆ CHILLBUFF_SUCCESS

#define CHILLBUFF_SUCCESS   0

Returned from a chillbuff function when everything went smooth 'n' chill. Time to get Schwifty!

Typedef Documentation

◆ chillbuff

typedef struct chillbuff 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?

Enumeration Type Documentation

◆ 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!

Function Documentation

◆ 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
buffThe 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
buffThe chillbuff to deallocate. If this is NULL, nothing happens at all.

◆ chillbuff_init()

static int chillbuff_init ( chillbuff buff,
const size_t  initial_capacity,
const size_t  element_size,
const chillbuff_growth_method  growth_method 
)
inlinestatic

Initializes a chillbuff instance and makes it ready to accept data.

Parameters
buffThe chillbuff instance to init (or rather, a pointer to it).
initial_capacityThe initial capacity of the underlying array. If you pass 0 here, 16 is used by default.
element_sizeHow big should every array element be? E.g. if you're storing int you should pass sizeof(int).
growth_methodHow 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
buffThe buffer into which to insert the elements.
elementsThe array of elements to insert (pointer to the first element).
elements_countAmount 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_callbackThe 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).