Go to the source code of this file.
◆ 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."); \
htils_assert(item !=
null &&
"Item cannot be null."); \
\
u64 new_capacity = old_capacity ? old_capacity * 2 : 8; \
\
\
header->
cap = new_capacity; \
\
if (darray) \
memcpy(header + 1, (darray), old_len * sizeof(*(darray))); \
\
(darray) = (void *)(header + 1); \
} \
\
} while (0)
#define arena_alloc(arena, type, size)
Allocate a chunk of memory from the arena.
Definition arena.h:167
uint64_t u64
Definition basictypes.h:17
#define null
Definition basictypes.h:47
#define DA_HEADER_SIZE
Definition darray.h:24
static da_header_t * da__hdr(void *da)
Get the da_header.
Definition darray.h:44
An arena.
Definition arena.h:26
Append an item to a dynamic array.
By incrementing the len, has automatic capacity growing functionality through extra allocations with arena_alloc().
- Parameters
-
| arena | The arena to allocate the dynamic array from. |
| darray | The dynamic array to append to. |
| item | The 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 | ) |
((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
-
| darray | The dynamic array to clear. |
◆ DA_HEADER_SIZE
Value:
~(_Alignof(max_align_t) - 1))
struct da_header da_header_t
The header of a dynamic array.
The size of the header of a dynamic array.
◆ da_last
Substitute with the last item in a dynamic array.
Asserts that the length of the dynamic array is larger than 0, then simply gets the last entry.
- Parameters
-
| darray | The dynamic array to get the last item from. |
◆ da_len
| #define da_len |
( |
|
darray | ) |
((darray) ? da__hdr(darray)->len : 0) |
Gets the length of the current dynamic array
◆ da_new
| #define da_new |
( |
|
arena, |
|
|
|
darray, |
|
|
|
intitial_capacity |
|
) |
| |
Value: do { \
htils_assert(
arena !=
null &&
"Arena cannot be null."); \
htils_assert(intitial_capacity > 0 && \
"Initial capacity must be greater than 0"); \
\
u64 capacity = (intitial_capacity); \
\
header->
cap = capacity; \
(darray) = (void *)(header + 1); \
} while (0)
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
-
| arena | The arena to allocate the dynamic array from. |
| darray | The dynamic array to initialize. |
| intitial_capacity | The initial capacity of the dynamic array. |
- Precondition
arena, and darray must be valid and cannot be null.
intitial_capacity must be greater than 0.
- See also
- arena_alloc()
◆ da_pop
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
-
| darray | The dynamic array to pop from. |
◆ da_header_t
The header of a dynamic array.
- Parameters
-
| cap | The capacity of the dynamic array. |
| len | The length of the dynamic array. |
◆ da__hdr()
Get the da_header.
Casts the da to cstr * from a dynamic array, by then offsetting by the size of it.
- Parameters
-
| da | The dynamic array to get the header from. |
- Precondition
da must be valid and cannot be null.
- Returns
- The da_header of the dynamic array.