elpekenin/utils/dyn_array.h

Dynamic (grows as needed) array of elements.


struct header_t
[source]

Metadata stored for a dynamic array.

allocator_t *allocator
[source]

The allocator it will use to grow.

size_t capacity
[source]

How many of slots the array has allocated.

size_t length
[source]

How many slots are currently used.

size_t item_size
[source]

Size of each element type, to allocate size accordingly.


new_array(type, size, allocator)
[source]

Create a new dynamic array.

Parameters:
  • type – The type of the elements in the container.

  • size – Initial size of the array. Useful to prevent extra allocation calls/wasted space.

  • allocator – Used to grow.

Returns:

A pointer to the first element in the newly created array.

void *_new_array(size_t item_size, size_t initial_size, allocator_t *allocator)
[source]

Internal function used by new_array.

Hint

You really shouldn’t use this, but the macro that provides some convenience.

static inline header_t *get_header(void *array)
[source]

Given a dynamic array, access its metadata.

static inline size_t array_len(void *array)
[source]

Convenience to get the length of a dynamic array.

int expand_if_needed(void **array)
[source]

Check if array is full (capacity == length). If so, make it bigger.

Returns:

Whether operation was successful. See elpekenin/errno.h for details.

Attention

So far, this grow duplicates capacity, beware your memory usage and initialize the array with a better size if needed.

array_append(array, value)
[source]

Put a new element into an array.

Returns:

Whether operation was successful. See elpekenin/errno.h for details.

static inline void array_pop(void *array, size_t n)
[source]

Remove n elements from the array.

Note

Does not zero out the memory, but mark it as “can be used”.