Browse Source

basic

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

8
alloc.cpp

@ -172,6 +172,13 @@ Arena* Arena::Init(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,6 +195,7 @@ void* Arena::Alloc(u32 sizeInBytes) {
}
return null;
*/
}
void Arena :: Clear() {
ULE_TYPES_H_FTAG;

15
file.cpp

@ -207,3 +207,18 @@ s32 File::Remove(const char* 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, size_t* outSize, const char* mode = "rb");
FILE* OpenExclusive(const char* path);
void Close(FILE* file);
size_t Size(const char* path);

39
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();

32
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

65
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<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
#define SERIALIZE_H_DESERIALIZE_FUNC_BODY deserializeInteger(buffer, v);

1
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;

Loading…
Cancel
Save