From 6e8474633ef8a9dcb32b9bc94c49e0ed7aa55e8c Mon Sep 17 00:00:00 2001 From: rhjr Date: Tue, 26 Nov 2024 21:50:37 +0100 Subject: [PATCH] Added: String8, String8Node and String8List types. --- docs/tasks | 11 ++++++----- src/base.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- src/base.h | 43 ++++++++++++++++++++++++++++++++++--------- src/eightysix.c | 14 +------------- 4 files changed, 88 insertions(+), 29 deletions(-) diff --git a/docs/tasks b/docs/tasks index e511749..91c4c89 100644 --- a/docs/tasks +++ b/docs/tasks @@ -1,14 +1,15 @@ Tasks - - [ ] finish the 'simple' arena implementation. - - [ ] arena_alloc - - [ ] arena_release + - [x] finish the 'simple' arena implementation. + - [x] arena_init + - [x] arena_alloc + - [x] arena_release - [ ] String8 implementation + - [x] String8 helpers - [ ] String8Node and String8List. - [ ] push node - [ ] list join - - [ ] Convert cstring to String8. - - [ ] Convert String8 to cstring. + - [x] Convert cstring to String8. - [ ] Read/Write to file. - [ ] Create CLI output. - [ ] Testcases? diff --git a/src/base.c b/src/base.c index fb9ba67..bf8fe41 100644 --- a/src/base.c +++ b/src/base.c @@ -1,6 +1,8 @@ +//= rhjr: allocator + internal void -arena_init(Arena *arena, u8 *backing_buffer, i32 size) +arena_init(Arena *arena, u8 *backing_buffer, i64 size) { arena->backing_buffer = backing_buffer; arena->offset = 0; @@ -8,7 +10,7 @@ arena_init(Arena *arena, u8 *backing_buffer, i32 size) } internal void * -arena_alloc(Arena *arena, i32 size) +arena_alloc(Arena *arena, i64 size) { void *result = 0; @@ -27,3 +29,46 @@ arena_release(Arena *arena) { arena->offset = 0; } + +//= rhjr: string + +String8 +str8(u8 *cstr, i64 length) +{ + String8 result = {0}; + result.str = cstr; + result.length = length; + return result; +} + +String8 +str8_cstring(u8 *cstr) +{ + String8 result = {0}; + u8 *ptr = cstr; + for(; *ptr != 0; ptr += 1); + result = str8_range(cstr, ptr); + return result; +} + +String8 +str8_range(u8 *first_char, u8 *last_char) +{ + String8 result = {0}; + result.str = first_char; + result.length = (u64)(last_char - first_char); + return result; +} + +void +str8_list_push (Arena *arena, String8List *list, String8 string) +{ +} + +String8 +str8_list_join(Arena *arena, String8List *list) +{ + String8 result = {0}; + + return result; +} diff --git a/src/base.h b/src/base.h index b68df4f..8cd0e97 100644 --- a/src/base.h +++ b/src/base.h @@ -31,24 +31,49 @@ typedef uint64_t b64; typedef struct Arena { u8 *backing_buffer; - i32 offset; - i32 size; + i64 offset; + i64 size; } Arena; -internal void arena_init(Arena *arena, u8 *backing_buffer, i32 size); -internal void * arena_alloc(Arena *arena, i32 size); +internal void arena_init(Arena *arena, u8 *backing_buffer, i64 size); +internal void * arena_alloc(Arena *arena, i64 size); internal void arena_release(Arena *arena); //= rhjr: strings -typedef struct String8 +typedef struct String8 String8; +struct String8 { - u8* contents; - i32 length; -} -String8; + u8* str; + i64 length; +}; +typedef struct String8Node String8Node; +struct String8Node +{ + String8Node *next; + String8 string; +}; +typedef struct String8List String8List; +struct String8List +{ + String8Node *first; + String8Node *last; + i64 num_of_nodes; + i64 total_length; +}; + +String8 str8(u8 *cstr, i64 length); +String8 str8_from_cstr(u8* cstr); + +#define str8_lit(string) str8((u8*)(string), sizeof(string) - 1) +#define str8_lit_comp(string) (u8*)(string), length) + +String8 str8_range (u8 *first_char, u8 *last_char); + +void str8_list_push (Arena *arena, String8List *list, String8 string); +String8 str8_list_join (Arena *arena, String8List *list); #endif // BASE_H diff --git a/src/eightysix.c b/src/eightysix.c index ea61ddb..e853ffa 100644 --- a/src/eightysix.c +++ b/src/eightysix.c @@ -15,19 +15,7 @@ u8 backing_buffer[ARENA_DEFAULT_SIZE]; int main(void) { - Arena arena = {0}; - arena_init(&arena, &backing_buffer[0], ARENA_DEFAULT_SIZE); - - i64 *idx = arena_alloc(&arena, sizeof(i64)); - *idx = 69; - - i64 *idx2 = arena_alloc(&arena, 2 * sizeof(i64)); - *idx2++ = 69; - - i64 *idx3 = arena_alloc(&arena, sizeof(i64)); - *idx3 = 124; - - *idx2 = 139; + String8 str = str8_lit("Hello world\n"); return 0; }