pawengine/src/dynarray.c

46 lines
1.1 KiB
C
Raw Normal View History

2025-02-24 20:11:51 +01:00
#include "allocator.h"
2025-02-09 18:48:25 +01:00
#include <stdlib.h>
2025-02-24 20:11:51 +01:00
2025-02-09 18:48:25 +01:00
struct da_template {
int count;
int capacity;
2025-02-24 20:11:51 +01:00
void *items;
struct Mem *mem;
2025-02-09 18:48:25 +01:00
};
2025-02-24 20:11:51 +01:00
// the array struct has to be freed manually, all internal data is freedd by
// dyn_array_destroy
2025-02-09 18:48:25 +01:00
void *dyn_array_create() {
struct da_template *da = malloc(sizeof(struct da_template));
da->items = malloc(0);
2025-02-24 20:11:51 +01:00
da->capacity = 0;
2025-02-09 18:48:25 +01:00
da->count = 0;
2025-02-24 20:11:51 +01:00
da->mem = NULL;
return da;
}
// the array struct has to be freed manually, all internal data is freedd by
// dyn_array_destroy
void *dyn_array_create_mem(struct Mem *mem) {
struct da_template *da = mem_malloc(mem, sizeof(struct da_template));
da->items = mem_malloc(mem, 0);
2025-02-09 18:48:25 +01:00
da->capacity = 0;
2025-02-24 20:11:51 +01:00
da->count = 0;
da->mem = mem;
2025-02-09 18:48:25 +01:00
return da;
}
void dyn_array_create_inplace(void *array) {
struct da_template *da = array;
da->items = malloc(0);
da->count = 0;
da->capacity = 0;
2025-02-24 20:11:51 +01:00
da->mem = NULL;
}
void dyn_array_create_inplace_mem(void *array, struct Mem *mem) {
struct da_template *da = array;
da->items = mem_malloc(mem, 0);
da->count = 0;
da->capacity = 0;
da->mem = mem;
2025-02-09 18:48:25 +01:00
}