Cleanup build tool main and add diagnostics for arenas

This commit is contained in:
Krzosa Karol
2024-01-28 08:02:44 +01:00
parent b0620ae57b
commit bb8d96e010
3 changed files with 42 additions and 26 deletions

View File

@@ -29,6 +29,9 @@ int main(int argument_count, char **arguments) {
if (build_file.str == 0) { if (build_file.str == 0) {
IO_Printf("Couldnt find build file in current dir: %.*s, exiting ... \n", S8_Expand(working_dir)); IO_Printf("Couldnt find build file in current dir: %.*s, exiting ... \n", S8_Expand(working_dir));
IO_Printf("- Proper build file contains 'build_file.c' in it's name\n");
IO_Printf("- Alternative compiler can be chosen like this: bld cc=clang\n");
IO_Printf("- Cache can be cleared like this: bld clear_cache\n");
return 0; return 0;
} }
} }
@@ -42,14 +45,28 @@ int main(int argument_count, char **arguments) {
double time = OS_GetTime(); double time = OS_GetTime();
int result = 0; int result = 0;
if (cc == "cl") { if (cc == "cl") {
result = OS_SystemF("cl %.*s -Fe:%.*s -WX -W3 -wd4200 -diagnostics:column -nologo -Zi", S8_Expand(build_file), S8_Expand(exe_name)); Array<S8_String> flags = {Perm};
flags += "-nologo -Zi";
flags += "-WX -W3 -wd4200 -diagnostics:column";
flags += Fmt("/Fe:%.*s", S8_Expand(exe_name));
result = Run(cc + build_file + flags);
} }
else if (cc == "clang") { else if (cc == "clang") {
result = OS_SystemF("clang++ -std=c++11 -fdiagnostics-absolute-paths -Wno-writable-strings %.*s -o %.*s -g", S8_Expand(build_file), S8_Expand(exe_name)); Array<S8_String> flags = {Perm};
flags += "-std=c++11 -g";
flags += "-fdiagnostics-absolute-paths";
flags += "-Wno-writable-strings";
flags += Fmt("-o %.*s", S8_Expand(exe_name));
result = Run(cc + build_file + flags);
} }
else { else {
IO_Assert(cc == "gcc"); IO_Assert(cc == "gcc");
result = OS_SystemF("g++ -Wno-write-strings %.*s -o %.*s -g", S8_Expand(build_file), S8_Expand(exe_name));
Array<S8_String> flags = {Perm};
flags += "-std=c++11 -g";
flags += "-Wno-write-strings";
flags += Fmt("-o %.*s", S8_Expand(exe_name));
result = Run(cc + build_file + flags);
} }
if (result != 0) { if (result != 0) {
@@ -60,12 +77,10 @@ int main(int argument_count, char **arguments) {
IO_Printf("TIME Build file compilation: %f\n", time); IO_Printf("TIME Build file compilation: %f\n", time);
} }
S8_String cmdline_args = Merge(cmd);
// Run the build file // Run the build file
double time = OS_GetTime(); double time = OS_GetTime();
if (build_file.str) { if (build_file.str) {
int result = OS_SystemF(IF_WINDOWS_ELSE("", "./") "%.*s %.*s", S8_Expand(exe_name), S8_Expand(cmdline_args)); int result = Run(exe_name + cmd);
if (result != 0) { if (result != 0) {
IO_Printf("FAILED execution of the build file!\n"); IO_Printf("FAILED execution of the build file!\n");
return 1; return 1;

View File

@@ -3,7 +3,8 @@
leeway for big buffers and other such things. Just make sure to not relay on it because it's easier unless specified. leeway for big buffers and other such things. Just make sure to not relay on it because it's easier unless specified.
- Not sure if we should assume that strings should use allocators or arenas, for now it's arenas because I don't have other use cases - Not sure if we should assume that strings should use allocators or arenas, for now it's arenas because I don't have other use cases
@todo @todo
- Remove static buffers from filesystem, use scratch arenas instead? - Add file, line info to Arenas!
- Remove static buffers from filesystem, use scratch arenas, more secure, data corruption instead of control flow corruption
- Use allocators instead of concrete Arenas - Use allocators instead of concrete Arenas
- Add proper string arrays and utilities for build files - Add proper string arrays and utilities for build files
- also add String Arrays and String Builder, temp allocators hook ins for nicer api - also add String Arrays and String Builder, temp allocators hook ins for nicer api
@@ -15,7 +16,7 @@
#define IO_VSNPRINTF stbsp_vsnprintf #define IO_VSNPRINTF stbsp_vsnprintf
#define IO_SNPRINTF stbsp_snprintf #define IO_SNPRINTF stbsp_snprintf
#include "../standalone_libraries/io.c" #include "../standalone_libraries/io.c"
#define MA_ASSERT(x) IO_Assert(x) #define MA_Assertf(x, ...) IO_Assertf(x, __VA_ARGS__)
#include "../standalone_libraries/arena.c" #include "../standalone_libraries/arena.c"
#define RE_ASSERT(x) IO_Assert(x) #define RE_ASSERT(x) IO_Assert(x)
#include "../standalone_libraries/regex.c" #include "../standalone_libraries/regex.c"

View File

@@ -1,7 +1,7 @@
#include "arena.h" #include "arena.h"
#ifndef MA_ASSERT #ifndef MA_Assertf
#include <assert.h> #include <assert.h>
#define MA_ASSERT(x) assert(x) #define MA_Assertf(x, ...) assert(x)
#endif #endif
#ifndef MA_MemoryZero #ifndef MA_MemoryZero
@@ -65,7 +65,7 @@ MA_StaticFunc uint8_t *MV__AdvanceCommit(MV_Memory *m, size_t *commit_size, size
size_t to_be_total_commited_size = aligned_up_commit + m->commit; size_t to_be_total_commited_size = aligned_up_commit + m->commit;
size_t to_be_total_commited_size_clamped_to_reserve = MA_CLAMP_TOP(to_be_total_commited_size, m->reserve); size_t to_be_total_commited_size_clamped_to_reserve = MA_CLAMP_TOP(to_be_total_commited_size, m->reserve);
size_t adjusted_to_boundary_commit = to_be_total_commited_size_clamped_to_reserve - m->commit; size_t adjusted_to_boundary_commit = to_be_total_commited_size_clamped_to_reserve - m->commit;
MA_ASSERT(adjusted_to_boundary_commit && "Reached the virtual memory reserved boundary"); MA_Assertf(adjusted_to_boundary_commit, "Reached the virtual memory reserved boundary");
*commit_size = adjusted_to_boundary_commit; *commit_size = adjusted_to_boundary_commit;
if (adjusted_to_boundary_commit == 0) { if (adjusted_to_boundary_commit == 0) {
@@ -76,7 +76,7 @@ MA_StaticFunc uint8_t *MV__AdvanceCommit(MV_Memory *m, size_t *commit_size, size
} }
MA_API void MA_PopToPos(MA_Arena *arena, size_t pos) { MA_API void MA_PopToPos(MA_Arena *arena, size_t pos) {
MA_ASSERT(arena->len >= arena->base_len); MA_Assertf(arena->len >= arena->base_len, "Bug: arena->len shouldn't ever be smaller then arena->base_len");
pos = MA_CLAMP(pos, arena->base_len, arena->len); pos = MA_CLAMP(pos, arena->base_len, arena->len);
size_t size = arena->len - pos; size_t size = arena->len - pos;
arena->len = pos; arena->len = pos;
@@ -106,7 +106,7 @@ MA_API void MA_SetAlignment(MA_Arena *arena, int alignment) {
} }
MA_API uint8_t *MA_GetTop(MA_Arena *a) { MA_API uint8_t *MA_GetTop(MA_Arena *a) {
MA_ASSERT(a->memory.data); // arena needs to be inited MA_Assertf(a->memory.data, "Arena needs to be inited, there is no top to get!");
return a->memory.data + a->len; return a->memory.data + a->len;
} }
@@ -120,17 +120,17 @@ MA_API void *MA_PushSizeNonZeroed(MA_Arena *a, size_t size) {
#if MA_ZERO_IS_INITIALIZATION #if MA_ZERO_IS_INITIALIZATION
MA_Init(a); MA_Init(a);
#else #else
MA_ASSERT("Pushing on uninitialized arena"); MA_Assertf(0, "Pushing on uninitialized arena with zero initialization turned off");
#endif #endif
} }
bool result = MV_Commit(&a->memory, size_with_alignment + MA_COMMIT_ADD_SIZE); bool result = MV_Commit(&a->memory, size_with_alignment + MA_COMMIT_ADD_SIZE);
MA_ASSERT(result && "Failed to commit memory"); MA_Assertf(result, "Failed to commit memory more memory! reserve: %zu commit: %zu len: %zu size_with_alignment: %zu", a->memory.reserve, a->memory.commit, a->len, size_with_alignment);
(void)result; (void)result;
} }
uint8_t *result = a->memory.data + aligned_len; uint8_t *result = a->memory.data + aligned_len;
a->len += size_with_alignment; a->len += size_with_alignment;
MA_ASSERT(a->len <= a->memory.commit); MA_Assertf(a->len <= a->memory.commit, "Reached commit boundary! reserve: %zu commit: %zu len: %zu size_with_alignment: %zu", a->memory.reserve, a->memory.commit, a->len, size_with_alignment);
ASAN_UNPOISON_MEMORY_REGION(result, size); ASAN_UNPOISON_MEMORY_REGION(result, size);
return (void *)result; return (void *)result;
} }
@@ -277,7 +277,7 @@ MA_StaticFunc void *M_ClibAllocatorProc(void *allocator, M_AllocatorOp kind, voi
return MA_CRealloc(p, size); return MA_CRealloc(p, size);
} }
MA_ASSERT("MA_Arena invalid codepath"); MA_Assertf(0, "MA_Arena invalid codepath");
return NULL; return NULL;
} }
@@ -296,7 +296,7 @@ MA_API void *MA_AllocatorProc(void *allocator, M_AllocatorOp kind, void *p, size
return NULL; return NULL;
} }
MA_ASSERT("MA_Arena invalid codepath"); MA_Assertf(0, "MA_Arena invalid codepath");
return NULL; return NULL;
} }
@@ -316,7 +316,7 @@ MA_API void *MA_ExclusiveAllocatorProc(void *allocator, M_AllocatorOp kind, void
return NULL; return NULL;
} }
MA_ASSERT("MA_Arena invalid codepath"); MA_Assertf(0, "MA_Arena invalid codepath");
return NULL; return NULL;
} }
@@ -358,7 +358,7 @@ MA_API MA_Checkpoint MA_GetScratchEx(MA_Arena **conflicts, int conflict_count) {
} }
} }
MA_ASSERT(unoccupied); MA_Assertf(unoccupied, "Failed to get free scratch memory, this is a fatal error, this shouldnt happen");
MA_Checkpoint result = MA_Save(unoccupied); MA_Checkpoint result = MA_Save(unoccupied);
return result; return result;
} }
@@ -390,7 +390,7 @@ MA_API MV_Memory MV_Reserve(size_t size) {
MA_MemoryZero(&result, sizeof(result)); MA_MemoryZero(&result, sizeof(result));
size_t adjusted_size = MA_AlignUp(size, MV__WIN32_PAGE_SIZE); size_t adjusted_size = MA_AlignUp(size, MV__WIN32_PAGE_SIZE);
result.data = (uint8_t *)VirtualAlloc(0, adjusted_size, MEM_RESERVE, PAGE_READWRITE); result.data = (uint8_t *)VirtualAlloc(0, adjusted_size, MEM_RESERVE, PAGE_READWRITE);
MA_ASSERT(result.data && "Failed to reserve virtual memory"); MA_Assertf(result.data, "Failed to reserve virtual memory");
result.reserve = adjusted_size; result.reserve = adjusted_size;
return result; return result;
} }
@@ -399,7 +399,7 @@ MA_API bool MV_Commit(MV_Memory *m, size_t commit) {
uint8_t *pointer = MV__AdvanceCommit(m, &commit, MV__WIN32_PAGE_SIZE); uint8_t *pointer = MV__AdvanceCommit(m, &commit, MV__WIN32_PAGE_SIZE);
if (pointer) { if (pointer) {
void *result = VirtualAlloc(pointer, commit, MEM_COMMIT, PAGE_READWRITE); void *result = VirtualAlloc(pointer, commit, MEM_COMMIT, PAGE_READWRITE);
MA_ASSERT(result && "Failed to commit more memory"); MA_Assertf(result, "Failed to commit more memory");
if (result) { if (result) {
m->commit += commit; m->commit += commit;
return true; return true;
@@ -410,7 +410,7 @@ MA_API bool MV_Commit(MV_Memory *m, size_t commit) {
MA_API void MV_Deallocate(MV_Memory *m) { MA_API void MV_Deallocate(MV_Memory *m) {
BOOL result = VirtualFree(m->data, 0, MEM_RELEASE); BOOL result = VirtualFree(m->data, 0, MEM_RELEASE);
MA_ASSERT(result != 0 && "Failed to release MV_Memory"); MA_Assertf(result != 0, "Failed to release MV_Memory");
} }
MA_API bool MV_DecommitPos(MV_Memory *m, size_t pos) { MA_API bool MV_DecommitPos(MV_Memory *m, size_t pos) {
@@ -435,7 +435,7 @@ MA_API MV_Memory MV_Reserve(size_t size) {
MV_Memory result = {}; MV_Memory result = {};
size_t size_aligned = MA_AlignUp(size, MV__UNIX_PAGE_SIZE); size_t size_aligned = MA_AlignUp(size, MV__UNIX_PAGE_SIZE);
result.data = (uint8_t *)mmap(0, size_aligned, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); result.data = (uint8_t *)mmap(0, size_aligned, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
MA_ASSERT(result.data && "Failed to reserve memory using mmap!!"); MA_Assertf(result.data, "Failed to reserve memory using mmap!!");
if (result.data) { if (result.data) {
result.reserve = size_aligned; result.reserve = size_aligned;
} }
@@ -446,7 +446,7 @@ MA_API bool MV_Commit(MV_Memory *m, size_t commit) {
uint8_t *pointer = MV__AdvanceCommit(m, &commit, MV__UNIX_PAGE_SIZE); uint8_t *pointer = MV__AdvanceCommit(m, &commit, MV__UNIX_PAGE_SIZE);
if (pointer) { if (pointer) {
int mprotect_result = mprotect(pointer, commit, PROT_READ | PROT_WRITE); int mprotect_result = mprotect(pointer, commit, PROT_READ | PROT_WRITE);
MA_ASSERT(mprotect_result == 0 && "Failed to commit more memory using mmap"); MA_Assertf(mprotect_result == 0, "Failed to commit more memory using mmap");
if (mprotect_result == 0) { if (mprotect_result == 0) {
m->commit += commit; m->commit += commit;
return true; return true;
@@ -457,6 +457,6 @@ MA_API bool MV_Commit(MV_Memory *m, size_t commit) {
MA_API void MV_Deallocate(MV_Memory *m) { MA_API void MV_Deallocate(MV_Memory *m) {
int result = munmap(m->data, m->reserve); int result = munmap(m->data, m->reserve);
MA_ASSERT(result == 0 && "Failed to release virtual memory using munmap"); MA_Assertf(result == 0, "Failed to release virtual memory using munmap");
} }
#endif #endif