2024-06-28 03:28:23 +02:00
|
|
|
|
2024-07-03 17:55:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-28 03:28:23 +02:00
|
|
|
|
|
|
|
#define thread_local _Thread_local
|
|
|
|
|
2024-06-29 17:54:30 +02:00
|
|
|
#define local_persist static
|
|
|
|
|
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
|
|
|
#define forward_global extern
|
2024-06-28 03:28:23 +02:00
|
|
|
|
2024-06-28 18:50:30 +02:00
|
|
|
|
|
|
|
void printf(const char* fmt, ...);
|
2024-07-02 15:27:33 +02:00
|
|
|
#define ASSERT_STR_HELPER(x) #x
|
|
|
|
#define ASSERT_STR(x) ASSERT_STR_HELPER(x)
|
2024-07-04 20:56:27 +02:00
|
|
|
#define assert_line(line, cond, ...) {if(!(cond)) { printf("Assertion failed in file " __FILE__ " on line " ASSERT_STR(line) "\nFailed Condition: " #cond ". Message: " __VA_ARGS__); crash(); }}
|
|
|
|
#define assert(cond, ...) {assert_line(__LINE__, cond, __VA_ARGS__)}
|
2024-06-28 03:28:23 +02:00
|
|
|
|
2024-07-02 15:27:33 +02:00
|
|
|
#define DEFER(start, end) for(int _i_ = ((start), 0); _i_ == 0; _i_ += 1, (end))
|
|
|
|
|
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
|
|
|
#if CONFIGURATION == RELEASE
|
2024-06-29 13:27:37 +02:00
|
|
|
#undef assert
|
2024-07-04 20:56:27 +02:00
|
|
|
#define assert(...) (void)0;
|
2024-06-29 13:27:37 +02:00
|
|
|
#endif
|
|
|
|
|
2024-07-04 20:56:27 +02:00
|
|
|
#define panic(...) { print(__VA_ARGS__); crash(); }
|
2024-06-29 17:54:30 +02:00
|
|
|
|
2024-06-28 03:28:23 +02:00
|
|
|
#define cast(t) (t)
|
|
|
|
|
2024-06-29 13:27:37 +02:00
|
|
|
#define ZERO(t) (t){0}
|
|
|
|
|
2024-07-03 00:01:11 +02:00
|
|
|
|
2024-07-03 17:55:25 +02:00
|
|
|
|
2024-06-28 21:57:37 +02:00
|
|
|
#define FIRST_ARG(arg1, ...) arg1
|
2024-07-01 13:10:06 +02:00
|
|
|
#define SECOND_ARG(arg1, arg2, ...) arg2
|
2024-06-28 21:57:37 +02:00
|
|
|
#define print(...) _Generic((FIRST_ARG(__VA_ARGS__)), \
|
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
|
|
|
string: prints, \
|
2024-06-28 21:57:37 +02:00
|
|
|
default: printf \
|
|
|
|
)(__VA_ARGS__)
|
2024-07-01 13:10:06 +02:00
|
|
|
#define sprint(...) _Generic((SECOND_ARG(__VA_ARGS__)), \
|
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
|
|
|
string: sprints, \
|
2024-06-28 21:57:37 +02:00
|
|
|
default: sprintf \
|
|
|
|
)(__VA_ARGS__)
|
|
|
|
#define tprint(...) _Generic((FIRST_ARG(__VA_ARGS__)), \
|
- File IO
- os_file_open(string path, Os_Io_Open_Flags flags)
- os_file_close(File f)
- os_file_delete(string path)
- os_file_write_string(File f, string s)
- os_file_write_bytes(File f, void *buffer, u64 size_in_bytes)
- os_file_read(File f, void* buffer, u64 bytes_to_read, u64 *actual_read_bytes)
- os_write_entire_file_handle(File f, string data)
- os_write_entire_file(string path, string data)
- os_read_entire_file_handle(File f, string *result)
- os_read_entire_file(string path, string *result)
- fprint(File, string/char*, ...)
- Buncha tests
- os_high_precision_sleep
- talloc_string
- Program memory is touched on VirtualAlloc so it actually allocates physical memory (and we can tell when an address is untouched)
2024-06-29 20:55:43 +02:00
|
|
|
string: tprints, \
|
2024-06-28 21:57:37 +02:00
|
|
|
default: tprintf \
|
|
|
|
)(__VA_ARGS__)
|
|
|
|
|
2024-06-28 03:28:23 +02:00
|
|
|
typedef struct Nothing {int nothing;} Nothing;
|
|
|
|
|
|
|
|
#ifndef CONTEXT_EXTRA
|
|
|
|
#define CONTEXT_EXTRA Nothing
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef enum Allocator_Message {
|
|
|
|
ALLOCATOR_ALLOCATE,
|
|
|
|
ALLOCATOR_DEALLOCATE,
|
2024-06-29 13:27:37 +02:00
|
|
|
ALLOCATOR_REALLOCATE,
|
2024-06-28 03:28:23 +02:00
|
|
|
} Allocator_Message;
|
2024-07-01 13:10:06 +02:00
|
|
|
typedef void*(*Allocator_Proc)(u64, void*, Allocator_Message, void*);
|
2024-06-28 03:28:23 +02:00
|
|
|
|
2024-07-01 02:14:08 +02:00
|
|
|
typedef enum Log_Level {
|
|
|
|
LOG_ERROR,
|
|
|
|
LOG_INFO,
|
|
|
|
LOG_WARNING,
|
|
|
|
LOG_VERBOSE
|
|
|
|
} Log_Level;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-28 03:28:23 +02:00
|
|
|
typedef struct Allocator {
|
|
|
|
Allocator_Proc proc;
|
|
|
|
void *data;
|
|
|
|
} Allocator;
|
|
|
|
|
|
|
|
typedef struct Context {
|
2024-07-01 02:14:08 +02:00
|
|
|
void *logger; // void(*Logger_Proc)(Log_Level level, string fmt, ...)
|
2024-06-28 03:28:23 +02:00
|
|
|
|
|
|
|
CONTEXT_EXTRA extra;
|
|
|
|
} Context;
|
|
|
|
|
|
|
|
|
|
|
|
#define CONTEXT_STACK_MAX 512
|
|
|
|
thread_local Context context;
|
|
|
|
thread_local Context context_stack[CONTEXT_STACK_MAX];
|
|
|
|
thread_local u64 num_contexts = 0;
|
|
|
|
|
2024-07-01 13:10:06 +02:00
|
|
|
forward_global thread_local Allocator temp;
|
|
|
|
|
2024-07-02 19:12:31 +02:00
|
|
|
void* memset(void* dest, int value, size_t amount);
|
2024-07-01 13:10:06 +02:00
|
|
|
void* alloc(Allocator allocator, u64 size) {
|
2024-07-02 19:12:31 +02:00
|
|
|
void *p = allocator.proc(size, 0, ALLOCATOR_ALLOCATE, allocator.data);
|
|
|
|
#if DO_ZERO_INITIALIZATION
|
|
|
|
memset(p, 0, size);
|
|
|
|
#endif
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
void* alloc_uninitialized(Allocator allocator, u64 size) {
|
2024-07-01 13:10:06 +02:00
|
|
|
return allocator.proc(size, 0, ALLOCATOR_ALLOCATE, allocator.data);
|
|
|
|
}
|
|
|
|
void dealloc(Allocator allocator, void *p) {
|
|
|
|
allocator.proc(0, p, ALLOCATOR_DEALLOCATE, allocator.data);
|
|
|
|
}
|
2024-06-28 03:28:23 +02:00
|
|
|
|
|
|
|
void push_context(Context c) {
|
|
|
|
assert(num_contexts < CONTEXT_STACK_MAX, "Context stack overflow");
|
|
|
|
|
|
|
|
context_stack[num_contexts] = context;
|
|
|
|
context = c;
|
|
|
|
num_contexts += 1;
|
|
|
|
}
|
|
|
|
void pop_context() {
|
|
|
|
assert(num_contexts > 0, "No contexts to pop!");
|
|
|
|
num_contexts -= 1;
|
|
|
|
context = context_stack[num_contexts];
|
|
|
|
}
|
|
|
|
|
2024-06-28 18:50:30 +02:00
|
|
|
|