Kohi Game Engine
darray.h
Go to the documentation of this file.
1 
19 #pragma once
20 
21 #include "defines.h"
22 
23 struct frame_allocator_int;
24 
33 KAPI void* _darray_create(u64 length, u64 stride, struct frame_allocator_int* frame_allocator);
34 
42 KAPI void* _darray_resize(void* array);
43 
51 KAPI void* _darray_push(void* array, const void* value_ptr);
52 
62 KAPI void* _darray_insert_at(void* array, u64 index, void* value_ptr);
63 
65 #define DARRAY_DEFAULT_CAPACITY 1
66 
68 #define DARRAY_RESIZE_FACTOR 2
69 
76 #define darray_create(type) \
77  _darray_create(DARRAY_DEFAULT_CAPACITY, sizeof(type), 0)
78 
86 #define darray_create_with_allocator(type, allocator) \
87  _darray_create(DARRAY_DEFAULT_CAPACITY, sizeof(type), allocator)
88 
96 #define darray_reserve(type, capacity) \
97  _darray_create(capacity, sizeof(type), 0)
98 
107 #define darray_reserve_with_allocator(type, capacity, allocator) \
108  _darray_create(capacity, sizeof(type), allocator)
109 
114 KAPI void darray_destroy(void* array);
115 
122 #define darray_push(array, value) \
123  { \
124  typeof(value) temp = value; \
125  array = _darray_push(array, &temp); \
126  }
127 // NOTE: could use __auto_type for temp above, but intellisense
128 // for VSCode flags it as an unknown type. typeof() seems to
129 // work just fine, though. Both are GNU extensions.
130 
136 KAPI void darray_pop(void* array, void* value_ptr);
137 
146 #define darray_insert_at(array, index, value) \
147  { \
148  typeof(value) temp = value; \
149  array = _darray_insert_at(array, index, &temp); \
150  }
151 
160 KAPI void* darray_pop_at(void* array, u64 index, void* value_ptr);
161 
166 KAPI void darray_clear(void* array);
167 
173 KAPI u64 darray_capacity(void* array);
174 
180 KAPI u64 darray_length(void* array);
181 
187 KAPI u64 darray_stride(void* array);
188 
196 KAPI void darray_length_set(void* array, u64 value);
KAPI void darray_length_set(void *array, u64 value)
Sets the length of the given array. This ensures the array has the required capacity to be able to se...
KAPI u64 darray_capacity(void *array)
Gets the given array's capacity.
KAPI void * _darray_insert_at(void *array, u64 index, void *value_ptr)
Inserts a copy of the given value into the supplied array at the given index. Triggers an array resiz...
KAPI void darray_destroy(void *array)
Destroys the provided array, freeing any memory allocated by it.
KAPI void * _darray_resize(void *array)
Resizes the given array using internal resizing amounts. Causes a new allocation.
KAPI u64 darray_stride(void *array)
Gets the stride (element size) of the given array.
KAPI void darray_clear(void *array)
Clears all entries from the array. Does not release any internally-allocated memory.
KAPI void darray_pop(void *array, void *value_ptr)
Pops an entry out of the array and places it into dest.
KAPI void * darray_pop_at(void *array, u64 index, void *value_ptr)
Pops an entry out of the array at the given index and places it into dest. Brings in all entries afte...
KAPI void * _darray_create(u64 length, u64 stride, struct frame_allocator_int *frame_allocator)
Creates a new darray of the given length and stride. Note that this performs a dynamic memory allocat...
KAPI u64 darray_length(void *array)
Gets the length (number of elements) in the given array.
KAPI void * _darray_push(void *array, const void *value_ptr)
Pushes a new entry to the given array. Resizes if necessary.
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:177
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
Definition: frame_data.h:7