aboutsummaryrefslogtreecommitdiff
path: root/teensy
diff options
context:
space:
mode:
Diffstat (limited to 'teensy')
-rw-r--r--teensy/dyn_arr.c42
-rw-r--r--teensy/dyn_arr.h50
-rw-r--r--teensy/mem.c6
-rw-r--r--teensy/teensy.h1
4 files changed, 42 insertions, 57 deletions
diff --git a/teensy/dyn_arr.c b/teensy/dyn_arr.c
index 38e3b31..46e619d 100644
--- a/teensy/dyn_arr.c
+++ b/teensy/dyn_arr.c
@@ -3,42 +3,10 @@
#include <stdio.h>
#include <assert.h>
-void *_ty_list_create(size_t type_size, int init_cap)
+void *ty_list_create(void)
{
- init_cap = init_cap < TY_LIST_MIN_CAP
- ? TY_LIST_MIN_CAP
- : init_cap;
-
- int *arr = (int*)ty_alloc(type_size * init_cap + sizeof(int) * 2);
-
- arr[0] = 0;
- arr[1] = init_cap;
- return (void*)(arr + 2);
-}
-
-void *_ty_list_append_slot(size_t type_size, void **arr)
-{
- int *cap = ty_list_cap_ptr(*arr);
- int *len = ty_list_len_ptr(*arr);
-
- (*len)++;
-
- if (*len > *cap) {
- *cap *= TY_LIST_GROW_RATE;
-
- void* base = ty_list_base(*arr);
- base = ty_realloc(base, (type_size * *cap) + sizeof(int) * 2);
- assert(base != NULL); // Just to handle the case
- *arr = (void*)((int*)base + 2);
-
- len = ty_list_len_ptr(*arr);
- }
-
- return (char*)*arr + ((*len - 1) * type_size);
-}
-
-void ty_list_free(void *arr)
-{
- void *base = ty_list_base(arr);
- ty_free(base);
+ struct ty_list_header *header = ty_alloc(sizeof(struct ty_list_header));
+ header->cap = 0;
+ header->len = 0;
+ return (void*)(header + 1);
}
diff --git a/teensy/dyn_arr.h b/teensy/dyn_arr.h
index 7a45d4e..a764cda 100644
--- a/teensy/dyn_arr.h
+++ b/teensy/dyn_arr.h
@@ -2,32 +2,48 @@
#define DYN_ARR_H_
#include "common.h"
+#include "mem.h"
+
+struct ty_list_header {
+ size_t cap;
+ size_t len;
+};
#define TY_LIST_MIN_CAP 8
#define TY_LIST_GROW_RATE 2
-#define ty_list_create(T, init_capacity) \
- ((T*)_ty_list_create(sizeof(T), init_capacity))
-
-#define ty_list_len_ptr(arr) ((int*)arr - 2)
-#define ty_list_len(arr) (*ty_list_len_ptr(arr))
+#define ty_list_get_header(arr) ((struct ty_list_header*)arr - 1)
+#define ty_list_cap(arr) (ty_list_get_header(arr)->cap)
+#define ty_list_len(arr) (ty_list_get_header(arr)->len)
-#define ty_list_cap_ptr(arr) ((int*)arr - 1)
-#define ty_list_cap(arr) (*ty_list_cap_ptr(arr))
-#define ty_list_base(arr) ((void*)ty_list_len_ptr(arr))
+#define ty_list_reserve(arr, amt) \
+ do { \
+ struct ty_list_header *header = ty_list_get_header(arr); \
+ if (amt > header->cap) { \
+ header->cap = header->cap < TY_LIST_MIN_CAP \
+ ? TY_LIST_MIN_CAP \
+ : header->cap * TY_LIST_GROW_RATE \
+ header = ty_realloc( \
+ header, \
+ (sizeof(*(arr)) * count) + sizeof(struct ty_list_header) \
+ ); \
+ (arr) = (void*)(header + 1); \
+ } \
+ } while (0)
-#define ty_list_append_slot(T, arr) \
- ((T*)_ty_list_append_slot(sizeof(T), (void**)arr))
-
-#define ty_list_append(T, arr, elem) \
- (*ty_list_append_slot(T, arr) = elem)
+#define ty_list_append(arr, elem) \
+ do { \
+ struct ty_list_header *header = ty_list_get_header(arr); \
+ ty_list_reserve(arr, header->len + 1); \
+ (arr)[header->len++] = (elem); \
+ } while (0)
#define ty_list_clear(arr) \
- (*ty_list_len_ptr(arr) = 0)
+ (ty_list_get_header(arr)->len = 0)
+
+#define ty_list_free(arr) (ty_free(ty_list_get_header(arr)))
-void *_ty_list_create(size_t type_size, int init_cap);
-void *_ty_list_append_slot(size_t type_size, void **arr);
-void ty_list_free(void *arr);
+void *ty_list_create(void);
#endif // DYN_ARR_H_
diff --git a/teensy/mem.c b/teensy/mem.c
index 2c938a6..fa885ca 100644
--- a/teensy/mem.c
+++ b/teensy/mem.c
@@ -35,7 +35,7 @@ void ty_init_mem(void)
ty_log_fatal(TY_ERR_MEM, "(%s) (track) ran out of memory", __func__);
#endif // TEENSY_DEBUG
- temp_allocations = ty_list_create(void*, 0);
+ temp_allocations = ty_list_create();
}
void ty_deinit_mem(void)
@@ -67,7 +67,7 @@ void ty_deinit_mem(void)
void ty_free_temp_allocs(void)
{
- for (int i = 0; i < ty_list_len(temp_allocations); i++) {
+ for (size_t i = 0; i < ty_list_len(temp_allocations); i++) {
void *temp = temp_allocations[i];
ty_free(temp);
}
@@ -140,7 +140,7 @@ void *ty_realloc(void *ptr, size_t new_size)
void *ty_talloc(size_t size)
{
void *ptr = ty_alloc(size);
- ty_list_append(void*, &temp_allocations, ptr);
+ ty_list_append(temp_allocations, ptr);
return ptr;
}
diff --git a/teensy/teensy.h b/teensy/teensy.h
index 372fc52..c94986f 100644
--- a/teensy/teensy.h
+++ b/teensy/teensy.h
@@ -3,6 +3,7 @@
#include "common.h"
#include "context.h"
+#include "dyn_arr.h"
#define ty_vec2(x, y) ((struct ty_vec2){x, y})
#define ty_vec2i(x, y) ((struct ty_vec2i){x, y})