1
0
Fork 0

Added: String8, String8Node and String8List types.

Browse Source
This commit is contained in:
rhjr 2024-11-26 21:50:37 +01:00
parent 2488680376
commit 6e8474633e
4 changed files with 88 additions and 29 deletions

View File

@ -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?

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}