dynamic arrays seem to be working
This commit is contained in:
@@ -25,9 +25,9 @@ typedef struct allocator_vtbl {
|
||||
// An instance of an allocator.
|
||||
typedef struct allocator {
|
||||
// pointer to the behind-the-scenes data that an allocator may store.
|
||||
void *const this;
|
||||
void *this;
|
||||
// pointer to the method implementations of an allocator.
|
||||
const allocator_vtbl_t *const vtbl;
|
||||
const allocator_vtbl_t *vtbl;
|
||||
} allocator_t;
|
||||
|
||||
void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line);
|
||||
@@ -125,7 +125,18 @@ void *dyn_array_create_func(dyn_array_create_func_args_t args);
|
||||
// Always reassign the array. if multiple variables reference the same growing
|
||||
// array, then you should be using pointer pointers.
|
||||
void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int line);
|
||||
#define dyn_array_grow(THIS, N_NEW_ITEMS) dyn_array_grow_func(THIS, N_NEW_ITEMS, __FILE__, __LINE__)
|
||||
void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *file, int line);
|
||||
|
||||
#define dyn_array_grow(THIS, N_NEW_ITEMS) \
|
||||
dyn_array_grow_func(THIS, N_NEW_ITEMS, __FILE__, __LINE__)
|
||||
|
||||
#define dyn_array_shrink(THIS, N_ITEMS_TO_REMOVE) \
|
||||
dyn_array_shrink_func(THIS, N_ITEMS_TO_REMOVE, __FILE__, __LINE__)
|
||||
|
||||
#define dyn_array_reset(THIS) \
|
||||
do { \
|
||||
dyn_array_shrink(THIS, dyn_array_length(THIS)); \
|
||||
} while (0)
|
||||
|
||||
typedef struct dyn_array_create_non_crashing_func_args {
|
||||
allocator_t allocator;
|
||||
@@ -136,6 +147,7 @@ void *dyn_array_create_non_crashing_func(dyn_array_create_non_crashing_func_args
|
||||
// This version returns a NULL pointer instead of crashing if the allocator return NULL.
|
||||
// It is up to you to check that the pointer returned isn't NULL
|
||||
#define dyn_array_create_non_crashing(ALLOCATOR, TYPE, ...) ((TYPE*) dyn_array_create_func((dyn_array_create_non_crashing_func_args_t){.allocator=ALLOCATOR, .itemsize=sizeof(TYPE), __VA_ARGS__}))
|
||||
|
||||
// This version returns a NULL pointer instead of crashing if the allocator return NULL.
|
||||
// It is up to you to check that the pointer returned isn't NULL
|
||||
// Always reassign the array. if multiple variables reference the same growing
|
||||
@@ -145,6 +157,12 @@ void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items);
|
||||
|
||||
size_t dyn_array_length(void *this);
|
||||
size_t dyn_array_capacity(void *this);
|
||||
#define dyn_array_append(THIS, VAL) do { \
|
||||
THIS = dyn_array_grow(THIS, 1); \
|
||||
THIS[dyn_array_length(THIS)-1] = VAL; \
|
||||
} while(0)
|
||||
|
||||
#define dyn_array_pop(THIS) (dyn_array_shrink(THIS, 1), THIS[dyn_array_length(THIS)])
|
||||
|
||||
#ifdef ALLOCATOR_IMPLEMENTATION
|
||||
|
||||
|
||||
+23
-8
@@ -24,7 +24,7 @@ void *dyn_array_create_func(dyn_array_create_func_args_t args) {
|
||||
void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int line) {
|
||||
void *bytes = dyn_array_grow_non_crashing_func(this, n_new_items);
|
||||
if (bytes == NULL) {
|
||||
fpritnf(
|
||||
fprintf(
|
||||
stderr,
|
||||
"%s:%d: allocator returned NULL\n",
|
||||
file,
|
||||
@@ -54,9 +54,9 @@ void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items) {
|
||||
if (header->capacity < new_size) {
|
||||
size_t cap_times_2 = header->capacity * 2;
|
||||
size_t new_capacity =
|
||||
cap_times_2 > new_size
|
||||
? cap_times_2
|
||||
: new_size;
|
||||
cap_times_2 < new_size
|
||||
? new_size
|
||||
: cap_times_2;
|
||||
header = allocator_resize(
|
||||
header->allocator,
|
||||
header,
|
||||
@@ -65,15 +65,30 @@ void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items) {
|
||||
if (header == NULL) { return NULL; }
|
||||
header->capacity = new_capacity;
|
||||
}
|
||||
return &header->bytes
|
||||
return &header->bytes;
|
||||
}
|
||||
|
||||
void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *file, int line) {
|
||||
if (dyn_array_length(this) < n_items_to_remove) {
|
||||
fprintf(
|
||||
stderr,
|
||||
"%s:%d: tried to remove more items than contained in dynamic array.\n",
|
||||
file,
|
||||
line
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
||||
header->size -= n_items_to_remove;
|
||||
}
|
||||
|
||||
size_t dyn_array_length(void *this) {
|
||||
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
||||
return header->size;
|
||||
|
||||
}
|
||||
|
||||
size_t dyn_array_capacity(void *this); {
|
||||
size_t dyn_array_capacity(void *this) {
|
||||
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
||||
return header->capacity;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
CC := gcc
|
||||
CFLAGS := -std=c99 -pedantic -Wall -Wextra -Wno-override-init -O0 -g -fno-omit-frame-pointer -fno-inline
|
||||
CFLAGS := -pedantic -Wall -Wextra -Wno-override-init -O0 -g -fno-omit-frame-pointer -fno-inline
|
||||
LDFLAGS := -lcriterion
|
||||
|
||||
TESTBIN := /tmp/all_tests
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <criterion/criterion.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "cig.h"
|
||||
|
||||
Test(dynamic_arrays, append) {
|
||||
with_borrow(alloc) {
|
||||
int *numbers = dyn_array_create(alloc, int);
|
||||
dyn_array_append(numbers, 40);
|
||||
dyn_array_append(numbers, 41);
|
||||
dyn_array_append(numbers, 42);
|
||||
dyn_array_append(numbers, 43);
|
||||
dyn_array_append(numbers, 44);
|
||||
dyn_array_append(numbers, 45);
|
||||
dyn_array_append(numbers, 46);
|
||||
dyn_array_append(numbers, 47);
|
||||
dyn_array_append(numbers, 48);
|
||||
dyn_array_append(numbers, 49);
|
||||
dyn_array_append(numbers, 50);
|
||||
for (int i = 0; i < (int)dyn_array_length(numbers); i++) {
|
||||
cr_assert_eq(numbers[i], i+40);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Test(dynamic_arrays, pop) {
|
||||
with_borrow(alloc) {
|
||||
int *numbers = dyn_array_create(alloc, int);
|
||||
dyn_array_append(numbers, 40);
|
||||
dyn_array_append(numbers, 41);
|
||||
dyn_array_append(numbers, 42);
|
||||
dyn_array_append(numbers, 43);
|
||||
dyn_array_append(numbers, 44);
|
||||
dyn_array_append(numbers, 45);
|
||||
dyn_array_append(numbers, 46);
|
||||
dyn_array_append(numbers, 47);
|
||||
dyn_array_append(numbers, 48);
|
||||
dyn_array_append(numbers, 49);
|
||||
dyn_array_append(numbers, 50);
|
||||
for (int i = 0; i < (int)dyn_array_length(numbers); i++) {
|
||||
int num = dyn_array_pop(numbers);
|
||||
cr_assert_eq(num, 50-i);
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -23,6 +23,6 @@ Test(macro_magic, default_values) {
|
||||
cr_assert_eq(a1.is_true, true);
|
||||
cr_assert_eq(a2.is_true, true);
|
||||
cr_assert_eq(a3.is_true, false);
|
||||
malloc(100);
|
||||
// TODO: make this shite crash the tests...
|
||||
// TODO if there is an allocation error it does not actually cause a crash...
|
||||
// FIX!!!
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user