From 92850237f42cecfeba519bc15f7f5bb7a76cde5f Mon Sep 17 00:00:00 2001 From: pryazha Date: Thu, 27 Mar 2025 08:29:28 +0500 Subject: renaming (mostly) --- example/example | Bin 62240 -> 62744 bytes example/example.c | 238 ++++++++++++++++++++++++++++-------------------------- prb.h | 9 ++- prb_arena.c | 48 +++++++++++ prb_arena.h | 6 ++ prb_macros.h | 92 ++++++++++----------- prb_math.c | 118 +++++++++++++-------------- prb_math.h | 86 ++++++++++---------- prb_memory.c | 42 ---------- prb_memory.h | 11 --- prb_os_io.c | 31 ------- prb_os_io.h | 8 -- prb_string.c | 193 +++++++++++++++++++++++-------------------- prb_string.h | 53 +++++++----- prb_sys.c | 52 ++++++++++++ prb_sys.h | 7 ++ prb_types.h | 57 +++++++------ 17 files changed, 561 insertions(+), 490 deletions(-) create mode 100644 prb_arena.c create mode 100644 prb_arena.h delete mode 100644 prb_memory.c delete mode 100644 prb_memory.h delete mode 100644 prb_os_io.c delete mode 100644 prb_os_io.h create mode 100644 prb_sys.c create mode 100644 prb_sys.h diff --git a/example/example b/example/example index 02b1008..cefef94 100755 Binary files a/example/example and b/example/example differ diff --git a/example/example.c b/example/example.c index 35d2b86..dc76ffb 100644 --- a/example/example.c +++ b/example/example.c @@ -19,77 +19,61 @@ typedef struct DLLNode { int main(void) { - F32 verts[] = { + F32 verts[] = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }; - Assert(v3a(1.0f).y); - U64 memory_size = Kilobytes(4); - void *some_memory = malloc(memory_size); - *(S32 *)some_memory = 69; - U64 ptr_as_int = IntFromPtr(some_memory); - void *ptr_to_memory = PtrFromInt(ptr_as_int); - printf("IntFromPtr: %lu\n", ptr_as_int); - printf("Using PtrFromInt(%lu) to get the first element of some_memory: %d\n\n", - ptr_as_int, *(S32 *)ptr_to_memory); - free(some_memory); - - printf("OffsetOfMember(SomeStruct, flags): %lld\n\n", - OffsetOfMember(SomeStruct, flags)); - - F32 range_min = 0.0f, range_max = 1.0f; - F32 clamped_number = 1.5f; - F32 another_clamped_number = -0.2f; - printf("ClampBottom(%f, %f): %f\n", range_min, - another_clamped_number, ClampBottom(another_clamped_number, range_min)); - printf("ClampTop(%f, %f): %f\n", range_max, - clamped_number, ClampTop(clamped_number, range_max)); - - printf("Clamp(%f, %f, %f): %f\n", range_min, clamped_number, range_max, - Clamp(range_min, clamped_number, range_max)); - printf("Clamp(%f, %f, %f): %f\n\n", range_min, another_clamped_number, range_max, - Clamp(range_min, another_clamped_number, range_max)); - - F32 swap_test_first = 10.0f, swap_test_second = 20.0f; - printf("Before Swap: swap_test_first = %f, swap_test_second = %f\n", + ASSERT(v3a(1.0f).y); + + printf("OFFSETOF(SomeStruct, flags): %ld\n\n", OFFSETOF(SomeStruct, flags)); + + F32 range_min = 0.0f, + range_max = 1.0f, + clamped_number = 1.5f, + another_clamped_number = -0.2f; + + printf("CLAMPBOT(%f, %f): %f\n", range_min, + another_clamped_number, CLAMPBOT(another_clamped_number, range_min)); + printf("CLAMPTOP(%f, %f): %f\n", range_max, + clamped_number, CLAMPTOP(clamped_number, range_max)); + + printf("CLAMP(%f, %f, %f): %f\n", range_min, clamped_number, range_max, + CLAMP(range_min, clamped_number, range_max)); + printf("CLAMP(%f, %f, %f): %f\n\n", range_min, another_clamped_number, range_max, + CLAMP(range_min, another_clamped_number, range_max)); + + F32 swap_test_first = 10.0f, + swap_test_second = 20.0f; + + printf("Before SWAP: swap_test_first = %f, swap_test_second = %f\n", swap_test_first, swap_test_second); - Swap(F32, swap_test_first, swap_test_second); - printf("After Swap: swap_test_first = %f, swap_test_second = %f\n\n", + SWAP(F32, swap_test_first, swap_test_second); + printf("After SWAP: swap_test_first = %f, swap_test_second = %f\n\n", swap_test_first, swap_test_second); - SomeStruct some_stuct = { 10, 20, 30 }; + SomeStruct some_stuct = { 10, 20, 30 }; printf("SomeStruct: first: %d, second: %d, flags: %d\n", some_stuct.first, some_stuct.second, some_stuct.flags); - MemoryZero(&some_stuct, sizeof(SomeStruct)); - printf("After MemoryZero(&some_stuct, sizeof(SomeStruct)):\n"); + MEM0(&some_stuct, sizeof(SomeStruct)); + printf("After MEM0(&some_stuct, sizeof(SomeStruct)):\n"); printf("some_stuct: first: %d, second: %d, flags: %d\n\n", some_stuct.first, some_stuct.second, some_stuct.flags); some_stuct = (SomeStruct){ 10, 20, 30 }; printf("some_stuct: first: %d, second: %d, flags: %d\n", some_stuct.first, some_stuct.second, some_stuct.flags); - MemoryZeroStruct(&some_stuct); - printf("After MemoryZeroStruct(&some_stuct):\n"); + MEM0STRUCT(&some_stuct); + printf("After MEM0STRUCT(&some_stuct):\n"); printf("some_stuct: first: %d, second: %d, flags: %d\n\n", some_stuct.first, some_stuct.second, some_stuct.flags); - printf("Before MemoryZeroArray:\n"); - for (S32 i = 0; i < ArrayCount(verts); ++i) - printf("%f%c", verts[i], (((i+1)%4 == 0) || - (i == ArrayCount(verts)-1)) ? '\n' : ' '); - MemoryZeroArray(verts); - printf("After:\n"); - for (S32 i = 0; i < ArrayCount(verts); ++i) - printf("%f%c", verts[i], (((i+1)%4 == 0) || - (i == ArrayCount(verts)-1)) ? '\n' : ' '); - printf("\n"); + S32 dynamic_array_length = 10; + U64 dynamic_array_size = dynamic_array_length*sizeof(S32); + F32 *dynamic_array_first = malloc(dynamic_array_size); + F32 *dynamic_array_second = malloc(dynamic_array_size); - S32 dynamic_array_length = 10; - U64 dynamic_array_size = dynamic_array_length*sizeof(S32); - F32 *dynamic_array_first = malloc(dynamic_array_size); - F32 *dynamic_array_second = malloc(dynamic_array_size); printf("first dynamic array:\n"); for (S32 i = 0; i < dynamic_array_length; ++i) { F32 *element = dynamic_array_first+i; @@ -101,15 +85,15 @@ int main(void) F32 *element = dynamic_array_second+i; printf("%f%c", *element, (i == dynamic_array_length-1) ? '\n' : ' '); } - MemoryCopy(dynamic_array_second, dynamic_array_first, dynamic_array_size); - printf("After MemoryCopy(dynamic_array_second, dynamic_array_first, dynamic_array_size):\n"); + MEMCPY(dynamic_array_second, dynamic_array_first, dynamic_array_size); + printf("After MEMCPY(dynamic_array_second, dynamic_array_first, dynamic_array_size):\n"); printf("second dynamic array:\n"); for (S32 i = 0; i < dynamic_array_length; ++i) { F32 *element = dynamic_array_second+i; printf("%f%c", *element, (i == dynamic_array_length-1) ? '\n' : ' '); } - S32 memory_match = MemoryMatch(dynamic_array_first, dynamic_array_second, dynamic_array_size); - printf("And MemoryMatch(MemoryMatch(dynamic_array_first, dynamic_array_second, dynamic_array_size): %d\n\n", memory_match); + S32 memory_match = MEMMATCH(dynamic_array_first, dynamic_array_second, dynamic_array_size); + printf("And MEMMATCH(dynamic_array_first, dynamic_array_second, dynamic_array_size): %d\n\n", memory_match); free(dynamic_array_first); free(dynamic_array_second); @@ -123,12 +107,12 @@ int main(void) for (S32 i = 0; i < list_length; ++i) { SLLNode *new = sllist+i; new->val = i; - SLLPush(sllfirst, slllast, new); + SLLPUSH(sllfirst, slllast, new); } for (SLLNode *node = sllfirst; node; node = node->next) printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); - SLLPop(sllfirst, slllast); - printf("After SLLPop(sllfirst, slllast): "); + SLLPOP(sllfirst, slllast); + printf("After SLLPOP(sllfirst, slllast): "); for (SLLNode *node = sllfirst; node; node = node->next) printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); free(sllist); @@ -142,14 +126,14 @@ int main(void) for (S32 i = 0; i < list_length; ++i) { DLLNode *new = dllist+i; new->val = i; - DLLPushBack(dllfirst, dlllast, new); + DLLPUSHBACK(dllfirst, dlllast, new); } for (DLLNode *node = dllfirst; node; node = node->next) printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); printf("Remove odd numbers: "); for (DLLNode *node = dllfirst; node; node = node->next) if (node->val % 2 == 1) - DLLRemove(dllfirst, dlllast, node); + DLLREMOVE(dllfirst, dlllast, node); for (DLLNode *node = dllfirst; node; node = node->next) printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); free(dllist); @@ -160,66 +144,91 @@ int main(void) printf("Matrices:\n"); printf("Identity:\n"); MAT4 m = MAT4_IDENTITY; - mat4print(m); - m = mat4scale(m, v3a(10.0f)); - m = mat4transl(m, v3a(1.0f)); - m = mat4transl(m, v3(0.0f, 68.0f, 0.0f)); - mat4print(m); - printf("Determinant: %f\n", mat4det(m)); - printf("Transpose:\n"); mat4print(mat4transp(m)); + printmat4(m); + m = scalemat4(m, v3a(10.0f)); + m = translmat4(m, v3a(1.0f)); + m = translmat4(m, v3(0.0f, 68.0f, 0.0f)); + printmat4(m); + printf("Determinant: %f\n", detmat4(m)); + printf("Transpose:\n"); printmat4(transpmat4(m)); printf("mat4rotate(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f)):\n"); - mat4print(mat4rotate(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f))); + printmat4(rotatemat4(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f))); + + sys_printf("Example of using sys_printf without args\n"); + sys_printf("And with some of number : %x : <- like that or string : %s : for example\n", 69, "yeah"); printf("Using Arenas:\n"); - Arena *a = arena_alloc(0); + + Arena *a; + U8 *df, + *ds; + Arena *tmpa; + + a = alloc_arena(10); + df = push_arena(a, 5); + memset(df, 5, 5); + ds = push_arena(a, 5); + memset(ds, 10, 5); + + tmpa = alloc_arena(0); + + flsprint(FLSLIT("|")); + for (S32 i = 0; i < 10; i++) + flsprint(flspushf(tmpa, "%x%s", df[i], ((i+1) % 5 == 0) ? "|" : " ")); + flsprint(FLSLIT("\n")); + + release_arena(tmpa); + release_arena(a); + + a = alloc_arena(0); SLLNode *first = 0; SLLNode *last = 0; S32 node_count = 10; for (S32 i = 0; i < node_count; ++i) { - SLLNode *new = arena_push(a, sizeof(SLLNode)); + SLLNode *new = push_arena(a, sizeof(SLLNode)); new->val = i; - SLLPush(first, last, new); + SLLPUSH(first, last, new); } for (SLLNode *node = first; node; node = node->next) printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); - arena_release(a); + release_arena(a); - a = arena_alloc(sizeof(verts)); - S32 verts_count = ArrayCount(verts); - F32 *dynamic_verts = arena_push(a, sizeof(verts)); - MemoryZero(dynamic_verts, verts_count*sizeof(F32)); + a = alloc_arena(sizeof(verts)); + S32 verts_count = ARRCNT(verts); + F32 *dynamic_verts = push_arena(a, sizeof(verts)); + MEM0(dynamic_verts, verts_count*sizeof(F32)); for (S32 i = 0; i < verts_count; ++i) { F32 *vert = dynamic_verts+i; printf("%f%c", *vert, (i == verts_count-1) ? '\n' : ' '); } - arena_release(a); + release_arena(a); printf("\n"); - Arena *str_arena = arena_alloc(0); + Arena *str_arena = alloc_arena(0); printf("Strings:\n"); char *cstr = "This is a C str\n"; - Str8 str = str8fromcstr(cstr); - str = str8chopstart(str, 10); - cstr = str8tocstr(a, str); + FLS str = flsfromcstr(cstr); + str = flschopstart(str, 10); + cstr = flstocstr(a, str); printf("%s", cstr); - str8print(str); - Str8 choped_str = str8chopend(str, 3); - str8print(choped_str); + flsprint(str); + FLS choped_str = flschopend(str, 3); + flsprint(choped_str); printf("\n"); - Str8List *list = str8list(str_arena); - str8listpush(str_arena, list, str, 0); - str8listpush(str_arena, list, str8fromcstr("test"), 0); - str8listpush(str_arena, list, str8fromcstr("and this is also a test\n"), 0); - str8listpush(str_arena, list, str8fromcstr("Kinda works!"), 1); - printf("Str8List: "); - str8listprint(list); - arena_release(str_arena); - str8listprint(list); - - str_arena = arena_alloc(Kilobytes(10)); - memory_size = 512; + FLSList *list = flslist(str_arena); + flslistpush(str_arena, list, str, 0); + flslistpush(str_arena, list, flsfromcstr("test"), 0); + flslistpush(str_arena, list, flsfromcstr("and this is also a test\n"), 0); + flslistpush(str_arena, list, flsfromcstr("Kinda works!"), 1); + printf("FLSList: "); + flslistprint(list); + release_arena(str_arena); + flslistprint(list); + + str_arena = alloc_arena(KB(10)); + U32 memory_size = 512; U32 count = memory_size/sizeof(S32); - S32 *some = arena_push(str_arena, memory_size); + S32 *some = push_arena(str_arena, memory_size); printf("str_arena(size): %ld\nMemory:\n", str_arena->cap); for (S32 i = 0; i < count; i++) { S32 *e = some+i; @@ -230,7 +239,7 @@ int main(void) printf("Remaining memory: %lu\n", str_arena->cap-str_arena->used); - arena_pop(str_arena, memory_size); + pop_arena(str_arena, memory_size); printf("After arena pop\n"); printf("Memory used: %lu\n", str_arena->used); @@ -239,26 +248,29 @@ int main(void) printf("some ptr is %s\n", some ? "not null" : "null"); printf("str_arena(size): %lu\nMemory:\n", str_arena->cap); - Str8 new_str = str8pushf(str_arena, "Test of the formatted string: %d\n", 69); - str8print(new_str); + FLS new_str = flspushf(str_arena, "Test of the formatted string: %d\n", 69); + flsprint(new_str); - list = str8list(str_arena); - str8listpushf(str_arena, list, 0, "This is a list %d", 34); - str8listpushf(str_arena, list, 0, " of formatted strings: %d", 35); - str8listpushf(str_arena, list, 1, "And you can push to the start"); + list = flslist(str_arena); + flslistpushf(str_arena, list, 0, "This is a list %d", 34); + flslistpushf(str_arena, list, 0, " of formatted strings: %d", 35); + flslistpushf(str_arena, list, 1, "And you can push to the start"); /* TODO(pryazha): Think about simpler way of using string literals. * Maybe you don't need to, I dunno */ - str8listpushf(str_arena, list, 1, "The most interesting part is that you can include the whole string like that: %*s\n", - str8expand(str8lit("Absolutely random string with absolutely random content (or not)."))); - str8listprint(list); + flslistpushf(str_arena, list, 1, "The most interesting part is that you can include the whole string like that: %*s\n", + FLSEXP(FLSLIT("Absolutely random string with absolutely random content (or not)."))); + flslistprint(list); - arena_release(str_arena); + release_arena(str_arena); - a = arena_alloc(Megabytes(1)); - str8print(str8lit("build.sh content:\n")); - Str8 file_content = str8_read_entire_file(a, str8lit("build.sh")); - str8print(file_content); - arena_release(a); + a = alloc_arena(MB(1)); + flsprint(FLSLIT("build.sh content:\n")); + FLS file_content = fls_read_entire_file(a, FLSLIT("build.sh")); + if (file_content.p) + flsprint(file_content); + else + flsprint(FLSLIT("failed to read build.sh\n")); + release_arena(a); return(0); } diff --git a/prb.h b/prb.h index 65f3244..f09df2d 100644 --- a/prb.h +++ b/prb.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "prb_macros.h" #include "prb_types.h" @@ -14,13 +15,13 @@ #include "prb_math.h" #include "prb_math.c" -#include "prb_memory.h" -#include "prb_memory.c" +#include "prb_arena.h" +#include "prb_arena.c" #include "prb_string.h" #include "prb_string.c" -#include "prb_os_io.h" -#include "prb_os_io.c" +#include "prb_sys.h" +#include "prb_sys.c" #endif /* PRB_H */ diff --git a/prb_arena.c b/prb_arena.c new file mode 100644 index 0000000..1938dec --- /dev/null +++ b/prb_arena.c @@ -0,0 +1,48 @@ +Arena *alloc_arena(U64 cap) +{ + Arena *a; + + /* TODO(pryazha): Use OS specific memory allocator + * (like VirtualAlloc on Windows or mmap on Linux) + */ + a = malloc(sizeof(Arena)); + ASSERT(a); + + if (!cap) + cap = PRB_DEFAULT_ALLOC_SIZE; + + a->mem = malloc(cap); + ASSERT(a->mem); + a->cap = cap; + a->used = 0; + + return a; +} + +void release_arena(Arena *a) +{ + free(a->mem); + MEM0STRUCT(a); + free(a); +} + +void *push_arena(Arena *a, U64 size) +{ + ASSERT(a); + ASSERT(a->used+size <= a->cap); + + void *r; + + r = a->mem+a->used; + a->used += size; + + return r; +} + +void pop_arena(Arena *a, U64 size) +{ + ASSERT(a); + U64 clamped; + clamped = CLAMPTOP(size, a->used); + a->used = a->used-clamped; +} diff --git a/prb_arena.h b/prb_arena.h new file mode 100644 index 0000000..2d3453f --- /dev/null +++ b/prb_arena.h @@ -0,0 +1,6 @@ +#define PRB_DEFAULT_ALLOC_SIZE KB(4) + +Arena *alloc_arena(U64 cap); +void release_arena(Arena *a); +void *push_arena(Arena *a, U64 size); +void pop_arena(Arena *a, U64 size); diff --git a/prb_macros.h b/prb_macros.h index a9a465d..5a38ae4 100644 --- a/prb_macros.h +++ b/prb_macros.h @@ -1,61 +1,55 @@ #ifndef PRB_MACROS_H #define PRB_MACROS_H -#define Assert(e) if (!(e)) { *(int *)0 = 0; } +#define ASSERT(E) if (!(E)) { *(int *)0 = 0; } -#define ArrayCount(a) (sizeof(a)/sizeof(*(a))) +#define ARRCNT(A) (sizeof(A)/sizeof(*(A))) -#define IntFromPtr(p) (unsigned long long)((char *)(p)-(char *)0) -#define PtrFromInt(n) (void *)((char *)0+(n)) +#define OFFSETOF(T, E) ((size_t)(&(((T *)(0))->E))) -#define Member(t, m) (((t *)0)->m) -#define OffsetOfMember(t, m) (IntFromPtr(&Member(t, m))) +#define MIN(A, B) (((A) < (B)) ? (A) : (B)) +#define MAX(A, B) (((A) > (B)) ? (A) : (B)) -#define Min(a, b) (((a) < (b)) ? (a) : (b)) -#define Max(a, b) (((a) > (b)) ? (a) : (b)) +#define CLAMP(A, X, B) (((X) < (A)) ? (A) : \ + (((X) > (B)) ? (B) : (X))) +#define CLAMPTOP(A, B) MIN(A, B) +#define CLAMPBOT(A, B) MAX(A, B) -#define Clamp(a, x, b) (((x) < (a)) ? (a) : \ - (((x) > (b)) ? (b) : (x))) -#define ClampTop(a, b) Min(a, b) -#define ClampBottom(a, b) Max(a, b) - -#define Swap(t, a, b) { t temp = a; a = b; b = temp; } +#define SWAP(T, A, B) { T tmp = A; A = B; B = tmp; } /* NOTE(pryazha): Memory */ -#define MemoryZero(p, n) memset((p), 0, (n)) -#define MemoryZeroStruct(p) MemoryZero((p), sizeof(*(p))) -#define MemoryZeroArray(p) MemoryZero((p), sizeof(p)) - -#define MemoryCopy(d, s, n) memmove((d), (s), (n)) -#define MemoryCopyStruct(d, s) MemoryCopy((d), (s), Min(sizeof(*(d)), sizeof(*(s)))) -#define MemoryCopyArray(d, s) MemoryCopy((d), (s), Min(sizeof(d), sizeof(s))) - -#define MemoryMatch(a, b, n) (memcmp((a), (b), (n)) == 0) - -#define Kilobytes(n) n*1024 -#define Megabytes(n) Kilobytes(n)*1024 -#define Gigabytes(n) Megabytes(n)*1024 - -/* NOTE(pryazha): Linked lists */ -#define SLLPush(f, l, n) \ - ((f) == 0 ? \ - ((f) = (l) = (n), (n)->next = 0) : \ - ((l)->next = (n), (l) = (n), (n)->next = 0)) -#define SLLPop(f, l) \ - ((f) == (l) ? \ - ((f) = (l) = 0) : \ - ((f) = (f)->next)) - -#define DLLPushBack_NP(f, l, n, next, prev) \ - ((f) == 0 ? \ - ((f) = (l) = (n), (n)->next = (n)->prev = 0) : \ - ((n)->prev = (l), (l)->next = (n), (l) = (n), (n)->next = 0)) -#define DLLPushBack(f, l, n) DLLPushBack_NP(f, l, n, next, prev) -#define DLLPushFront(f, l, n) DLLPushBack_NP(l, f, n, prev, next) -#define DLLRemove(f, l, n) \ - ((f) == (n) ? \ - ((f) == (l) ? (f) = (l) = 0 : ((f) = (f)->next, (f)->prev = 0)) : \ - ((l) == (n) ? ((l) = (l)->prev, (l)->next = 0) : \ - ((n)->next->prev = (n)->prev, (n)->prev->next = (n)->next))) +#define MEM0(P, N) memset((P), 0, (N)) +#define MEM0STRUCT(P) MEM0((P), sizeof(*(P))) + +#define MEMCPY(D, S, N) memmove((D), (S), (N)) + +#define MEMMATCH(A, B, N) (memcmp((A), (B), (N)) == 0) + +#define KB(N) N*1024 +#define MB(N) KB(N)*1024 +#define GB(N) MB(N)*1024 + +/* NOTE(pryazha): Singly-linked list */ +#define SLLPUSH(F, L, N) \ + ((F) == 0 ? \ + ((F) = (L) = (N), (N)->next = 0) : \ + ((L)->next = (N), (L) = (N), (N)->next = 0)) +#define SLLPOP(F, L) \ + ((F) == (L) ? \ + ((F) = (L) = 0) : \ + ((F) = (F)->next)) + +/* NOTE(pryazha): Doubly-linked list */ +#define DLLPUSHBACK_NP(F, L, N, next, prev) \ + ((F) == 0 ? \ + ((F) = (L) = (N), (N)->next = (N)->prev = 0) : \ + ((N)->prev = (L), (L)->next = (N), (L) = (N), (N)->next = 0)) +#define DLLPUSHBACK(F, L, N) DLLPUSHBACK_NP(F, L, N, next, prev) +#define DLLPUSHFRONT(F, L, N) DLLPUSHBACK_NP(L, F, N, prev, next) +#define DLLREMOVE(F, L, N) \ + ((F) == (N) ? \ + ((F) == (L) ? (F) = (L) = 0 : ((F) = (F)->next, (F)->prev = 0)) : \ + ((L) == (N) ? ((L) = (L)->prev, (L)->next = 0) : \ + ((N)->next->prev = (N)->prev, (N)->prev->next = (N)->next))) #endif /* PRB_MACROS_H */ diff --git a/prb_math.c b/prb_math.c index bbc3161..2efb091 100644 --- a/prb_math.c +++ b/prb_math.c @@ -42,76 +42,76 @@ V2 v2a(F32 x) return r; } -V2 v2inv(V2 a) +V2 invv2(V2 a) { V2 r; r = v2(-a.x, -a.y); return r; } -V2 v2add(V2 a, V2 b) +V2 addv2(V2 a, V2 b) { V2 r; r = v2(a.x+b.x, a.y+b.y); return r; } -V2 v2sub(V2 a, V2 b) +V2 subv2(V2 a, V2 b) { V2 r; r = v2(a.x-b.x, a.y-b.y); return r; } -V2 v2scalef(V2 a, F32 s) +V2 scalefv2(V2 a, F32 s) { V2 r; r = v2(a.x*s, a.y*s); return r; } -V2 v2scale(V2 a, V2 s) +V2 scalev2(V2 a, V2 s) { V2 r; r = v2(a.x*s.x, a.y*s.y); return r; } -F32 v2dot(V2 a, V2 b) +F32 dotv2(V2 a, V2 b) { F32 r; r = a.x*b.x+a.y*b.y; return r; } -F32 v2len2(V2 a) +F32 len2v2(V2 a) { F32 r; - r = v2dot(a, a); + r = dotv2(a, a); return r; } -F32 v2len(V2 a) +F32 lenv2(V2 a) { F32 r; - r = f32sqrt(v2len2(a)); + r = f32sqrt(len2v2(a)); return r; } -V2 v2norm(V2 a) +V2 normv2(V2 a) { F32 len; V2 r; r = V2_ZERO; - len = v2len(a); + len = lenv2(a); if (len) r = v2(a.x/len, a.y/len); return r; } -void v2print(V2 a) +void printv2(V2 a) { fprintf(stdout, "{%f, %f}T\n", a.x, a.y); } @@ -146,49 +146,49 @@ V3 v3fromv4(V4 a) return r; } -V3 v3inv(V3 a) +V3 invv3(V3 a) { V3 r; r = v3(-a.x, -a.y, -a.z); return r; } -V3 v3add(V3 a, V3 b) +V3 addv3(V3 a, V3 b) { V3 r; r = v3(a.x+b.x, a.y+b.y, a.z+b.z); return r; } -V3 v3sub(V3 a, V3 b) +V3 subv3(V3 a, V3 b) { V3 r; r = v3(a.x-b.x, a.y-b.y, a.z-b.z); return r; } -V3 v3scalef(V3 a, F32 s) +V3 scalefv3(V3 a, F32 s) { V3 r; r = v3(a.x*s, a.y*s, a.z*s); return r; } -V3 v3scale(V3 a, V3 s) +V3 scalev3(V3 a, V3 s) { V3 r; r = v3(a.x*s.x, a.y*s.y, a.z*s.z); return r; } -F32 v3dot(V3 a, V3 b) +F32 dotv3(V3 a, V3 b) { F32 r; r = a.x*b.x + a.y*b.y + a.z*b.z; return r; } -V3 v3cross(V3 left, V3 right) +V3 crossv3(V3 left, V3 right) { V3 r; r = v3((left.y*right.z-right.y*left.z), @@ -197,34 +197,34 @@ V3 v3cross(V3 left, V3 right) return r; } -F32 v3len2(V3 a) +F32 len2v3(V3 a) { F32 r; - r = v3dot(a, a); + r = dotv3(a, a); return r; } -F32 v3len(V3 a) +F32 lenv3(V3 a) { F32 r; - r = f32sqrt(v3len2(a)); + r = f32sqrt(len2v3(a)); return r; } -V3 v3norm(V3 a) +V3 normv3(V3 a) { F32 len; V3 r; r = V3_ZERO; - len = v3len(a); + len = lenv3(a); if (len) r = v3(a.x/len, a.y/len, a.z/len); return r; } -void v3print(V3 a) +void printv3(V3 a) { fprintf(stdout, "{%f, %f, %f}T\n", a.x, a.y, a.z); } @@ -253,82 +253,82 @@ V4 v4fromv3(V3 a) return r; } -V4 v4inv(V4 a) +V4 invv4(V4 a) { V4 r; r = v4(-a.x, -a.y, -a.z, -a.w); return r; } -V4 v4add(V4 a, V4 b) +V4 addv4(V4 a, V4 b) { V4 r; r = v4(a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w); return r; } -V4 v4sub(V4 a, V4 b) +V4 subv4(V4 a, V4 b) { V4 r; r = v4(a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w); return r; } -V4 v4scalef(V4 a, F32 s) +V4 scalefv4(V4 a, F32 s) { V4 r; r = v4(a.x*s, a.y*s, a.z*s, a.w*s); return r; } -V4 v4scale(V4 a, V4 s) +V4 scalev4(V4 a, V4 s) { V4 r; r = v4(a.x*s.x, a.y*s.y, a.z*s.z, a.w*s.w); return r; } -F32 v4dot(V4 a, V4 b) +F32 dotv4(V4 a, V4 b) { F32 r; r = a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w; return r; } -F32 v4len2(V4 a) +F32 len2v4(V4 a) { F32 r; - r = v4dot(a, a); + r = dotv4(a, a); return r; } -F32 v4len(V4 a) +F32 lenv4(V4 a) { F32 r; - r = f32sqrt(v4len2(a)); + r = f32sqrt(len2v4(a)); return r; } -V4 v4norm(V4 a) +V4 normv4(V4 a) { F32 len; V4 r; r = V4_ZERO; - len = v4len(a); + len = lenv4(a); if (len) r = v4(a.x/len, a.y/len, a.z/len, a.w/len); return r; } -void v4print(V4 a) +void printv4(V4 a) { fprintf(stdout, "{%f, %f, %f, %f}T\n", a.x, a.y, a.z, a.w); } /* NOTE(pryazha): Matrices */ -F32 mat4det(MAT4 m) +F32 detmat4(MAT4 m) { F32 r, m00minor, m01minor, m02minor, m03minor; @@ -354,25 +354,25 @@ F32 mat4det(MAT4 m) return r; } -MAT4 mat4transp(MAT4 m) +MAT4 transpmat4(MAT4 m) { MAT4 r; r = m; - Swap(F32, r.m0.y, r.m1.x); - Swap(F32, r.m0.z, r.m2.x); - Swap(F32, r.m0.w, r.m3.x); + SWAP(F32, r.m0.y, r.m1.x); + SWAP(F32, r.m0.z, r.m2.x); + SWAP(F32, r.m0.w, r.m3.x); - Swap(F32, r.m1.z, r.m2.y); - Swap(F32, r.m1.w, r.m3.y); + SWAP(F32, r.m1.z, r.m2.y); + SWAP(F32, r.m1.w, r.m3.y); - Swap(F32, r.m2.w, r.m3.z); + SWAP(F32, r.m2.w, r.m3.z); return r; } -MAT4 mat4mul(MAT4 left, MAT4 right) +MAT4 mulmat4(MAT4 left, MAT4 right) { F32 l00, l01, l02, l03; F32 l10, l11, l12, l13; @@ -419,7 +419,7 @@ MAT4 mat4mul(MAT4 left, MAT4 right) return r; } -MAT4 mat4transl(MAT4 m, V3 v) +MAT4 translmat4(MAT4 m, V3 v) { MAT4 t, r; @@ -427,12 +427,12 @@ MAT4 mat4transl(MAT4 m, V3 v) t.m3.x = v.x; t.m3.y = v.y; t.m3.z = v.z; - r = mat4mul(t, m); + r = mulmat4(t, m); return r; } -MAT4 mat4scale(MAT4 m, V3 v) +MAT4 scalemat4(MAT4 m, V3 v) { MAT4 s, r; @@ -440,12 +440,12 @@ MAT4 mat4scale(MAT4 m, V3 v) s.m0.x = v.x; s.m1.y = v.y; s.m2.z = v.z; - r = mat4mul(s, m); + r = mulmat4(s, m); return r; } -MAT4 mat4_change_basis(V3 x, V3 y, V3 z) +MAT4 rotateaxismat4(V3 x, V3 y, V3 z) { MAT4 r; @@ -463,7 +463,7 @@ MAT4 mat4_change_basis(V3 x, V3 y, V3 z) * | 0 cx -sx |*| 0 1 0 |*| sz cz 0 |=| sx*sy*cz+cx*sz -sx*sy*sz+cx*cz -sx*cy | * | 0 sx cx | | -sy 0 cy | | 0 0 1 | | -cx*sy*cz+sx*sz cx*sy*sz+sx*cz cx*cy | */ -MAT4 mat4rotate(MAT4 m, V3 angles) +MAT4 rotatemat4(MAT4 m, V3 angles) { F32 angle, cx, sx, cy, sy, cz, sz; MAT4 rotate, r; @@ -482,14 +482,14 @@ MAT4 mat4rotate(MAT4 m, V3 angles) newx = v3(cy*cz, sx*sy*cz+cx*sz, -cx*sy*cz+sx*sz); newy = v3(-cy*sz, -sx*sy*sz+cx*cz, cx*sy*sz+sx*cz); newz = v3(sy, -sx*cy, cx*cy); - rotate = mat4_change_basis(newx, newy, newz); + rotate = rotateaxismat4(newx, newy, newz); - r = mat4mul(rotate, m); + r = mulmat4(rotate, m); return r; } -V4 mat4v4mul(MAT4 m, V4 v) +V4 mulmat4v4(MAT4 m, V4 v) { V4 r; @@ -501,7 +501,7 @@ V4 mat4v4mul(MAT4 m, V4 v) return r; } -void mat4print(MAT4 m) +void printmat4(MAT4 m) { fprintf(stdout, "| %.4f %.4f %.4f %.4f |\n", m.m0.x, m.m1.x, m.m2.x, m.m3.x); fprintf(stdout, "| %.4f %.4f %.4f %.4f |\n", m.m0.y, m.m1.y, m.m2.y, m.m3.y); diff --git a/prb_math.h b/prb_math.h index f569100..52c6289 100644 --- a/prb_math.h +++ b/prb_math.h @@ -2,10 +2,10 @@ #define PRB_MATH_H /* NOTE(pryazha): Numeric */ -#define F32_PI 3.14159265359f +#define F32PI 3.14159265359f -#define DEG2RAD F32_PI/180.0f -#define RAD2DEG 182.0f/F32_PI +#define DEG2RAD F32PI/180.0f +#define RAD2DEG 182.0f/F32PI F32 f32sin(F32 a); F32 f32cos(F32 a); @@ -15,57 +15,57 @@ F32 f32sqrt(F32 a); /* NOTE(pryazha): Vectors */ V2 v2(F32 x, F32 y); V2 v2a(F32 x); -V2 v2inv(V2 a); -V2 v2add(V2 a, V2 b); -V2 v2sub(V2 a, V2 b); -V2 v2scalef(V2 a, F32 s); -V2 v2scale(V2 a, V2 s); -F32 v2dot(V2 a, V2 b); -F32 v2len2(V2 a); -F32 v2len(V2 a); -V2 v2norm(V2 a); -void v2print(V2 a); +V2 invv2(V2 a); +V2 addv2(V2 a, V2 b); +V2 subv2(V2 a, V2 b); +V2 scalefv2(V2 a, F32 s); +V2 scalev2(V2 a, V2 s); +F32 dotv2(V2 a, V2 b); +F32 len2v2(V2 a); +F32 lenv2(V2 a); +V2 normv2(V2 a); +void printv2(V2 a); V3 v3(F32 x, F32 y, F32 z); V3 v3a(F32 x); V3 v3fromv2(V2 a); V3 v3fromv4(V4 a); -V3 v3inv(V3 a); -V3 v3add(V3 a, V3 b); -V3 v3sub(V3 a, V3 b); -V3 v3scalef(V3 a, F32 s); -V3 v3scale(V3 a, V3 s); -F32 v3dot(V3 a, V3 b); -V3 v3cross(V3 l, V3 r); -F32 v3len2(V3 a); -F32 v3len(V3 a); -V3 v3norm(V3 a); -void v3print(V3 a); +V3 invv3(V3 a); +V3 addv3(V3 a, V3 b); +V3 subv3(V3 a, V3 b); +V3 scalefv3(V3 a, F32 s); +V3 scalev3(V3 a, V3 s); +F32 dotv3(V3 a, V3 b); +V3 crossv3(V3 l, V3 r); +F32 len2v3(V3 a); +F32 lenv3(V3 a); +V3 normv3(V3 a); +void printv3(V3 a); V4 v4(F32 x, F32 y, F32 z, F32 w); V4 v4a(F32 x); V4 v4fromv3(V3 a); -V4 v4inv(V4 a); -V4 v4add(V4 a, V4 b); -V4 v4sub(V4 a, V4 b); -V4 v4scalef(V4 a, F32 s); -V4 v4scale(V4 a, V4 s); -F32 v4dot(V4 a, V4 b); -F32 v4len2(V4 a); -F32 v4len(V4 a); -V4 v4norm(V4 a); -void v4print(V4 a); +V4 invv4(V4 a); +V4 addv4(V4 a, V4 b); +V4 subv4(V4 a, V4 b); +V4 scalefv4(V4 a, F32 s); +V4 scalev4(V4 a, V4 s); +F32 dotv4(V4 a, V4 b); +F32 len2v4(V4 a); +F32 lenv4(V4 a); +V4 normv4(V4 a); +void printv4(V4 a); /* NOTE(pryazha): Matrices */ -F32 mat4det(MAT4 m); -MAT4 mat4transp(MAT4 m); -MAT4 mat4mul(MAT4 left, MAT4 right); -MAT4 mat4transl(MAT4 m, V3 v); -MAT4 mat4scale(MAT4 m, V3 v); -MAT4 mat4_change_basis(V3 x, V3 y, V3 z); +F32 detmat4(MAT4 m); +MAT4 transpmat4(MAT4 m); +MAT4 mulmat4(MAT4 left, MAT4 right); +MAT4 translmat4(MAT4 m, V3 v); +MAT4 scalemat4(MAT4 m, V3 v); +MAT4 rotateaxismat4(V3 x, V3 y, V3 z); /* NOTE(pryazha): Angles in degrees */ -MAT4 mat4rotate(MAT4 m, V3 angles); -V4 mat4v4mul(MAT4 m, V4 v); -void mat4print(MAT4 m); +MAT4 rotatemat4(MAT4 m, V3 angles); +V4 mulmat4v4(MAT4 m, V4 v); +void printmat4(MAT4 m); #endif /* PRB_MATH_H */ diff --git a/prb_memory.c b/prb_memory.c deleted file mode 100644 index a160465..0000000 --- a/prb_memory.c +++ /dev/null @@ -1,42 +0,0 @@ -Arena *arena_alloc(U64 cap) -{ - Arena *arena = 0; - - /* TODO(pryazha): Use OS specific memory allocator (like VirtualAlloc on Windows or mmap on Linux) */ - arena = malloc(sizeof(Arena)); - Assert(arena); - - if (!cap) - cap = DEFAULT_ALLOC_SIZE; - - arena->mem = malloc(cap); - Assert(arena->mem); - arena->cap = cap; - arena->used = 0; - - return arena; -} - -void arena_release(Arena *arena) -{ - free(arena->mem); - arena->mem = 0; - arena->cap = 0; - arena->used = 0; - free(arena); -} - -void *arena_push(Arena *arena, U64 size) -{ - Assert(arena->used+size <= arena->cap); - void *r = arena->mem+arena->used; - arena->used += size; - return r; -} - -void arena_pop(Arena *arena, U64 amount) -{ - Assert(arena); - U64 clamped = ClampTop(amount, arena->used); - arena->used = arena->used-clamped; -} diff --git a/prb_memory.h b/prb_memory.h deleted file mode 100644 index 40a00dc..0000000 --- a/prb_memory.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef PRB_MEMORY_H -#define PRB_MEMORY_H - -#define DEFAULT_ALLOC_SIZE Kilobytes(4) - -Arena *arena_alloc(U64 cap); -void arena_release(Arena *arena); -void *arena_push(Arena *arena, U64 size); -void arena_pop(Arena *arena, U64 amount); - -#endif /* PRB_MEMORY_H */ diff --git a/prb_os_io.c b/prb_os_io.c deleted file mode 100644 index 3ef4839..0000000 --- a/prb_os_io.c +++ /dev/null @@ -1,31 +0,0 @@ -Str8 str8_read_entire_file(Arena *arena, Str8 filename) -{ - /* TODO(pryazha): Make it crossplatform */ - Assert(filename.ptr); - Assert(filename.length); - - Str8 result = {0}; - Arena *tmp = arena_alloc(0); - - char *cfilename = str8tocstr(tmp, filename); - - FILE *f = fopen(cfilename, "rb"); - if (!f) - return result; - - fseek(f, 0, SEEK_END); - U64 size = ftell(f); - rewind(f); - - U8 *mem = arena_push(arena, size+1); - fread(mem, size, 1, f); - fclose(f); - - mem[size] = 0; - - result = str8(mem, size); - - arena_release(tmp); - - return result; -} diff --git a/prb_os_io.h b/prb_os_io.h deleted file mode 100644 index 7138dec..0000000 --- a/prb_os_io.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef PRB_OS_IO_H -#define PRB_OS_IO_H - -Str8 str8_read_entire_file(Arena *arena, Str8 filename); - -Str8 get_cwd(void); - -#endif /* PRB_OS_IO_H */ diff --git a/prb_string.c b/prb_string.c index bb235df..06f4ed8 100644 --- a/prb_string.c +++ b/prb_string.c @@ -1,153 +1,170 @@ -Str8 str8(U8 *ptr, U64 length) +/* NOTE(pryazha): Null-terminated strings */ + +/* TODO(pryazha): Move to another file specific to OS you're in */ +void sys_vprintf(const char *fmt, va_list args) { - Assert(ptr); + vprintf(fmt, args); +} - Str8 r; +void sys_printf(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + sys_vprintf(fmt, args); + va_end(args); +} - r.ptr = ptr; - r.length = length; +/* NOTE(pryazha): Fixed-length strings */ +FLS fls(U8 *p, U64 len) +{ + FLS r; + r.p = p; + r.len = len; return r; } -Str8 str8range(U8 *start, U8 *end) +FLS flsfromcstr(const char *cstr) { - Str8 r; + U8 *p; + FLS r; - r.ptr = start; - r.length = end-start; + p = (U8 *)cstr; + for (; *p; ++p); + r = flsrange((U8 *)cstr, p); return r; } -Str8 str8fromcstr(char *cstr) +char *flstocstr(Arena *a, FLS s) { - U8 *ptr; - Str8 r; + U64 len; + char *cstr; - ptr = (U8 *)cstr; - for (; *ptr; ++ptr); - r = str8range((U8 *)cstr, ptr); + len = s.len+1; + cstr = push_arena(a, len*sizeof(U8)); + MEMCPY(cstr, s.p, len*sizeof(U8)); + *(cstr+s.len) = 0; - return r; + return cstr; } -char *str8tocstr(Arena *arena, Str8 s) +FLS flsrange(U8 *st_arenat, U8 *end) { - U64 length; - char *r; - - length = s.length+1; - r = arena_push(arena, length*sizeof(U8)); - MemoryCopy(r, s.ptr, length*sizeof(U8)); - *(r+s.length) = 0; - + FLS r; + r.p = st_arenat; + r.len = end-st_arenat; return r; } -Str8 str8chopend(Str8 s, U64 count) +FLS flschopend(FLS s, U64 cnt) { - U64 length; - Str8 r; + U64 len; + FLS r; - length = s.length-ClampTop(count, s.length); - r = str8(s.ptr, length); + len = s.len-CLAMPTOP(cnt, s.len); + r = fls(s.p, len); return r; } -Str8 str8chopstart(Str8 s, U64 count) +FLS flschopstart(FLS s, U64 cnt) { - U64 clamped, length; - Str8 r; + U64 clamped, len; + FLS r; - clamped = ClampTop(count, s.length); - length = s.length-ClampTop(count, s.length); - r = str8(s.ptr+clamped, length); + clamped = CLAMPTOP(cnt, s.len); + len = s.len-CLAMPTOP(cnt, s.len); + r = fls(s.p+clamped, len); return r; } -void str8print(Str8 s) +FLS flspushfv(Arena *a, const char *fmt, va_list args) { - for (U64 i = 0; i < s.length; ++i) - printf("%c", (char)(*(s.ptr+i))); -} + ASSERT(a); -Str8List *str8list(Arena *arena) -{ - Str8List *list; - list = arena_push(arena, sizeof(Str8List)); - MemoryZero(list, sizeof(Str8List)); - return list; + char tmp[1024]; + S32 n; + FLS r; + + n = vsnprintf(tmp, 1024, fmt, args); + + MEM0(&r, sizeof(FLS)); + + if (n > 0) { + r.p = push_arena(a, n); + MEMCPY(r.p, tmp, n); + r.len = n; + } + + return r; } -void str8listpush(Arena *arena, Str8List *list, Str8 str, B32 to_front) +FLS flspushf(Arena *a, const char *fmt, ...) { - Assert(arena && list); + va_list args; + FLS r; - Str8Node *n; + va_start(args, fmt); + r = flspushfv(a, fmt, args); + va_end(args); - n = arena_push(arena, sizeof(Str8Node)); - n->str = str; - if (to_front) - DLLPushFront(list->first, list->last, n); - else - DLLPushBack(list->first, list->last, n); - list->total_length += str.length; - list->node_count++; + return r; } -void str8listprint(Str8List *list) +void flsprint(FLS s) { - Str8Node *n; - for (n = list->first; n; n = n->next) - str8print(n->str); + ASSERT(s.p); + ASSERT(s.len); + + fwrite(s.p, sizeof(U8), s.len, stdout); } -Str8 str8pushfv(Arena *arena, char *fmt, va_list args) + +FLSList *flslist(Arena *a) { - va_list args2; - Str8 r; - S32 buf_size, n; - U8 *buf; + FLSList *l; - va_copy(args2, args); + l = push_arena(a, sizeof(FLSList)); + MEM0(l, sizeof(FLSList)); - buf_size = 1024; - buf = arena_push(arena, buf_size); + return l; +} - n = vsnprintf((char *)buf, buf_size, fmt, args); - va_end(args2); +void flslistpush(Arena *a, FLSList *l, FLS s, B32 front) +{ + ASSERT(a); + ASSERT(l); - MemoryZero(&r, sizeof(Str8)); + FLSNode *n; - if (n > 0) - r = str8(buf, n); + n = push_arena(a, sizeof(FLSNode)); + n->str = s; - return r; + if (front) + DLLPUSHFRONT(l->first, l->last, n); + else + DLLPUSHBACK(l->first, l->last, n); + l->len += s.len; + l->ncnt++; } -Str8 str8pushf(Arena *arena, char *fmt, ...) +void flslistpushf(Arena *a, FLSList *l, B32 front, const char *fmt, ...) { va_list args; - Str8 r; + FLS str; va_start(args, fmt); - r = str8pushfv(arena, fmt, args); + str = flspushfv(a, fmt, args); va_end(args); - return r; + flslistpush(a, l, str, front); } -void str8listpushf(Arena *arena, Str8List *list, B32 to_front, char *fmt, ...) +void flslistprint(FLSList *l) { - va_list args; - Str8 str; - - va_start(args, fmt); - str = str8pushfv(arena, fmt, args); - va_end(args); - - str8listpush(arena, list, str, to_front); + FLSNode *n; + for (n = l->first; n; n = n->next) + flsprint(n->str); } diff --git a/prb_string.h b/prb_string.h index 49c7d24..7df8246 100644 --- a/prb_string.h +++ b/prb_string.h @@ -1,23 +1,40 @@ #ifndef PRB_STRING_H #define PRB_STRING_H -#define str8lit(s) str8((U8 *)(s), sizeof(s)-1) -#define str8expand(s) (int)((s).length), ((s).ptr) - -Str8 str8(U8 *ptr, U64 length); -Str8 str8range(U8 *start, U8 *end); -Str8 str8fromcstr(char *cstr); -char *str8tocstr(Arena *arena, Str8 s); -Str8 str8chopend(Str8 s, U64 count); -Str8 str8chopstart(Str8 s, U64 count); -void str8print(Str8 s); - -Str8List *str8list(Arena *arena); -void str8listpush(Arena *arena, Str8List *list, Str8 str, B32 to_front); -void str8listprint(Str8List *list); - -Str8 str8pushfv(Arena *arena, char *fmt, va_list args); -Str8 str8pushf(Arena *arena, char *fmt, ...); -void str8listpushf(Arena *arena, Str8List *list, B32 to_front, char *fmt, ...); +/* NOTE(pryazha): I don't know wheter it's a good idea + * to keep regular and fixed-length strings together, but + * seems reasonable at the time. + */ + +/* NOTE(pryazha): Null-terminated strings */ + +void sys_vprintf(const char *fmt, va_list args); +void sys_printf(const char *fmt, ...); + +/* NOTE(pryazha): Fixed-length strings */ + +#define FLSLIT(s) fls((U8 *)(s), sizeof(s)-1) +#define FLSEXP(s) (int)((s).len), ((s).p) + +FLS fls(U8 *p, U64 len); + +FLS flsfromcstr(const char *cstr); +char *flstocstr(Arena *a, FLS s); + +FLS flsrange(U8 *start, U8 *end); +FLS flschopend(FLS s, U64 cnt); +FLS flschopstart(FLS s, U64 cnt); + +FLS flspushfv(Arena *a, const char *fmt, va_list args); +FLS flspushf(Arena *a, const char *fmt, ...); + +void flsprint(FLS s); + +FLSList *flslist(Arena *a); + +void flslistpush(Arena *a, FLSList *l, FLS s, B32 front); +void flslistpushf(Arena *a, FLSList *l, B32 front, const char *fmt, ...); + +void flslistprint(FLSList *l); #endif /* PRB_STRING_H */ diff --git a/prb_sys.c b/prb_sys.c new file mode 100644 index 0000000..3747f18 --- /dev/null +++ b/prb_sys.c @@ -0,0 +1,52 @@ +/* TODO(pryazha): OS specific */ +FLS sys_read_entire_file_fls(Arena *a, const char *fname) +{ + FILE *f; + FLS r; + S32 len; + + MEM0STRUCT(&r); + + f = fopen(fname, "rb"); + if (!f) + return r; + + if (fseek(f, 0, SEEK_END) == -1) + goto error; + if ((len = ftell(f)) == -1) + goto error; + rewind(f); + + r.p = push_arena(a, len+1); + if (!fread(r.p, len, 1, f)) + goto error; + r.p[len] = 0; + r.len = len+1; + + fclose(f); + + return r; +error: + fclose(f); + return r; +} + +#define PRGE_MAX_PATH 256 + +char *sys_getbindir(Arena *a) +{ + U64 size; + char *path; + ssize_t len; + + size = PRGE_MAX_PATH*sizeof(char); + path = push_arena(a, size); + + len = readlink("/proc/self/exe", path, size-1); + if (len <= 0) + return path; + + path[len] = 0; + + return path; +} diff --git a/prb_sys.h b/prb_sys.h new file mode 100644 index 0000000..45a6d6d --- /dev/null +++ b/prb_sys.h @@ -0,0 +1,7 @@ +#ifndef PRB_OS_IO_H +#define PRB_OS_IO_H + +FLS sys_read_entire_file_fls(Arena *arena, const char *fname); +char *sys_getbindir(Arena *arena); + +#endif /* PRB_OS_IO_H */ diff --git a/prb_types.h b/prb_types.h index bfcafcc..8760513 100644 --- a/prb_types.h +++ b/prb_types.h @@ -18,10 +18,6 @@ typedef double F64; /* NOTE(pryazha): The library only use right-handed coordiante system (for now) */ -typedef struct { - F32 x, y; -} V2; - #define V2_ZERO (V2){ 0.0f, 0.0f } #define V2_ONE (V2){ 1.0f, 1.0f } #define V2_RIGHT (V2){ 1.0f, 0.0f } @@ -30,8 +26,9 @@ typedef struct { #define V2_DOWN (V2){ 0.0f, -1.0f } typedef struct { - F32 x, y, z; -} V3; + F32 x; + F32 y; +} V2; #define V3_ZERO (V3){ 0.0f, 0.0f, 0.0f } #define V3_ONE (V3){ 1.0f, 1.0f, 1.0f } @@ -43,15 +40,20 @@ typedef struct { #define V3_BACKWARD (V3){ 0.0f, 0.0f, -1.0f } typedef struct { - F32 x, y, z, w; -} V4; + F32 x; + F32 y; + F32 z; +} V3; #define V4_ZERO (V4){ 0.0f, 0.0f, 0.0f, 0.0f } #define V4_ONE (V4){ 1.0f, 1.0f, 1.0f, 1.0f } typedef struct { - V4 m0, m1, m2, m3; -} MAT4; + F32 x; + F32 y; + F32 z; + F32 w; +} V4; #define MAT4_IDENTITY (MAT4) { \ { 1.0f, 0.0f, 0.0f, 0.0f }, \ @@ -59,29 +61,36 @@ typedef struct { { 0.0f, 0.0f, 1.0f, 0.0f }, \ { 0.0f, 0.0f, 0.0f, 1.0f } } +typedef struct { + V4 m0; + V4 m1; + V4 m2; + V4 m3; +} MAT4; + typedef struct { U8 *mem; U64 cap; U64 used; } Arena; -/* NOTE(pryazha): Strings */ +/* NOTE(pryazha): Fixed-length string */ typedef struct { - U8 *ptr; - U64 length; -} Str8; + U8 *p; + U64 len; +} FLS; -typedef struct Str8Node { - Str8 str; - struct Str8Node *next; - struct Str8Node *prev; -} Str8Node; +typedef struct FLSNode { + FLS str; + struct FLSNode *next; + struct FLSNode *prev; +} FLSNode; typedef struct { - Str8Node *first; - Str8Node *last; - U64 total_length; - U32 node_count; -} Str8List; + struct FLSNode *first; + struct FLSNode *last; + U64 len; + U32 ncnt; +} FLSList; #endif /* PRB_TYPES_H */ -- cgit v1.2.3-70-g09d2