Browse Source

basic

master
Nick Hayashi 1 year ago
parent
commit
e890423bbb
  1. 14
      alloc.cpp
  2. 55
      file.cpp
  3. 2
      file.h
  4. 39
      print.cpp
  5. 32
      print.h
  6. 63
      serialize.cpp
  7. 1
      string.h

14
alloc.cpp

@ -162,7 +162,7 @@ static u64 alignForward(u64 ptr, size_t alignment) {
//================================================================================ //================================================================================
// Scratch/Arena // Scratch/Arena
Arena* Arena::Init(u32 sizeInBytes) {
Arena* Arena :: Init(u32 sizeInBytes) {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
Arena* arena = (Arena*) pMalloc(sizeof(Arena)); Arena* arena = (Arena*) pMalloc(sizeof(Arena));
arena->index = 0; arena->index = 0;
@ -170,8 +170,15 @@ Arena* Arena::Init(u32 sizeInBytes) {
arena->bufferSizeInBytes = sizeInBytes; arena->bufferSizeInBytes = sizeInBytes;
return arena; return arena;
} }
void* Arena::Alloc(u32 sizeInBytes) {
void* Arena :: Alloc(u32 sizeInBytes) {
ULE_TYPES_H_FTAG; 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; u8* p = this->buffer + this->index;
u32 offset = (u32) alignForward2((u64) p, 64); u32 offset = (u32) alignForward2((u64) p, 64);
@ -188,8 +195,9 @@ void* Arena::Alloc(u32 sizeInBytes) {
} }
return null; return null;
*/
} }
void Arena::Clear() {
void Arena :: Clear() {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
this->index = 0; this->index = 0;
} }

55
file.cpp

@ -10,13 +10,13 @@
#include "types.h" #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; ULE_TYPES_H_FTAG;
return fopen(path, mode); 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; ULE_TYPES_H_FTAG;
FILE* fp = File::Open(path, mode);
FILE* fp = File :: Open(path, mode);
if (fp == null) { if (fp == null) {
return null; return null;
@ -29,13 +29,13 @@ FILE* File::Open(const char* path, size_t* outSize, const char* mode) {
return fp; return fp;
} }
void File::Close(FILE* file) {
void File :: Close(FILE* file) {
fclose(file); fclose(file);
} }
size_t File::Size(const char* path) {
size_t File :: Size(const char* path) {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
FILE* fp = File::Open(path);
FILE* fp = File :: Open(path);
// get the file's size in bytes // get the file's size in bytes
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
size_t size = ftell(fp); size_t size = ftell(fp);
@ -43,7 +43,7 @@ size_t File::Size(const char* path) {
fclose(fp); fclose(fp);
return size; return size;
} }
size_t File::Size(FILE* fp) {
size_t File :: Size(FILE* fp) {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
size_t size = ftell(fp); size_t size = ftell(fp);
@ -51,9 +51,9 @@ size_t File::Size(FILE* fp) {
return size; return size;
} }
u8* File::Read(const char* path) {
u8* File :: Read(const char* path) {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
FILE* fp = File::Open(path, "rb");
FILE* fp = File :: Open(path, "rb");
if (fp == null) { if (fp == null) {
return null; return null;
@ -72,9 +72,9 @@ u8* File::Read(const char* path) {
return (u8*) buffer; 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; ULE_TYPES_H_FTAG;
FILE* fp = File::Open(path, "rb");
FILE* fp = File :: Open(path, "rb");
if (fp == null) { if (fp == null) {
return null; return null;
@ -97,7 +97,7 @@ u8* File::Read(const char* path, size_t* outSize) {
return (u8*) buffer; return (u8*) buffer;
} }
size_t File::Read(FILE* fp, void* destination) {
size_t File :: Read(FILE* fp, void* destination) {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
@ -107,14 +107,14 @@ size_t File::Read(FILE* fp, void* destination) {
fread(destination, sizeof (char), size + 1, fp); fread(destination, sizeof (char), size + 1, fp);
return size; 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; ULE_TYPES_H_FTAG;
return fread(destination, sizeof (char), size + 1, fp); 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; ULE_TYPES_H_FTAG;
FILE* fp = File::Open(path, "wb");
FILE* fp = File :: Open(path, "wb");
if (fp == null) { if (fp == null) {
return -1; // failed to open the file return -1; // failed to open the file
@ -133,7 +133,7 @@ s32 File::Write(const char* path, char* data, u32 count) {
#if _WIN32 #if _WIN32
#include <windows.h> #include <windows.h>
// writes the filenames into the provided array |outFileNames|, must be allocated ahead of time. // writes the filenames into the provided array |outFileNames|, must be allocated ahead of time.
void File::GetFileNamesInFolder(const char* path, Array<char*>* outFileNames) {
void File :: GetFileNamesInFolder(const char* path, Array<char*>* outFileNames) {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
massert(path != null, "provided 'null' for path argument"); massert(path != null, "provided 'null' for path argument");
massert(outFileNames != null, "provided 'null' for array argument"); massert(outFileNames != null, "provided 'null' for array argument");
@ -159,7 +159,7 @@ void File::GetFileNamesInFolder(const char* path, Array<char*>* outFileNames) {
} }
#else #else
#include <dirent.h> #include <dirent.h>
void File::GetFileNamesInFolder(const char* path, Array<char*>* outFileNames) {
void File :: GetFileNamesInFolder(const char* path, Array<char*>* outFileNames) {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
massert(path != null, "provided 'null' for path argument"); massert(path != null, "provided 'null' for path argument");
massert(outFileNames != null, "provided 'null' for array argument"); massert(outFileNames != null, "provided 'null' for array argument");
@ -188,7 +188,7 @@ void File::GetFileNamesInFolder(const char* path, Array<char*>* outFileNames) {
#define stat _stat #define stat _stat
#endif #endif
time_t File::LastModified(const char* path) {
time_t File :: LastModified(const char* path) {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
struct stat result; struct stat result;
if (stat(path, &result) == 0) { if (stat(path, &result) == 0) {
@ -199,11 +199,26 @@ time_t File::LastModified(const char* path) {
} }
#undef stat #undef stat
s32 File::Rename(const char* oldFilename, const char* newFilename) {
s32 File :: Rename(const char* oldFilename, const char* newFilename) {
return rename(oldFilename, newFilename); return rename(oldFilename, newFilename);
} }
s32 File::Remove(const char* path) {
s32 File :: Remove(const char* path) {
return remove(path); return remove(path);
} }
#ifdef _WIN32
#include <winbase.h>
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

2
file.h

@ -14,6 +14,8 @@ namespace File {
FILE* Open(const char* path, const char* mode = "rb"); FILE* Open(const char* path, const char* mode = "rb");
FILE* Open(const char* path, size_t* outSize, 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); void Close(FILE* file);
size_t Size(const char* path); size_t Size(const char* path);

39
print.cpp

@ -222,33 +222,47 @@ void trace(String* string) {
#undef BACKTRACE_MAX_FRAMES #undef BACKTRACE_MAX_FRAMES
#endif #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; ULE_TYPES_H_FTAG;
if (format == null) { 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; return;
} }
va_list args; va_list args;
va_start(args, format); 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); vprintln(format, args);
va_end(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; ULE_TYPES_H_FTAG;
if (format == null) { 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; return;
} }
va_list args; va_list args;
va_start(args, format); 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); vprintln(format, args);
va_end(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. // 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' // 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 // 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; ULE_TYPES_H_FTAG;
if (format == null) { if (format == null) {
if (customDie == 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(); trace();
exit(1); exit(1);
return; return;
@ -285,7 +306,7 @@ void die(const char* format, ...) {
va_start(args, format); va_start(args, format);
if (customDie == null) { 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); vprintln(format, args);
println(); println();

32
print.h

@ -86,7 +86,15 @@ extern void trace(String* string = null);
extern void writeMinidump(void* exceptionPointers); extern void writeMinidump(void* exceptionPointers);
// This ends the program and calls trace(). generally you should use 'massert' instead // 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, // when calling 'die', instead of the default behavior,
// (print a stack trace and then call 'exit(1)') // (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) (((void)(message), test))
#define massert(test, message) if (!(test)) die("%s\n", message); #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'... // @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 #else
// define some empty macros // define some empty macros

63
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, float v) { ULE_TYPES_H_FTAG; SERIALIZE_H_FUNC_BODY }
void serialize(String* str, double v) { ULE_TYPES_H_FTAG; SERIALIZE_H_FUNC_BODY } void serialize(String* str, double v) { ULE_TYPES_H_FTAG; SERIALIZE_H_FUNC_BODY }
template<typename T> // @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 // just for integers, signed/unsigned including size_t
#define SERIALIZE_H_DESERIALIZE_FUNC_BODY deserializeInteger(buffer, v); #define SERIALIZE_H_DESERIALIZE_FUNC_BODY deserializeInteger(buffer, v);

1
string.h

@ -237,7 +237,6 @@ public:
return a; return a;
} }
static inline void memcpy(void* dest, void* src, u32 size) { static inline void memcpy(void* dest, void* src, u32 size) {
ULE_TYPES_H_FTAG; ULE_TYPES_H_FTAG;
u8* dest_ = (u8*) dest; u8* dest_ = (u8*) dest;

Loading…
Cancel
Save