diff --git a/alloc.cpp b/alloc.cpp index d298418..e595afa 100644 --- a/alloc.cpp +++ b/alloc.cpp @@ -162,7 +162,7 @@ static u64 alignForward(u64 ptr, size_t alignment) { //================================================================================ // Scratch/Arena -Arena* Arena::Init(u32 sizeInBytes) { +Arena* Arena :: Init(u32 sizeInBytes) { ULE_TYPES_H_FTAG; Arena* arena = (Arena*) pMalloc(sizeof(Arena)); arena->index = 0; @@ -170,8 +170,15 @@ Arena* Arena::Init(u32 sizeInBytes) { arena->bufferSizeInBytes = sizeInBytes; return arena; } -void* Arena::Alloc(u32 sizeInBytes) { +void* Arena :: Alloc(u32 sizeInBytes) { ULE_TYPES_H_FTAG; + + u8* p = this->buffer + this->index; + this->index += sizeInBytes; + //String::memset(p, 0, sizeInBytes); + return (void*) p; + + /* u8* p = this->buffer + this->index; u32 offset = (u32) alignForward2((u64) p, 64); @@ -188,8 +195,9 @@ void* Arena::Alloc(u32 sizeInBytes) { } return null; + */ } -void Arena::Clear() { +void Arena :: Clear() { ULE_TYPES_H_FTAG; this->index = 0; } diff --git a/file.cpp b/file.cpp index 5d6ad2f..b47e456 100644 --- a/file.cpp +++ b/file.cpp @@ -10,13 +10,13 @@ #include "types.h" -FILE* File::Open(const char* path, const char* mode) { +FILE* File :: Open(const char* path, const char* mode) { ULE_TYPES_H_FTAG; return fopen(path, mode); } -FILE* File::Open(const char* path, size_t* outSize, const char* mode) { +FILE* File :: Open(const char* path, size_t* outSize, const char* mode) { ULE_TYPES_H_FTAG; - FILE* fp = File::Open(path, mode); + FILE* fp = File :: Open(path, mode); if (fp == null) { return null; @@ -29,13 +29,13 @@ FILE* File::Open(const char* path, size_t* outSize, const char* mode) { return fp; } -void File::Close(FILE* file) { +void File :: Close(FILE* file) { fclose(file); } -size_t File::Size(const char* path) { +size_t File :: Size(const char* path) { ULE_TYPES_H_FTAG; - FILE* fp = File::Open(path); + FILE* fp = File :: Open(path); // get the file's size in bytes fseek(fp, 0, SEEK_END); size_t size = ftell(fp); @@ -43,7 +43,7 @@ size_t File::Size(const char* path) { fclose(fp); return size; } -size_t File::Size(FILE* fp) { +size_t File :: Size(FILE* fp) { ULE_TYPES_H_FTAG; fseek(fp, 0, SEEK_END); size_t size = ftell(fp); @@ -51,9 +51,9 @@ size_t File::Size(FILE* fp) { return size; } -u8* File::Read(const char* path) { +u8* File :: Read(const char* path) { ULE_TYPES_H_FTAG; - FILE* fp = File::Open(path, "rb"); + FILE* fp = File :: Open(path, "rb"); if (fp == null) { return null; @@ -72,9 +72,9 @@ u8* File::Read(const char* path) { return (u8*) buffer; } -u8* File::Read(const char* path, size_t* outSize) { +u8* File :: Read(const char* path, size_t* outSize) { ULE_TYPES_H_FTAG; - FILE* fp = File::Open(path, "rb"); + FILE* fp = File :: Open(path, "rb"); if (fp == null) { return null; @@ -97,7 +97,7 @@ u8* File::Read(const char* path, size_t* outSize) { return (u8*) buffer; } -size_t File::Read(FILE* fp, void* destination) { +size_t File :: Read(FILE* fp, void* destination) { ULE_TYPES_H_FTAG; fseek(fp, 0, SEEK_END); @@ -107,14 +107,14 @@ size_t File::Read(FILE* fp, void* destination) { fread(destination, sizeof (char), size + 1, fp); return size; } -size_t File::Read(FILE* fp, void* destination, size_t size) { +size_t File :: Read(FILE* fp, void* destination, size_t size) { ULE_TYPES_H_FTAG; return fread(destination, sizeof (char), size + 1, fp); } -s32 File::Write(const char* path, char* data, u32 count) { +s32 File :: Write(const char* path, char* data, u32 count) { ULE_TYPES_H_FTAG; - FILE* fp = File::Open(path, "wb"); + FILE* fp = File :: Open(path, "wb"); if (fp == null) { return -1; // failed to open the file @@ -133,7 +133,7 @@ s32 File::Write(const char* path, char* data, u32 count) { #if _WIN32 #include // writes the filenames into the provided array |outFileNames|, must be allocated ahead of time. -void File::GetFileNamesInFolder(const char* path, Array* outFileNames) { +void File :: GetFileNamesInFolder(const char* path, Array* outFileNames) { ULE_TYPES_H_FTAG; massert(path != null, "provided 'null' for path argument"); massert(outFileNames != null, "provided 'null' for array argument"); @@ -159,7 +159,7 @@ void File::GetFileNamesInFolder(const char* path, Array* outFileNames) { } #else #include -void File::GetFileNamesInFolder(const char* path, Array* outFileNames) { +void File :: GetFileNamesInFolder(const char* path, Array* outFileNames) { ULE_TYPES_H_FTAG; massert(path != null, "provided 'null' for path argument"); massert(outFileNames != null, "provided 'null' for array argument"); @@ -188,7 +188,7 @@ void File::GetFileNamesInFolder(const char* path, Array* outFileNames) { #define stat _stat #endif -time_t File::LastModified(const char* path) { +time_t File :: LastModified(const char* path) { ULE_TYPES_H_FTAG; struct stat result; if (stat(path, &result) == 0) { @@ -199,11 +199,26 @@ time_t File::LastModified(const char* path) { } #undef stat -s32 File::Rename(const char* oldFilename, const char* newFilename) { +s32 File :: Rename(const char* oldFilename, const char* newFilename) { return rename(oldFilename, newFilename); } -s32 File::Remove(const char* path) { +s32 File :: Remove(const char* path) { return remove(path); } +#ifdef _WIN32 +#include +FILE* OpenExclusive(const char* path) { + HFILE result = OpenFile(path, null, OF_SHARE_EXCLUSIVE); + if (result == HFILE_ERROR) { + println("failed"); + } + return null; +} +#else +FILE* OpenExclusive(const char* path) { + return null; +} +#endif + diff --git a/file.h b/file.h index 190a757..ef52997 100644 --- a/file.h +++ b/file.h @@ -14,6 +14,8 @@ namespace File { FILE* Open(const char* path, const char* mode = "rb"); FILE* Open(const char* path, size_t* outSize, const char* mode = "rb"); + FILE* OpenExclusive(const char* path); + void Close(FILE* file); size_t Size(const char* path); diff --git a/print.cpp b/print.cpp index 568d802..a0bc993 100644 --- a/print.cpp +++ b/print.cpp @@ -222,33 +222,47 @@ void trace(String* string) { #undef BACKTRACE_MAX_FRAMES #endif -void _debug(const char* format, ...) { +void _debug( + const char* date, + const char* time, + const char* filename, + const int line, + const char* format, + ... +) { ULE_TYPES_H_FTAG; if (format == null) { - print("%sdebug:%s null\n", ANSI_BLUE, ANSI_RESET); + print("[%s, %s] [%s:%d] %sdebug:%s null\n", date, time, filename, line, ANSI_BLUE, ANSI_RESET); return; } va_list args; va_start(args, format); - print("%sdebug:%s ", ANSI_BLUE, ANSI_RESET); + print("[%s, %s] [%s:%d] %sdebug:%s ", date, time, filename, line, ANSI_BLUE, ANSI_RESET); vprintln(format, args); va_end(args); } -void _warn(const char* format, ...) { +void _warn( + const char* date, + const char* time, + const char* filename, + const int line, + const char* format, + ... +) { ULE_TYPES_H_FTAG; if (format == null) { - print("%swarning:%s null\n", ANSI_YELLOW, ANSI_RESET); + print("[%s, %s] [%s:%d] %swarning:%s null\n", date, time, filename, line, ANSI_YELLOW, ANSI_RESET); return; } va_list args; va_start(args, format); - print("%swarning:%s ", ANSI_YELLOW, ANSI_RESET); + print("[%s, %s] [%s:%d] %swarning:%s ", date, time, filename, line, ANSI_YELLOW, ANSI_RESET); vprintln(format, args); va_end(args); @@ -264,11 +278,18 @@ void setCustomDieBehavior(void (*dieBehavior)(const char* string)) { // for fatal errors which may occur at runtime, even on a release binary. // if a fatal error should not occur at runtime on a release binary, consider preferring 'massert' // it's unclear when you should use asserts vs. die actually. idk man, they kinda do the same thing right now -void die(const char* format, ...) { +void _die( + const char* date, + const char* time, + const char* filename, + const int line, + const char* format, + ... +) { ULE_TYPES_H_FTAG; if (format == null) { if (customDie == null) { - print("%serror:%s (unspecified error)\n", ANSI_RED, ANSI_RESET); + print("[%s, %s] [%s:%d] %serror:%s (unspecified error)", date, time, filename, line, ANSI_RED, ANSI_RESET); trace(); exit(1); return; @@ -285,7 +306,7 @@ void die(const char* format, ...) { va_start(args, format); if (customDie == null) { - println("%serror:%s", ANSI_RED, ANSI_RESET); + println("[%s, %s] [%s:%d] %serror:%s ", date, time, filename, line, ANSI_RED, ANSI_RESET); vprintln(format, args); println(); diff --git a/print.h b/print.h index 685051d..32ec6cc 100644 --- a/print.h +++ b/print.h @@ -86,7 +86,15 @@ extern void trace(String* string = null); extern void writeMinidump(void* exceptionPointers); // This ends the program and calls trace(). generally you should use 'massert' instead -extern void die(const char* format, ...); +void _die( + const char* date, + const char* time, + const char* filename, + const int line, + const char* format, + ... +); +#define die(format, ...) _die(__DATE__, __TIME__, __FILE__, __LINE__, format, ##__VA_ARGS__) // when calling 'die', instead of the default behavior, // (print a stack trace and then call 'exit(1)') @@ -100,12 +108,26 @@ extern void setCustomDieBehavior(void (*dieBehavior)(const char* string)); //#define massert(test, message) (((void)(message), test)) #define massert(test, message) if (!(test)) die("%s\n", message); -extern void _debug(const char* format, ...); -#define debug(format, ...) _debug(format, ##__VA_ARGS__) +extern void _debug( + const char* date, + const char* time, + const char* filename, + const int line, + const char* format, + ... +); +#define debug(format, ...) _debug(__DATE__, __TIME__, __FILE__, __LINE__, format, ##__VA_ARGS__) // @NOTE there's a conflict on win32 with the name 'warning'... -extern void _warn(const char* format, ...); -#define warn(format, ...) _warn(format, ##__VA_ARGS__) +extern void _warn( + const char* date, + const char* time, + const char* filename, + const int line, + const char* format, + ... +); +#define warn(format, ...) _warn(__DATE__, __TIME__, __FILE__, __LINE__, format, ##__VA_ARGS__) #else // define some empty macros diff --git a/serialize.cpp b/serialize.cpp index c889c95..d95fe22 100644 --- a/serialize.cpp +++ b/serialize.cpp @@ -55,41 +55,36 @@ void serialize(String* str, s64 v) { ULE_TYPES_H_FTAG; SERIALIZE_H_FUNC_BODY void serialize(String* str, float v) { ULE_TYPES_H_FTAG; SERIALIZE_H_FUNC_BODY } void serialize(String* str, double v) { ULE_TYPES_H_FTAG; SERIALIZE_H_FUNC_BODY } -template // @TODO do not use a template for this. -static inline void deserializeInteger(char** buffer, T* v) { - ULE_TYPES_H_FTAG; - char* _buffer = *buffer; - T value = 0; - - // skip leading whitespace - all our functions do this, so gotta - while (String::isAsciiWhitespace(*_buffer)) { - _buffer++; - } - - // check the first character for a negative sign - bool negative = false; - if (_buffer[0] == '-') { - negative = true; - _buffer++; - } - - // parse the digits of the number. doesn't account for overflow - while (String::isDigit(*_buffer)) { - value = 10 * value + (*_buffer++ - '0'); - } - - // make it negative if the first character was '-' - if (negative) value *= -1; - - // store the value back into v - *v = value; - - // if the original pointer is the same as the current, we didn't parse anything - massert(_buffer != *buffer, "tried to parse an integer, and found nothing"); - - // report back how far we advanced the buffer - *buffer = _buffer; +#define SERIALIZE_H_FUNC_BODY_INT(T) \ +static inline void deserializeInteger(char** buffer, T* v) { \ + ULE_TYPES_H_FTAG; \ + char* _buffer = *buffer; \ + T value = 0; \ + while (String::isAsciiWhitespace(*_buffer)) { \ + _buffer++; \ + } \ + bool negative = false; \ + if (_buffer[0] == '-') { \ + negative = true; \ + _buffer++; \ + } \ + while (String::isDigit(*_buffer)) { \ + value = 10 * value + (*_buffer++ - '0'); \ + } \ + if (negative) value *= -1; \ + *v = value; \ + massert(_buffer != *buffer, "tried to parse an integer, and found nothing"); \ + *buffer = _buffer; \ } +SERIALIZE_H_FUNC_BODY_INT(u8) +SERIALIZE_H_FUNC_BODY_INT(u16) +SERIALIZE_H_FUNC_BODY_INT(u32) +SERIALIZE_H_FUNC_BODY_INT(u64) +SERIALIZE_H_FUNC_BODY_INT(s8) +SERIALIZE_H_FUNC_BODY_INT(s16) +SERIALIZE_H_FUNC_BODY_INT(s32) +SERIALIZE_H_FUNC_BODY_INT(s64) +#undef SERIALIZE_H_FUNC_BODY_INT // just for integers, signed/unsigned including size_t #define SERIALIZE_H_DESERIALIZE_FUNC_BODY deserializeInteger(buffer, v); diff --git a/string.h b/string.h index 976f48d..e9e0b89 100644 --- a/string.h +++ b/string.h @@ -237,7 +237,6 @@ public: return a; } - static inline void memcpy(void* dest, void* src, u32 size) { ULE_TYPES_H_FTAG; u8* dest_ = (u8*) dest;