From 49e379dd54ff5b758b7261fc7246e3e74352343a Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Thu, 2 Jan 2025 18:20:56 +0100 Subject: [PATCH] update --- app.cpp | 22 ++++++++++---- build | 2 +- build.bat | 2 +- core.cpp | 69 ++++++++++++++++++++++++++++---------------- core.h | 5 ++-- os.h | 2 +- os_linux.cpp | 9 ++++-- os_win32.cpp | 11 +++---- vendor/stb_sprintf.h | 2 ++ 9 files changed, 80 insertions(+), 44 deletions(-) diff --git a/app.cpp b/app.cpp index 51331ce..211f75f 100644 --- a/app.cpp +++ b/app.cpp @@ -1,5 +1,6 @@ #include #include "core.cpp" +#include "core.h" int main(int argc, char **argv) { int statusCode = 0; @@ -7,14 +8,23 @@ int main(int argc, char **argv) { Arena *arena = arenaAlloc(Megabytes(64)); list args = getArgs(arena, argc, argv); - log(strSplit(arena, strlit("-"), strlit("hallo-world"))); + log(strSplit(arena, "-"_s, "hallo-world"_s)); while (true) { - size_t arenaPos = arena->head; - string line = PushString(arena, 128); - fgets(line.str, (int)line.length, stdin); - log(strSplit(arena, strlit("-"), line)); - arenaFreeFrom(arena, arenaPos); + string line; + list split; + + WithScratch(temp) { + line = PushString(temp.arena, 128); + fgets(line.str, (int)line.length, stdin); + split = strSplit(temp.arena, "-"_s, line); + } + + if (line.str[0] == '\n' && line.str[1] == '\0') { + break; + } else { + log(split); + } } return statusCode; diff --git a/build b/build index 41f7456..5601ac6 100644 --- a/build +++ b/build @@ -1,3 +1,3 @@ #!/bin/bash -gcc -g -g3 ./app.cpp -o ./target/app +g++ -g -g3 -lm -DOS_LINUX=1 -DENABLE_ASSERT=1 ./app.cpp -o ./target/app diff --git a/build.bat b/build.bat index 903a381..4d507fb 100644 --- a/build.bat +++ b/build.bat @@ -5,7 +5,7 @@ if NOT EXIST .\target mkdir .\target set commonLinkerFlags=-opt:ref set commonCompilerFlags=^ -MT %= Make sure the C runtime library is statically linked =%^ - -Gm- %= Turns off incremently building =%^ + -Gm- %= Turns off incremental building =%^ -nologo %= No one cares you made the compiler Microsoft =%^ -Oi %= Always use intrinsics =%^ -EHa- %= Disable exception handling =%^ diff --git a/core.cpp b/core.cpp index 7320438..1d85fc4 100644 --- a/core.cpp +++ b/core.cpp @@ -1,6 +1,9 @@ +#include // TODO(djledda): get outta here +#include +#include #define STB_SPRINTF_IMPLEMENTATION #include "core.h" -#include "os.h" +#include "os.cpp" void *pushSize(Arena *arena, size_t bytes) { if (arena->capacity - arena->head >= bytes) { @@ -20,13 +23,7 @@ Arena *arenaAlloc(size_t capacity) { } void arenaFree(Arena *arena) { - os_free(arena); -#if OS_WINDOWS - VirtualFree(arena, NULL, MEM_RELEASE); -#elif OS_LINUX - // TODO(dledda): implement this for Linux - Assert(false); -#endif + os_free(arena, arena->capacity); } void arenaFreeFrom(Arena *arena, size_t position) { @@ -60,6 +57,9 @@ Scratch scratchStart(Arena **conflicts, size_t conflictCount) { return scratch; } +#define DeferLoop(begin_stmnt, end_stmnt) for(int __defer_i = ((begin_stmnt), 0); __defer_i < 1; (++__defer_i, (end_stmnt))) +#define WithScratch(scratchName) Scratch scratchName; DeferLoop(scratchName = scratchStart(0, 0), scratchEnd(scratchName)) + void scratchEnd(Scratch scratch) { arenaFreeFrom(scratch.arena, scratch.start); } @@ -86,6 +86,13 @@ void zeroList(list *list) { memset(list->data, 0, list->head * sizeof(T)); } +inline string operator""_s(const char *cstrLiteral, unsigned long length) { + return { + (char *)cstrLiteral, + length, + }; +} + const char *cstring(Arena *arena, list buf) { char *arr = PushArray(arena, char, buf.length + 1); memmove(arr, buf.data, buf.length); @@ -166,7 +173,7 @@ list listSlice(list l, size_t start, size_t stop) { if (stop == 0) { stop = l.head; } - // TODO(dledda): maybe assert instead + // TODO(djledda): maybe assert instead if (stop > l.head || start > stop) { return {0}; } @@ -181,7 +188,7 @@ string strSlice(string str, size_t start, size_t stop) { if (stop == 0) { stop = str.length; } - // TODO(dledda): maybe assert instead + // TODO(djledda): maybe assert instead if (stop > str.length || start > stop) { return {0}; } @@ -207,9 +214,9 @@ bool stringContains(string str, char c) { return false; } -const char NUMERIC_CHARS[] = "0123456789"; +string NUMERIC_CHARS = "0123456789"_s; inline bool isNumeric(char c) { - return stringContains(strlit(NUMERIC_CHARS), c); + return stringContains(NUMERIC_CHARS, c); } list strSplit(Arena *arena, string splitStr, string inputStr) { @@ -317,17 +324,16 @@ string readEntireFile(Arena *arena, string filename) { struct stat st; stat((char *)filename.str, &st); size_t fsize = st.st_size; - string readBuffer = PushString(arena, filesize); - readBuffer.length = filesize; - fread(readBuffer.str, sizeof(byte), filesize, input); + string readBuffer = PushString(arena, fsize); + fread(readBuffer.str, sizeof(byte), readBuffer.length, input); fclose(input); return readBuffer; #endif } bool writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) { -#if OS_WINDOWS bool result = false; +#if OS_WINDOWS HANDLE fileHandle = CreateFileA(cstring(arena, filename), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL); if (fileHandle != INVALID_HANDLE_VALUE) { DWORD bytesWritten; @@ -337,15 +343,15 @@ bool writeEntireFile(Arena *arena, string filename, const byte *contents, size_t } CloseHandle(fileHandle); } - return result; #elif OS_LINUX Assert(false); #endif + return result; } bool fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) { -#if OS_WINDOWS bool result = false; +#if OS_WINDOWS HANDLE fileHandle = CreateFileA(cstring(arena, filename), FILE_APPEND_DATA | FILE_GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (fileHandle != INVALID_HANDLE_VALUE) { DWORD bytesWritten; @@ -356,10 +362,10 @@ bool fileAppend(Arena *arena, string filename, const byte *contents, size_t cont } CloseHandle(fileHandle); } - return result; #elif OS_LINUX Assert(false); #endif + return result; } list getArgs(Arena *arena, int argc, char **argv) { @@ -382,7 +388,7 @@ Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp) { } string formatTimeHms(Arena *arena, UnixTimestamp time) { - local_persist const string format = strlit("HH-MM-SS"); + local_persist const string format = "HH-MM-SS"_s; string buf = PushString(arena, format.length); tm *timestamp = gmtime((time_t *)&time); strftime(buf.str, buf.length + 1, "%T", timestamp); @@ -390,14 +396,14 @@ string formatTimeHms(Arena *arena, UnixTimestamp time) { } string formatTimeHms(Arena *arena, Timestamp *time) { - local_persist const string format = strlit("HH-MM-SS"); + local_persist const string format = "HH-MM-SS"_s; string buf = PushString(arena, format.length); strftime(buf.str, buf.length + 1, "%T", (tm *)time); return buf; } string formatTimeYmd(Arena *arena, UnixTimestamp time) { - local_persist const string format = strlit("YYYY-mm-dd"); + local_persist const string format = "YYYY-mm-dd"_s; string buf = PushString(arena, format.length); tm *timestamp = gmtime((time_t *)&time); strftime(buf.str, buf.length + 1, "%Y-%m-%d", timestamp); @@ -405,7 +411,7 @@ string formatTimeYmd(Arena *arena, UnixTimestamp time) { } string formatTimeYmd(Arena *arena, Timestamp *time) { - local_persist const string format = strlit("YYYY-mm-dd"); + local_persist const string format = "YYYY-mm-dd"_s; string buf = PushString(arena, format.length); strftime(buf.str, buf.length + 1, "%Y-%m-%d", (tm *)time); return buf; @@ -433,8 +439,21 @@ function void __core_log(LogTarget target, const char *fmt, va_list argList) { } WriteFile(stdHandle, result.str, (DWORD)result.length, &done, 0); #elif OS_LINUX - // TODO(dledda): implementation - Assert(false); + // TODO(djledda): finish implementation without cstdlib + switch (target) { + case LogTarget_stdin: + write(0, (const void *)result.str, result.length); + break; + case LogTarget_stderr: + fflush(stderr); + write(2, (const void *)result.str, result.length); + break; + case LogTarget_stdout: + default: + fflush(stdout); + write(1, (const void *)result.str, result.length); + break; + } #endif scratchEnd(scratch); } diff --git a/core.h b/core.h index 7ff357b..60e4dde 100644 --- a/core.h +++ b/core.h @@ -5,7 +5,7 @@ #include #include // necessary for int type sizes #include -#include // TODO(dledda): try not to depend on this one +#include // TODO(djledda): try not to depend on this one // ### Misc macros ### #if ENABLE_ASSERT @@ -150,6 +150,7 @@ struct string { #define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1}) #define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) }) +string operator""_s(const char *cstrLiteral, unsigned long length); // C Strings const char *cstring(Arena *arena, list buf); @@ -192,7 +193,7 @@ string formatTimeYmd(Arena *arena, UnixTimestamp time); string formatTimeYmd(Arena *arena, Timestamp *time); // ### Linked Lists ### -// TODO(dledda): implement basic linked lists (based on arenas?) +// TODO(djledda): implement basic linked lists (based on arenas?) // ### Logging ### enum LogTarget { diff --git a/os.h b/os.h index e23818f..44aac28 100644 --- a/os.h +++ b/os.h @@ -7,6 +7,6 @@ void *os_alloc(size_t capacity); void os_reserve(void *ptr); void os_decommit(void *ptr); -void os_free(void *ptr); +void os_free(void *ptr, size_t freeSize); #endif diff --git a/os_linux.cpp b/os_linux.cpp index 0ca6a11..122a4bb 100644 --- a/os_linux.cpp +++ b/os_linux.cpp @@ -1,21 +1,24 @@ #ifndef OS_IMPL_LINUX_CPP #define OS_IMPL_LINUX_CPP +#include "os.h" + #include #include -#include "os.h" void *os_alloc(size_t capacity) { return mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); } -void os_reserve(void *ptr) { +void os_commit(void *ptr) { } void os_decommit(void *ptr) { } -void os_free(void *ptr) { +void os_free(void *ptr, size_t size) { + int err = munmap(ptr, size); + Assert(err != -1); } #endif diff --git a/os_win32.cpp b/os_win32.cpp index 46fc5ef..01eb4ec 100644 --- a/os_win32.cpp +++ b/os_win32.cpp @@ -4,17 +4,18 @@ #include "os.h" #include "Windows.h" -void *osAlloc(size_t capacity) { - return VirtualAlloc(NULL, sizeof(Arena) + capacity, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); +void *os_alloc(size_t commitSize) { + return VirtualAlloc(NULL, commitSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); } -void osReserve(void *ptr) { +void os_reserve(void *ptr) { } -void osDecommit(void *ptr) { +void os_decommit(void *ptr) { } -void osFree(void *ptr) { +void os_free(void *ptr, size_t size) { + VirtualFree(ptr, NULL, MEM_RELEASE); } #endif diff --git a/vendor/stb_sprintf.h b/vendor/stb_sprintf.h index ee9f58c..6c2fd25 100644 --- a/vendor/stb_sprintf.h +++ b/vendor/stb_sprintf.h @@ -603,6 +603,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, goto scopy; case 'S': + { // string struct string str = va_arg(va, string); s = (char *)str.str; @@ -614,6 +615,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, dp = 0; cs = 0; goto scopy; + } break; case 'c': // char // get the character