summaryrefslogtreecommitdiff
path: root/src/std
diff options
context:
space:
mode:
Diffstat (limited to 'src/std')
-rw-r--r--src/std/containers/darray.h57
-rw-r--r--src/std/containers/ring_queue.c2
-rw-r--r--src/std/mem.c10
-rw-r--r--src/std/mem.h1
-rw-r--r--src/std/str.c26
-rw-r--r--src/std/str.h14
6 files changed, 67 insertions, 43 deletions
diff --git a/src/std/containers/darray.h b/src/std/containers/darray.h
index 729b4cf..b15d269 100644
--- a/src/std/containers/darray.h
+++ b/src/std/containers/darray.h
@@ -5,6 +5,11 @@
*/
// COPIED FROM KITC WITH SOME MINOR ADJUSTMENTS
+/* TODO:
+ - a 'find' function that takes a predicate (maybe wrap with a macro so we dont have to define a
+ new function?)
+*/
+
#ifndef KITC_TYPED_ARRAY_H
#define KITC_TYPED_ARRAY_H
@@ -33,14 +38,24 @@
#define PREFIX static
-#define KITC_DECL_TYPED_ARRAY(T) \
- typedef typed_array(T) T##_darray; \
- typedef typed_array_iterator(T) T##_darray_iter; \
+/* if (arena != NULL) {\ */
+/* d = arena_alloc(arena, sizeof(T##_darray));\ */
+/* data = arena_alloc(arena, starting_capacity * sizeof(T));\ */
+/* } else {\ */
+/* }\ */
+
+#define KITC_DECL_TYPED_ARRAY(T) DECL_TYPED_ARRAY(T, T)
+
+#define DECL_TYPED_ARRAY(T, Type) \
+ typedef typed_array(T) Type##_darray; \
+ typedef typed_array_iterator(Type) Type##_darray_iter; \
\
/* Create a new one growable array */ \
- PREFIX T##_darray *T##_darray_new(size_t starting_capacity) { \
- T##_darray *d = malloc(sizeof(T##_darray)); \
- T *data = malloc(starting_capacity * sizeof(T)); \
+ PREFIX Type##_darray *Type##_darray_new(size_t starting_capacity) { \
+ Type##_darray *d; \
+ T *data; \
+ d = malloc(sizeof(Type##_darray)); \
+ data = malloc(starting_capacity * sizeof(T)); \
\
d->len = 0; \
d->capacity = starting_capacity; \
@@ -49,14 +64,14 @@
return d; \
} \
\
- PREFIX void T##_darray_free(T##_darray *d) { \
+ PREFIX void Type##_darray_free(Type##_darray *d) { \
if (d != NULL) { \
free(d->data); \
free(d); \
} \
} \
\
- PREFIX T *T##_darray_resize(T##_darray *d, size_t capacity) { \
+ PREFIX T *Type##_darray_resize(Type##_darray *d, size_t capacity) { \
/* resize the internal data block */ \
T *new_data = realloc(d->data, sizeof(T) * capacity); \
/* TODO: handle OOM error */ \
@@ -66,22 +81,22 @@
return new_data; \
} \
\
- PREFIX void T##_darray_push(T##_darray *d, T value) { \
+ PREFIX void Type##_darray_push(Type##_darray *d, T value) { \
if (d->len >= d->capacity) { \
size_t new_capacity = \
d->capacity > 0 ? d->capacity * DARRAY_RESIZE_FACTOR : DARRAY_DEFAULT_CAPACITY; \
- T *resized = T##_darray_resize(d, new_capacity); \
+ T *resized = Type##_darray_resize(d, new_capacity); \
} \
\
d->data[d->len] = value; \
d->len += 1; \
} \
\
- PREFIX void T##_darray_push_copy(T##_darray *d, const T *value) { \
+ PREFIX void Type##_darray_push_copy(Type##_darray *d, const T *value) { \
if (d->len >= d->capacity) { \
size_t new_capacity = \
d->capacity > 0 ? d->capacity * DARRAY_RESIZE_FACTOR : DARRAY_DEFAULT_CAPACITY; \
- T *resized = T##_darray_resize(d, new_capacity); \
+ T *resized = Type##_darray_resize(d, new_capacity); \
} \
\
T *place = d->data + d->len; \
@@ -89,18 +104,18 @@
memcpy(place, value, sizeof(T)); \
} \
\
- PREFIX void T##_darray_pop(T##_darray *d, T *dest) { \
+ PREFIX void Type##_darray_pop(Type##_darray *d, T *dest) { \
T *item = d->data + (d->len - 1); \
d->len -= 1; \
memcpy(dest, item, sizeof(T)); \
} \
\
- PREFIX void T##_darray_ins(T##_darray *d, const T *value, size_t index) { \
+ PREFIX void Type##_darray_ins(Type##_darray *d, const T *value, size_t index) { \
/* check if requires resize */ \
if (d->len + 1 > d->capacity) { \
size_t new_capacity = \
d->capacity > 0 ? d->capacity * DARRAY_RESIZE_FACTOR : DARRAY_DEFAULT_CAPACITY; \
- T *resized = T##_darray_resize(d, new_capacity); \
+ T *resized = Type##_darray_resize(d, new_capacity); \
} \
\
/* shift existing data after index */ \
@@ -114,14 +129,14 @@
memcpy(insert_dest, value, sizeof(T)); \
} \
\
- PREFIX void T##_darray_clear(T##_darray *d) { \
+ PREFIX void Type##_darray_clear(Type##_darray *d) { \
d->len = 0; \
memset(d->data, 0, d->capacity * sizeof(T)); \
} \
\
- PREFIX size_t T##_darray_len(T##_darray *d) { return d->len; } \
+ PREFIX size_t Type##_darray_len(Type##_darray *d) { return d->len; } \
\
- PREFIX void T##_darray_print(T##_darray *d) { \
+ PREFIX void Type##_darray_print(Type##_darray *d) { \
printf("len: %zu ", d->len); \
printf("capacity: %zu\n", d->capacity); \
for (int i = 0; i < d->len; i++) { \
@@ -129,14 +144,14 @@
} \
} \
\
- PREFIX T##_darray_iter T##_darray_iter_new(T##_darray *d) { \
- T##_darray_iter iterator; \
+ PREFIX Type##_darray_iter Type##_darray_iter_new(Type##_darray *d) { \
+ Type##_darray_iter iterator; \
iterator.array = d; \
iterator.current_idx = 0; \
return iterator; \
} \
\
- PREFIX void *T##_darray_iter_next(T##_darray_iter *iterator) { \
+ PREFIX void *Type##_darray_iter_next(Type##_darray_iter *iterator) { \
if (iterator->current_idx < iterator->array->len) { \
return &iterator->array->data[iterator->current_idx++]; \
} else { \
diff --git a/src/std/containers/ring_queue.c b/src/std/containers/ring_queue.c
index a9d3506..8bfc10b 100644
--- a/src/std/containers/ring_queue.c
+++ b/src/std/containers/ring_queue.c
@@ -1,5 +1,7 @@
#include "ring_queue.h"
+
#include <stdlib.h>
+#include <string.h>
#include "defines.h"
ring_queue* ring_queue_new(size_t type_size, size_t capacity, void* memory) {
diff --git a/src/std/mem.c b/src/std/mem.c
index f5b92d4..5468898 100644
--- a/src/std/mem.c
+++ b/src/std/mem.c
@@ -11,12 +11,12 @@
void* arena_alloc_align(arena* a, size_t size, size_t align) {
ptrdiff_t padding = -(uintptr_t)a->curr & (align - 1);
ptrdiff_t available = a->end - a->curr - padding;
- TRACE("Padding %td available %td", padding, available);
+ // TRACE("Padding %td available %td", padding, available);
if (available < 0 || (ptrdiff_t)size > available) {
ERROR_EXIT("Arena ran out of memory\n");
}
- void* p = a->begin + padding;
- a->begin += padding + size;
+ void* p = a->curr + padding;
+ a->curr += padding + size;
return memset(p, 0, size);
}
void* arena_alloc(arena* a, size_t size) { return arena_alloc_align(a, size, DEFAULT_ALIGNMENT); }
@@ -29,4 +29,6 @@ arena arena_create(void* backing_buffer, size_t capacity) {
void arena_free_all(arena* a) {
a->curr = a->begin; // pop everything at once and reset to the start.
-} \ No newline at end of file
+}
+
+void arena_free_storage(arena* a) { free(a->begin); } \ No newline at end of file
diff --git a/src/std/mem.h b/src/std/mem.h
index c3ec61d..2f92894 100644
--- a/src/std/mem.h
+++ b/src/std/mem.h
@@ -22,4 +22,5 @@ arena arena_create(void* backing_buffer, size_t capacity);
void* arena_alloc(arena* a, size_t size);
void* arena_alloc_align(arena* a, size_t size, size_t align);
void arena_free_all(arena* a);
+void arena_free_storage(arena* a);
// TODO: arena_resize \ No newline at end of file
diff --git a/src/std/str.c b/src/std/str.c
index 7dafef1..07a8e73 100644
--- a/src/std/str.c
+++ b/src/std/str.c
@@ -1,10 +1,12 @@
#include "str.h"
-#include <string.h>
#include <assert.h>
+#include <string.h>
#include "mem.h"
str8 str8_create(u8* buf, size_t len) { return (str8){ .buf = buf, .len = len }; }
+str8 str8_cstr_view(char* string) { return str8_create((u8*)string, strlen(string)); }
+
bool str8_equals(str8 a, str8 b) {
if (a.len != b.len) {
return false;
@@ -48,25 +50,15 @@ str8 str8_substr(str8 s, u64 min, u64 max) {
assert(min < s.len);
assert(max >= 0);
assert(max <= s.len);
- uint8_t * start = s.buf + (ptrdiff_t)min;
+ uint8_t* start = s.buf + (ptrdiff_t)min;
size_t new_len = max - min;
- return (str8) {.buf = start, .len = new_len };
+ return (str8){ .buf = start, .len = new_len };
}
-str8 str8_take(str8 s, u64 first_n) {
- return str8_substr(s, 0, first_n);
-}
+str8 str8_take(str8 s, u64 first_n) { return str8_substr(s, 0, first_n); }
-str8 str8_drop(str8 s, u64 last_n) {
- return str8_substr(s, s.len - last_n, s.len);
-}
-
-str8 str8_skip(str8 s, u64 n) {
- return str8_substr(s, n, s.len);
-}
-
-str8 str8_chop(str8 s, u64 n) {
- return str8_substr(s, 0, s.len - n);
-}
+str8 str8_drop(str8 s, u64 last_n) { return str8_substr(s, s.len - last_n, s.len); }
+str8 str8_skip(str8 s, u64 n) { return str8_substr(s, n, s.len); }
+str8 str8_chop(str8 s, u64 n) { return str8_substr(s, 0, s.len - n); }
diff --git a/src/std/str.h b/src/std/str.h
index 735b88e..1ebecac 100644
--- a/src/std/str.h
+++ b/src/std/str.h
@@ -10,6 +10,8 @@
*/
#pragma once
+#include <ctype.h>
+
#include "defines.h"
#include "mem.h"
@@ -36,6 +38,11 @@ char* str8_to_cstr(arena* a, str8 s);
#define cstr(a, s) (str8_to_cstr(a, s)) // Shorthand
+/** @brief Return a str8 that references a statically allocated string.
+ `string` therefore must already be null-terminated.
+ @note The backing `string` cannot be modified. */
+str8 str8_cstr_view(char* string);
+
// --- Comparisons
/** @brief Compare two strings for exact equality */
@@ -70,4 +77,9 @@ str8 str8_concat(arena* a, str8 left, str8 right);
static inline bool str8_is_null_term(str8 a) {
return a.buf[a.len] == 0; // This doesn't seem safe. YOLO
-} \ No newline at end of file
+}
+
+// TODO: move or delete this and replace with handling using our internal type
+static void skip_space(char* p) {
+ while (isspace((unsigned char)*p)) ++p;
+}