Compare commits

5 Commits
Author SHA1 Message Date
noselasd 075b049894 Missing commit 2026-09-10 13:13:24 +02:00
noselasd e34cadf6e7 Reorganize files to base/ folder 2026-09-10 13:11:41 +02:00
noselasd 47082f3de2 comment update 2026-09-09 08:47:31 +02:00
noselasd d8a5e176e2 Cleanups 2026-09-09 00:08:57 +02:00
noselasd 10186202ad Add single/double linked list macros 2026-09-08 23:45:14 +02:00
12 changed files with 297 additions and 16 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
CFLAGS=${CFLAGS:-"-fsanitize=address -ggdb"} CFLAGS=${CFLAGS:-"-fsanitize=address -ggdb"}
CC=${CC:-gcc} CC=${CC:-gcc}
set -x set -x
${CC} -DDEBUG -pipe -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable -std=gnu2x $CFLAGS src/*c ${CC} -DDEBUG -pipe -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable -std=gnu2x $CFLAGS src/*c src/base/*c
+1 -1
View File
@@ -1,5 +1,5 @@
#include "arena.h" #include "arena.h"
#include "base_core.h" #include "core.h"
#include "platform.h" #include "platform.h"
+1 -1
View File
@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "base_core.h" #include "core.h"
typedef U32 ArenaFlags; typedef U32 ArenaFlags;
+168
View File
@@ -410,3 +410,171 @@ static INLINE void allocator_free_all(Allocator *allocator)
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \ #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size)) ((void)(addr), (void)(size))
#endif #endif
// linked lists
/*
SList - singly linked lists
"list" struct must have a "*first" and "**last" member to entrys
"first"/"last" structs must have a "*next" member
*/
/**
* Initialize a list
*
* @param list struct pointer with a *first and a **last member to an entry struct
*/
#define SListInit(list) \
do { \
(list)->first = NULL; \
(list)->last = &(list)->first; \
} while (0)
/**
* Adds entry to the head of a list
*
* @param list struct pointer with a *first and a **last member to an entry struct
* @param entry struct pointer with a *next member to an entry struct
*/
#define SListAddFirst(list, entry) \
do { \
(entry)->next = (list)->first; \
if ((entry)->next == NULL) { \
(list)->last = &(entry)->next; \
} \
(list)->first = (entry); \
} while (0)
/**
* Adds entry to the tail of a list
*
* @param list struct pointer with a *first and a **last member to an entry struct
* @param entry struct pointer with a *next member to an entry struct
*/
#define SListAddLast(list, entry) \
do { \
(entry)->next = NULL; \
*(list)->last = (entry); \
(list)->last = &(entry)->next; \
} while (0)
/**
* Remove head from list, list MUST not be empty
*
* @param entry struct pointer with a *next member to an entry struct
*/
#define SListRemoveFirst(list) \
do { \
(list)->first = (list)->first->next; \
if ((list)->first == NULL) { \
(list)->last = &(list)->first; \
} \
} while (0)
#define SListIsEmpty(list) ((list)->first == NULL)
#define SListResetNode(node) ((node)->next = NULL)
/*
DList - doubly linked lists
"list" struct must have a "*first" and "*last" member to entrys
"first"/"last" structs must have a "*next" and a "*prev" member
*/
/**
* Initialize a list
*
* @param list struct pointer with a *first and a *last member to an entry struct
*/
#define DListInit(list) \
do { \
(list)->first = NULL; \
(list)->last = NULL; \
} while (0)
/**
* Adds entry to the head of a list
*
* @param list struct pointer with a *first and a *last member to an entry struct
* @param entry struct pointer with a *next and *prev member to an entry struct
*/
#define DListAddFirst(list, entry) \
do { \
(entry)->next = (list)->first; \
(entry)->prev = NULL; \
if ((list)->first != NULL) { \
(list)->first->prev = (entry); \
} else { \
(list)->last = (entry); \
} \
(list)->first = (entry); \
} while (0)
/**
* Adds entry to the tail of a list
*
* @param list struct pointer with a *first and a *last member to an entry struct
* @param entry struct pointer with a *next and *prev member to an entry struct
*/
#define DListAddLast(list, entry) \
do { \
(entry)->prev = (list)->last; \
(entry)->next = NULL; \
if ((list)->last != NULL) { \
(list)->last->next = (entry); \
} else { \
(list)->first = (entry); \
} \
(list)->last = (entry); \
} while (0)
/**
* Remove first entry from list, list MUST not be empty
*
* @param entry struct pointer with a *next and *prev member to an entry struct
*/
#define DListRemoveFirst(list) \
do { \
(list)->first = (list)->first->next; \
if ((list)->first != NULL) { \
(list)->first->prev = NULL; \
} else { \
(list)->last = NULL; \
} \
} while (0)
/**
* Remove last entry from list, list MUST not be empty
*
* @param entry struct pointer with a *next and *prev member to an entry struct
*/
#define DListRemoveLast(list) \
do { \
(list)->last = (list)->last->prev; \
if ((list)->last != NULL) { \
(list)->last->next = NULL; \
} else { \
(list)->first = NULL; \
} \
} while (0)
/**
* Remove last entry from list, entry must be in the list
*
* @param entry struct pointer with a *next and *prev member to an entry struct
*/
#define DListRemove(list, entry) \
do { \
if ((entry)->next != NULL) { \
(entry)->next->prev = (entry)->prev; \
} else { \
(list)->last = (entry)->prev; \
} \
if ((entry)->prev != NULL) { \
(entry)->prev->next = (entry)->next; \
} else { \
(list)->first = (entry)->next; \
} \
} while (0)
#define DListIsEmpty(list) ((list)->first == NULL)
#define DListResetNode(node) ((node)->next = (node)->prev = NULL)
+1 -1
View File
@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "base_core.h" #include "core.h"
// Mini Arena for arena allocations from a supplied buffer // Mini Arena for arena allocations from a supplied buffer
+1 -1
View File
@@ -1,4 +1,4 @@
#include "base_core.h" #include "core.h"
#include "platform.h" #include "platform.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include <stdlib.h> #include <stdlib.h>
#include "base_core.h" #include "core.h"
void *platform_malloc(size_t size); void *platform_malloc(size_t size);
void platform_free(void *mem); void platform_free(void *mem);
View File
+1 -1
View File
@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "base_core.h" #include "core.h"
typedef union APoolElement APoolElement; typedef union APoolElement APoolElement;
union APoolElement { union APoolElement {
View File
+3 -3
View File
@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "base_core.h" #include "core.h"
typedef struct Str Str; typedef struct Str Str;
struct Str { struct Str {
@@ -103,9 +103,9 @@ static INLINE Str str_skip(Str str, U32 len)
return new_str; return new_str;
} }
// Destination must be at least source.len * 2. Returns hex_result. // hex_result must be at least source.len * 2. Returns hex_result.
Str str_hex_encode(Str binary_data, Str hex_result); Str str_hex_encode(Str binary_data, Str hex_result);
// Destination must be at least source.len / 2. Returns result. // result must be at least source.len / 2. Returns result.
Str str_hex_decode(Str hex_str, Str result); Str str_hex_decode(Str hex_str, Str result);
static INLINE Str str_hex_encode_alloc(Allocator *allocator, Str binary_data) static INLINE Str str_hex_encode_alloc(Allocator *allocator, Str binary_data)
+119 -6
View File
@@ -1,9 +1,9 @@
#include "base_core.h" #include "base/core.h"
#include "arena.h" #include "base/arena.h"
#include "mini_arena.h" #include "base/marena.h"
#include "platform.h" #include "base/platform.h"
#include "pool.h" #include "base/pool.h"
#include "string.h" #include "base/string.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -515,6 +515,8 @@ void print_runes(Str str)
void string_test_utf8(void) void string_test_utf8(void)
{ {
TRACEF("\n");
Str str1 = S("Test 1ø2 ☺"); Str str1 = S("Test 1ø2 ☺");
TRACEF("str1:\n"); TRACEF("str1:\n");
@@ -537,6 +539,8 @@ void string_test_utf8(void)
void string_test_hex(void) void string_test_hex(void)
{ {
TRACEF("\n");
MArena arena; MArena arena;
U8 data[4096]; U8 data[4096];
marena_init(&arena, data, sizeof data); marena_init(&arena, data, sizeof data);
@@ -557,8 +561,115 @@ void string_test_hex(void)
Str str2_decoded = str_hex_decode_alloc(&allocator, str2_encoded); Str str2_decoded = str_hex_decode_alloc(&allocator, str2_encoded);
printf("str2_decoded: '%.*s'\n", str2_decoded.len, str2_decoded.str); printf("str2_decoded: '%.*s'\n", str2_decoded.len, str2_decoded.str);
}
typedef struct StrSLNode StrSLNode;
struct StrSLNode {
Str str;
StrSLNode *next;
};
typedef struct StrSList StrSList;
struct StrSList {
StrSLNode *first;
StrSLNode **last;
};
void string_test_slist(void)
{
TRACEF("\n");
Str str1 = S("one");
Str str2 = S("two");
Str str3 = S("three");
StrSList strlist = {};
StrSLNode n1 = {str1, NULL};
StrSLNode n2 = {str2, NULL};
StrSLNode n3 = {str3, NULL};
SListInit(&strlist);
SListAddFirst(&strlist, &n1);
SListAddFirst(&strlist, &n2);
SListAddFirst(&strlist, &n3);
TRACEF("List test first\n");
for (StrSLNode *n = strlist.first; n != NULL; n = n->next) {
printf("Node: %.*s\n", n->str.len, n->str.str);
}
TRACEF("List test first done\n");
SListInit(&strlist);
SListAddLast(&strlist, &n1);
SListAddLast(&strlist, &n2);
SListAddLast(&strlist, &n3);
SListRemoveFirst(&strlist);
TRACEF("List test last\n");
for (StrSLNode *n = strlist.first; n != NULL; n = n->next) {
printf("Node: %.*s\n", n->str.len, n->str.str);
}
TRACEF("List test last done\n");
}
typedef struct StrDLNode StrDLNode;
struct StrDLNode {
Str str;
StrDLNode *next;
StrDLNode *prev;
};
typedef struct StrDList StrDList;
struct StrDList {
StrDLNode *first;
StrDLNode *last;
};
void string_test_dlist(void)
{
TRACEF("\n");
Str str1 = S("one");
Str str2 = S("two");
Str str3 = S("three");
Str str4 = S("four");
StrDList strlist = {};
StrDLNode n1 = {str1, NULL, NULL};
StrDLNode n2 = {str2, NULL, NULL};
StrDLNode n3 = {str3, NULL, NULL};
StrDLNode n4 = {str4, NULL, NULL};
DListInit(&strlist);
DListAddFirst(&strlist, &n1);
DListAddFirst(&strlist, &n2);
DListAddFirst(&strlist, &n3);
TRACEF("List test first\n");
for (StrDLNode *n = strlist.first; n != NULL; n = n->next) {
printf("Node: %.*s\n", n->str.len, n->str.str);
}
TRACEF("List test first done\n");
DListInit(&strlist);
DListAddLast(&strlist, &n2);
DListAddLast(&strlist, &n3);
DListAddLast(&strlist, &n4);
DListAddFirst(&strlist, &n1);
TRACEF("List test full\n");
for (StrDLNode *n = strlist.first; n != NULL; n = n->next) {
printf("Node: %.*s\n", n->str.len, n->str.str);
}
TRACEF("List test full done\n");
DListRemove(&strlist, &n1);
DListRemoveFirst(&strlist);
DListRemoveLast(&strlist);
TRACEF("List test tail removed\n");
for (StrDLNode *n = strlist.first; n != NULL; n = n->next) {
printf("Node: %.*s\n", n->str.len, n->str.str);
}
TRACEF("List test tail removed done\n");
} }
@@ -580,6 +691,8 @@ int main(int argc, char *argv[])
string_test_join(); string_test_join();
string_test_utf8(); string_test_utf8();
string_test_hex(); string_test_hex();
string_test_slist();
string_test_dlist();
} }