htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
darray.h File Reference
#include <htils/arena.h>
#include <htils/assert.h>
#include <htils/basictypes.h>

Go to the source code of this file.

Data Structures

struct  da_header
 The header of a dynamic array. More...

Macros

#define DA_HEADER_SIZE
#define da_len(darray)
#define da_cap(darray)
#define da_new(arena, darray, initial_capacity)
 Initialize a dynamic array.
#define da_append(arena, darray, item)
 Append an item to a dynamic array.
#define da_pop(darray)
 Pop an item from a dynamic array.
#define da_last(darray)
 Get the last item of a dynamic array.
#define da_clear(darray)
 Clears a dynamic array.

Typedefs

typedef struct da_header da_header_t
 The header of a dynamic array.

Functions

static da_header_tda__hdr (void *da)
 Get a dynamic array's header.

Macro Definition Documentation

◆ da_append

#define da_append ( arena,
darray,
item )
Value:
do { \
htils_assert(arena != null && "Arena cannot be null."); \
htils_assert(darray != null && "Darray cannot be null."); \
\
if (!(darray) || da_len(darray) >= da_cap(darray)) { \
u64 old_capacity = da_cap(darray); \
u64 new_capacity = \
old_capacity ? old_capacity + old_capacity / 2 + (old_capacity % 2) \
: 8; \
u64 old_len = da_len(darray); \
u64 alloc_size = DA_HEADER_SIZE + new_capacity * sizeof(*(darray)); \
\
da_header_t *header = __arena_alloc((arena), alloc_size); \
\
header->cap = new_capacity; \
header->len = old_len; \
\
if (darray) \
memcpy(header + 1, (darray), old_len * sizeof(*(darray))); \
\
(darray) = (void *)(header + 1); \
} \
\
(darray)[da__hdr(darray)->len++] = (item); \
} while (0)
void * __arena_alloc(struct arena *arena, u64 size)
Allocate a chunk of memory to the arena.
Definition arena.c:403
uint64_t u64
Definition basictypes.h:22
#define null
Definition basictypes.h:56
#define da_len(darray)
Definition darray.h:49
#define DA_HEADER_SIZE
Definition darray.h:24
static da_header_t * da__hdr(void *da)
Get a dynamic array's header.
Definition darray.h:40
#define da_cap(darray)
Definition darray.h:52
struct da_header da_header_t
The header of a dynamic array.
An atomic arena.
Definition arena.h:45
u64 len
Definition darray.h:20
u64 cap
Definition darray.h:19

Append an item to a dynamic array.

Appends item, growing the capacity (a new arena allocation holding the existing elements) when the array is full.

Parameters
arenaThe arena to allocate the dynamic array from.
darrayThe dynamic array to append to.
itemThe item to append to the dynamic array.
Precondition
arena, darray, and item must be valid and cannot be null.
See also
arena_alloc().

◆ da_cap

#define da_cap ( darray)
Value:
((darray) ? da__hdr(darray)->cap : 0)

Gets the capacity of the current dynamic array.

◆ da_clear

#define da_clear ( darray)
Value:
do { \
if (darray) \
da__hdr(darray)->len = 0; \
} while (0)

Clears a dynamic array.

By setting its length to 0, if the dynamic array is null, nothing happens.

Parameters
darrayThe dynamic array to clear.

◆ DA_HEADER_SIZE

#define DA_HEADER_SIZE
Value:
((sizeof(da_header_t) + _Alignof(max_align_t) - 1) & \
~(_Alignof(max_align_t) - 1))

The size of the header of a dynamic array.

◆ da_last

#define da_last ( darray)
Value:
(htils_assert(da__hdr(darray)->len > 0), (darray)[da__hdr(darray)->len - 1])
#define htils_assert(expr)
Assert that expr is true, terminating the program otherwise.
Definition assert.h:57

Get the last item of a dynamic array.

Asserts that the length of the dynamic array is larger than 0, then simply gets the last entry.

Parameters
darrayThe dynamic array to get the last item from.

◆ da_len

#define da_len ( darray)
Value:
((darray) ? da__hdr(darray)->len : 0)

Gets the length of the current dynamic array.

◆ da_new

#define da_new ( arena,
darray,
initial_capacity )
Value:
do { \
htils_assert(arena != null && "Arena cannot be null."); \
htils_assert(initial_capacity > 0 && \
"Initial capacity must be greater than 0"); \
\
u64 capacity = (initial_capacity); \
u64 alloc_size = DA_HEADER_SIZE + capacity * sizeof(*(darray)); \
\
da_header_t *header = __arena_alloc_zeroed((arena), alloc_size); \
header->cap = capacity; \
header->len = 0; \
(darray) = (void *)(header + 1); \
} while (0)
void * __arena_alloc_zeroed(struct arena *arena, u64 size)
Allocate a chunk of zeroed memory to the arena.
Definition arena.c:474

Initialize a dynamic array.

Caluclates the size for the header with DA_HEADER_SIZE, and the capacity; Allocates the header with initial_capacity, using arena_alloc(), then sets the dynamic array to be the header + 1 offsetting the header.

Parameters
arenaThe arena to allocate the dynamic array from.
darrayThe dynamic array to initialize.
initial_capacityThe initial capacity of the dynamic array.
Precondition
  • arena, and darray must be valid and cannot be null.
  • initial_capacity must be greater than 0.
See also
arena_alloc().

◆ da_pop

#define da_pop ( darray)
Value:
do { \
if (darray && da__hdr(darray)->len > 0) \
da__hdr(darray)->len--; \
} while (0)

Pop an item from a dynamic array.

By simply decrementing its length, if the dynamic array is empty, nothing happens.

Parameters
darrayThe dynamic array to pop from.

Typedef Documentation

◆ da_header_t

typedef struct da_header da_header_t

The header of a dynamic array.

Parameters
capThe capacity of the dynamic array.
lenThe length of the dynamic array.

Function Documentation

◆ da__hdr()

da_header_t * da__hdr ( void * da)
inlinestatic

Get a dynamic array's header.

The header sits immediately before the array's first element; this offsets back to it.

Parameters
daThe dynamic array to get the header from.
Precondition
da must be valid and cannot be null.
Returns
The da_header of the dynamic array.