2023-11-25 18:51:42 +00:00
|
|
|
/**
|
|
|
|
@file
|
|
|
|
@brief Dynamic `void*` arrays.
|
|
|
|
*/
|
2023-11-23 18:02:40 +00:00
|
|
|
#ifndef _BOOTSTRAP_DYN_ARRAY_H
|
|
|
|
#define _BOOTSTRAP_DYN_ARRAY_H
|
2023-11-22 20:43:34 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-11-25 18:51:42 +00:00
|
|
|
/**
|
|
|
|
@brief A dynamic array of generic pointers.
|
|
|
|
|
|
|
|
A `void*` wrapper that grows larger as needed. If more space is needed during an
|
|
|
|
append operation, the capacity of the internal array is doubled.
|
|
|
|
*/
|
2023-11-22 21:39:27 +00:00
|
|
|
struct DynArray {
|
2023-11-25 18:51:42 +00:00
|
|
|
/// The underlying `void*` pointer.
|
2023-11-22 21:39:27 +00:00
|
|
|
void **buf;
|
2023-11-25 18:51:42 +00:00
|
|
|
/// The size of @ref DynArray.buf.
|
2023-11-22 21:39:27 +00:00
|
|
|
size_t size;
|
2023-11-25 18:51:42 +00:00
|
|
|
// The allocated size of @ref DynArray.buf.
|
2023-11-22 21:39:27 +00:00
|
|
|
size_t _capacity;
|
|
|
|
};
|
2023-11-22 20:43:34 +00:00
|
|
|
|
2023-11-25 18:51:42 +00:00
|
|
|
/**
|
|
|
|
@brief Create a new @ref DynArray instance.
|
|
|
|
|
|
|
|
@param capacity
|
|
|
|
The initial size of the internal array. To avoid too many reallocations, aim to
|
|
|
|
make this value large enough to accommodate the eventual size of the buffer.
|
|
|
|
|
|
|
|
@see dyn_array_free
|
|
|
|
*/
|
2023-11-22 20:43:34 +00:00
|
|
|
struct DynArray *dyn_array_new(size_t capacity);
|
|
|
|
|
2023-11-25 18:51:42 +00:00
|
|
|
/**
|
|
|
|
@brief Returns the number of items contained in the internal buffer.
|
|
|
|
|
|
|
|
@param a
|
|
|
|
A valid pointer to a @ref DynArray instance.
|
|
|
|
@return
|
|
|
|
The number of items contained in the internal buffer.
|
|
|
|
*/
|
2023-11-22 20:43:34 +00:00
|
|
|
size_t dyn_array_size(struct DynArray *a);
|
|
|
|
|
2023-11-25 18:51:42 +00:00
|
|
|
/**
|
|
|
|
@brief Appends a new item onto the end of the internal @ref DynArray.buf.
|
|
|
|
|
|
|
|
This function takes ownership of @p item and will attempt to `free` the
|
|
|
|
parameter when the @ref DynArray is `free`'d. For this reason, only provide
|
|
|
|
entries that have been allocated on the heap.
|
|
|
|
|
|
|
|
@param a
|
|
|
|
A valid pointer to a @ref DynArray instance.
|
|
|
|
@param item
|
|
|
|
A valid pointer to a heap-allocated object.
|
|
|
|
|
|
|
|
@see dyn_array_free
|
|
|
|
*/
|
2023-11-22 20:43:34 +00:00
|
|
|
void dyn_array_push(struct DynArray *a, void *item);
|
|
|
|
|
2023-11-25 18:51:42 +00:00
|
|
|
/**
|
|
|
|
@brief Deallocates a previously allocated @ref DynArray instance.
|
|
|
|
|
|
|
|
@param a
|
|
|
|
A pointer to a @ref DynArray instance. If null, this function is a no-op.
|
|
|
|
|
|
|
|
@see dyn_array_new
|
|
|
|
*/
|
2023-11-22 20:43:34 +00:00
|
|
|
void dyn_array_free(struct DynArray *a);
|
|
|
|
|
2023-11-23 18:02:40 +00:00
|
|
|
#endif /* _BOOTSTRAP_DYN_ARRAY_H */
|