diff --git a/core.c b/core.c index c1d91a4..3eba17d 100644 --- a/core.c +++ b/core.c @@ -7,7 +7,7 @@ void *pushSizeFill(Arena *arena, size_t bytes, byte fill) { if (arena->capacity - arena->head >= bytes) { - void *ptr = (char *)arena->memory + arena->head; + void *ptr = (byte *)arena->memory + arena->head; arena->head += bytes; memset(ptr, fill, bytes); return ptr; @@ -17,7 +17,7 @@ void *pushSizeFill(Arena *arena, size_t bytes, byte fill) { void *pushSize(Arena *arena, size_t bytes) { if (arena->capacity - arena->head >= bytes) { - void *ptr = (char *)arena->memory + arena->head; + void *ptr = (byte *)arena->memory + arena->head; arena->head += bytes; return ptr; } @@ -26,7 +26,7 @@ void *pushSize(Arena *arena, size_t bytes) { Arena *arenaAlloc(size_t capacity) { Arena *result = (Arena *)os_alloc(sizeof(Arena) + capacity); - result->memory = result + sizeof(Arena); + result->memory = (byte *)result + sizeof(Arena); result->capacity = capacity; result->head = 0; return result; diff --git a/core.h b/core.h index 5a3cd41..4a48b95 100644 --- a/core.h +++ b/core.h @@ -7,7 +7,7 @@ #include "time.h" // TODO(djledda): try not to depend on this one // ### Misc macros ### -#if ENABLE_ASSERT +#if DJSTDLIB_DEBUG #define Assert(expression) if (!(expression)) {*(volatile int *)0 = 0;} #else #define Assert(expression) @@ -47,16 +47,18 @@ typedef struct string string; #define MemberSizeUnderlying(type, memberName) sizeof(*((type *)0)->memberName) // ### Arenas ### -typedef struct { +typedef struct Arena Arena; +struct Arena { void *memory; size_t capacity; size_t head; -} Arena; +}; -typedef struct { +typedef struct Scratch Scratch; +struct Scratch { Arena *arena; size_t start; -} Scratch; +}; void *pushSize(Arena *arena, size_t bytes); void *pushSizeFill(Arena *arena, size_t bytes, byte fill); @@ -76,13 +78,14 @@ void scratchEnd(Scratch scratch); #define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0) // ### Vectors ### -typedef union { +typedef union Vec2 Vec2; +union Vec2 { struct { real32 x; real32 y; }; real32 vec[2]; -} Vec2; +}; inline function Vec2 vec2(real32 x, real32 y) { Vec2 result = {0}; result.x = x; @@ -90,14 +93,15 @@ inline function Vec2 vec2(real32 x, real32 y) { return result; } -typedef union { +typedef union Vec3 Vec3; +union Vec3 { struct { real32 x; real32 y; real32 z; }; real32 vec[3]; -} Vec3; +}; inline function Vec3 vec3(real32 x, real32 y, real32 z) { Vec3 result = {0}; result.x = x; @@ -106,7 +110,8 @@ inline function Vec3 vec3(real32 x, real32 y, real32 z) { return result; } -typedef union { +typedef union Vec4 Vec4; +union Vec4 { struct { real32 r; real32 g; @@ -120,7 +125,7 @@ typedef union { real32 w; }; real32 vec[4]; -} Vec4; +}; inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) { Vec4 result = {0}; result.x = x; @@ -132,20 +137,21 @@ inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) { // ### Lists ### #define DefineList(type, prefix) \ - typedef struct {\ + typedef struct prefix ## List prefix ## List;\ + struct prefix ## List {\ type* data;\ size_t length;\ size_t capacity;\ - } prefix ## List;\ + };\ typedef type prefix ## List ## _underlying #define ListElementSize(list) MemberSizeUnderlying(list, data) DefineList(string, String); -#define PushList(arena, type, size) (type){ pushSize(arena, ListElementSize(type)*size), 0, size } -#define PushListZero(arena, type, size) (type){ pushSizeFill(arena, ListElementSize(type)*size, 0), 0, size } -#define PushFullList(arena, type, size) (type){ pushSize(arena, ListElementSize(type)*size), size, size } -#define PushFullListZero(arena, type, size) (type){ pushSizeFill(arena, ListElementSize(type)*size, 0), size, size } +#define PushList(arena, type, size) (type){ .data=pushSize(arena, ListElementSize(type)*(size)), .length=0, .capacity=(size) } +#define PushListZero(arena, type, size) (type){ .data=pushSizeFill(arena, ListElementSize(type)*(size), 0), .length=0, .capacity=(size) } +#define PushFullList(arena, type, size) (type){ .data=pushSize(arena, ListElementSize(type)*size), .length=(size), .capacity=(size) } +#define PushFullListZero(arena, type, size) (type){ .data=pushSizeFill(arena, ListElementSize(type)*(size), 0), .length=(size), .capacity=(size) } #define EmptyList() { NULL, 0, 0 } #define __ArrayAsList(array) { .data=(array), .length=ArrayCount(array), .capacity=ArrayCount(array) } @@ -199,16 +205,18 @@ StringList strSplit(Arena *arena, string splitStr, string inputStr); string strPrintfv(Arena *arena, const char *fmt, va_list args); string strPrintf(Arena *arena, const char *fmt, ...); -typedef struct { +typedef struct ParsePositiveIntResult ParsePositiveIntResult; +struct ParsePositiveIntResult { uint8 result; bool valid; -} ParsePositiveIntResult; +}; ParsePositiveIntResult parsePositiveInt(string str, size_t *lengthPointer); -typedef struct { +typedef struct ParsePositiveReal32Result ParsePositiveReal32Result; +struct ParsePositiveReal32Result { real32 result; bool valid; -} ParsePositiveReal32Result; +}; ParsePositiveReal32Result parsePositiveReal32(string str, size_t *lengthPointer); inline function bool isNumeric(char c); @@ -290,6 +298,7 @@ extern void (*print)(const char *fmt, ...); // ### Loops ### #define EachIn(list, it) size_t it = 0; it < (list).length; it++ +#define EachEl(list, type, element) type* element = (list).data; element < (list).data + (list).length; element += 1 #define EachInReversed(list, it) size_t it = (list).length - 1; it >= 0 && it < (list).length; it-- #define EachInArray(arr, it) size_t it = 0; it < ArrayCount(arr); ++it