Daniel Ledda vor 2 Wochen
Ursprung
Commit
49e379dd54
9 geänderte Dateien mit 80 neuen und 44 gelöschten Zeilen
  1. +16
    -6
      app.cpp
  2. +1
    -1
      build
  3. +1
    -1
      build.bat
  4. +44
    -25
      core.cpp
  5. +3
    -2
      core.h
  6. +1
    -1
      os.h
  7. +6
    -3
      os_linux.cpp
  8. +6
    -5
      os_win32.cpp
  9. +2
    -0
      vendor/stb_sprintf.h

+ 16
- 6
app.cpp Datei anzeigen

@@ -1,5 +1,6 @@
#include <stdio.h>
#include "core.cpp"
#include "core.h"

int main(int argc, char **argv) {
int statusCode = 0;
@@ -7,14 +8,23 @@ int main(int argc, char **argv) {
Arena *arena = arenaAlloc(Megabytes(64));
list<string> args = getArgs(arena, argc, argv);

log(strSplit(arena, strlit("-"), strlit("hallo-world")));
log(strSplit(arena, "-"_s, "hallo-world"_s));

while (true) {
size_t arenaPos = arena->head;
string line = PushString(arena, 128);
fgets(line.str, (int)line.length, stdin);
log(strSplit(arena, strlit("-"), line));
arenaFreeFrom(arena, arenaPos);
string line;
list<string> split;

WithScratch(temp) {
line = PushString(temp.arena, 128);
fgets(line.str, (int)line.length, stdin);
split = strSplit(temp.arena, "-"_s, line);
}

if (line.str[0] == '\n' && line.str[1] == '\0') {
break;
} else {
log(split);
}
}

return statusCode;


+ 1
- 1
build Datei anzeigen

@@ -1,3 +1,3 @@
#!/bin/bash

gcc -g -g3 ./app.cpp -o ./target/app
g++ -g -g3 -lm -DOS_LINUX=1 -DENABLE_ASSERT=1 ./app.cpp -o ./target/app

+ 1
- 1
build.bat Datei anzeigen

@@ -5,7 +5,7 @@ if NOT EXIST .\target mkdir .\target
set commonLinkerFlags=-opt:ref
set commonCompilerFlags=^
-MT %= Make sure the C runtime library is statically linked =%^
-Gm- %= Turns off incremently building =%^
-Gm- %= Turns off incremental building =%^
-nologo %= No one cares you made the compiler Microsoft =%^
-Oi %= Always use intrinsics =%^
-EHa- %= Disable exception handling =%^


+ 44
- 25
core.cpp Datei anzeigen

@@ -1,6 +1,9 @@
#include <unistd.h> // TODO(djledda): get outta here
#include <math.h>
#include <string.h>
#define STB_SPRINTF_IMPLEMENTATION
#include "core.h"
#include "os.h"
#include "os.cpp"

void *pushSize(Arena *arena, size_t bytes) {
if (arena->capacity - arena->head >= bytes) {
@@ -20,13 +23,7 @@ Arena *arenaAlloc(size_t capacity) {
}

void arenaFree(Arena *arena) {
os_free(arena);
#if OS_WINDOWS
VirtualFree(arena, NULL, MEM_RELEASE);
#elif OS_LINUX
// TODO(dledda): implement this for Linux
Assert(false);
#endif
os_free(arena, arena->capacity);
}

void arenaFreeFrom(Arena *arena, size_t position) {
@@ -60,6 +57,9 @@ Scratch scratchStart(Arena **conflicts, size_t conflictCount) {
return scratch;
}

#define DeferLoop(begin_stmnt, end_stmnt) for(int __defer_i = ((begin_stmnt), 0); __defer_i < 1; (++__defer_i, (end_stmnt)))
#define WithScratch(scratchName) Scratch scratchName; DeferLoop(scratchName = scratchStart(0, 0), scratchEnd(scratchName))

void scratchEnd(Scratch scratch) {
arenaFreeFrom(scratch.arena, scratch.start);
}
@@ -86,6 +86,13 @@ void zeroList(list<T> *list) {
memset(list->data, 0, list->head * sizeof(T));
}

inline string operator""_s(const char *cstrLiteral, unsigned long length) {
return {
(char *)cstrLiteral,
length,
};
}

const char *cstring(Arena *arena, list<char> buf) {
char *arr = PushArray(arena, char, buf.length + 1);
memmove(arr, buf.data, buf.length);
@@ -166,7 +173,7 @@ list<T> listSlice(list<T> l, size_t start, size_t stop) {
if (stop == 0) {
stop = l.head;
}
// TODO(dledda): maybe assert instead
// TODO(djledda): maybe assert instead
if (stop > l.head || start > stop) {
return {0};
}
@@ -181,7 +188,7 @@ string strSlice(string str, size_t start, size_t stop) {
if (stop == 0) {
stop = str.length;
}
// TODO(dledda): maybe assert instead
// TODO(djledda): maybe assert instead
if (stop > str.length || start > stop) {
return {0};
}
@@ -207,9 +214,9 @@ bool stringContains(string str, char c) {
return false;
}

const char NUMERIC_CHARS[] = "0123456789";
string NUMERIC_CHARS = "0123456789"_s;
inline bool isNumeric(char c) {
return stringContains(strlit(NUMERIC_CHARS), c);
return stringContains(NUMERIC_CHARS, c);
}

list<string> strSplit(Arena *arena, string splitStr, string inputStr) {
@@ -317,17 +324,16 @@ string readEntireFile(Arena *arena, string filename) {
struct stat st;
stat((char *)filename.str, &st);
size_t fsize = st.st_size;
string readBuffer = PushString(arena, filesize);
readBuffer.length = filesize;
fread(readBuffer.str, sizeof(byte), filesize, input);
string readBuffer = PushString(arena, fsize);
fread(readBuffer.str, sizeof(byte), readBuffer.length, input);
fclose(input);
return readBuffer;
#endif
}

bool writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
#if OS_WINDOWS
bool result = false;
#if OS_WINDOWS
HANDLE fileHandle = CreateFileA(cstring(arena, filename), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL);
if (fileHandle != INVALID_HANDLE_VALUE) {
DWORD bytesWritten;
@@ -337,15 +343,15 @@ bool writeEntireFile(Arena *arena, string filename, const byte *contents, size_t
}
CloseHandle(fileHandle);
}
return result;
#elif OS_LINUX
Assert(false);
#endif
return result;
}

bool fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
#if OS_WINDOWS
bool result = false;
#if OS_WINDOWS
HANDLE fileHandle = CreateFileA(cstring(arena, filename), FILE_APPEND_DATA | FILE_GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle != INVALID_HANDLE_VALUE) {
DWORD bytesWritten;
@@ -356,10 +362,10 @@ bool fileAppend(Arena *arena, string filename, const byte *contents, size_t cont
}
CloseHandle(fileHandle);
}
return result;
#elif OS_LINUX
Assert(false);
#endif
return result;
}

list<string> getArgs(Arena *arena, int argc, char **argv) {
@@ -382,7 +388,7 @@ Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp) {
}

string formatTimeHms(Arena *arena, UnixTimestamp time) {
local_persist const string format = strlit("HH-MM-SS");
local_persist const string format = "HH-MM-SS"_s;
string buf = PushString(arena, format.length);
tm *timestamp = gmtime((time_t *)&time);
strftime(buf.str, buf.length + 1, "%T", timestamp);
@@ -390,14 +396,14 @@ string formatTimeHms(Arena *arena, UnixTimestamp time) {
}

string formatTimeHms(Arena *arena, Timestamp *time) {
local_persist const string format = strlit("HH-MM-SS");
local_persist const string format = "HH-MM-SS"_s;
string buf = PushString(arena, format.length);
strftime(buf.str, buf.length + 1, "%T", (tm *)time);
return buf;
}

string formatTimeYmd(Arena *arena, UnixTimestamp time) {
local_persist const string format = strlit("YYYY-mm-dd");
local_persist const string format = "YYYY-mm-dd"_s;
string buf = PushString(arena, format.length);
tm *timestamp = gmtime((time_t *)&time);
strftime(buf.str, buf.length + 1, "%Y-%m-%d", timestamp);
@@ -405,7 +411,7 @@ string formatTimeYmd(Arena *arena, UnixTimestamp time) {
}

string formatTimeYmd(Arena *arena, Timestamp *time) {
local_persist const string format = strlit("YYYY-mm-dd");
local_persist const string format = "YYYY-mm-dd"_s;
string buf = PushString(arena, format.length);
strftime(buf.str, buf.length + 1, "%Y-%m-%d", (tm *)time);
return buf;
@@ -433,8 +439,21 @@ function void __core_log(LogTarget target, const char *fmt, va_list argList) {
}
WriteFile(stdHandle, result.str, (DWORD)result.length, &done, 0);
#elif OS_LINUX
// TODO(dledda): implementation
Assert(false);
// TODO(djledda): finish implementation without cstdlib
switch (target) {
case LogTarget_stdin:
write(0, (const void *)result.str, result.length);
break;
case LogTarget_stderr:
fflush(stderr);
write(2, (const void *)result.str, result.length);
break;
case LogTarget_stdout:
default:
fflush(stdout);
write(1, (const void *)result.str, result.length);
break;
}
#endif
scratchEnd(scratch);
}


+ 3
- 2
core.h Datei anzeigen

@@ -5,7 +5,7 @@
#include <math.h>
#include <stdint.h> // necessary for int type sizes
#include <stdio.h>
#include <time.h> // TODO(dledda): try not to depend on this one
#include <time.h> // TODO(djledda): try not to depend on this one

// ### Misc macros ###
#if ENABLE_ASSERT
@@ -150,6 +150,7 @@ struct string {

#define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1})
#define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) })
string operator""_s(const char *cstrLiteral, unsigned long length);

// C Strings
const char *cstring(Arena *arena, list<char> buf);
@@ -192,7 +193,7 @@ string formatTimeYmd(Arena *arena, UnixTimestamp time);
string formatTimeYmd(Arena *arena, Timestamp *time);

// ### Linked Lists ###
// TODO(dledda): implement basic linked lists (based on arenas?)
// TODO(djledda): implement basic linked lists (based on arenas?)

// ### Logging ###
enum LogTarget {


+ 1
- 1
os.h Datei anzeigen

@@ -7,6 +7,6 @@
void *os_alloc(size_t capacity);
void os_reserve(void *ptr);
void os_decommit(void *ptr);
void os_free(void *ptr);
void os_free(void *ptr, size_t freeSize);

#endif

+ 6
- 3
os_linux.cpp Datei anzeigen

@@ -1,21 +1,24 @@
#ifndef OS_IMPL_LINUX_CPP
#define OS_IMPL_LINUX_CPP

#include "os.h"

#include <sys/mman.h>
#include <sys/stat.h>
#include "os.h"

void *os_alloc(size_t capacity) {
return mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
}

void os_reserve(void *ptr) {
void os_commit(void *ptr) {
}

void os_decommit(void *ptr) {
}

void os_free(void *ptr) {
void os_free(void *ptr, size_t size) {
int err = munmap(ptr, size);
Assert(err != -1);
}

#endif

+ 6
- 5
os_win32.cpp Datei anzeigen

@@ -4,17 +4,18 @@
#include "os.h"
#include "Windows.h"

void *osAlloc(size_t capacity) {
return VirtualAlloc(NULL, sizeof(Arena) + capacity, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
void *os_alloc(size_t commitSize) {
return VirtualAlloc(NULL, commitSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
}

void osReserve(void *ptr) {
void os_reserve(void *ptr) {
}

void osDecommit(void *ptr) {
void os_decommit(void *ptr) {
}

void osFree(void *ptr) {
void os_free(void *ptr, size_t size) {
VirtualFree(ptr, NULL, MEM_RELEASE);
}

#endif

+ 2
- 0
vendor/stb_sprintf.h Datei anzeigen

@@ -603,6 +603,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
goto scopy;

case 'S':
{
// string struct
string str = va_arg(va, string);
s = (char *)str.str;
@@ -614,6 +615,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
dp = 0;
cs = 0;
goto scopy;
} break;

case 'c': // char
// get the character


Laden…
Abbrechen
Speichern