From 44902cb49cd51e2bb2f1cef05cb3c0e799b83434 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sat, 3 Aug 2024 16:11:57 +0100 Subject: Rework OS abstractions - As much as possible avoid dragging system headers into translation units. This should avoid namespace pollution and, hopefully, speed up builds a little bit. - Avoid leaning on the UCRT so much on Windows - prefer native win32 calls and native file handles except where doing so is inconvenient (in particular, for stat(), which we might try and replace later). - Also, switch from SystemFunction036 to ProcessPrng on Windows. This requires us to generate a stub for bcryptprimitives.dll because Microsoft haven't bothered to provide a link library, but the function is better-documented and seems to be a more direct under-the-hood call as well. Apparently it's what's used by the major web browsers these days, which seems like a good indication it's stable and trusted. - Lastly, remove a bunch of functions and macros and stuff that weren't actually being used. It seems good to try and keep the scope of OS-dependent stuff relatively contained and only add to it when actually required. --- src/build/cmeta.c | 22 +++++++++++----------- src/build/cmeta.h | 4 ++-- src/build/mkentprops.c | 8 ++++---- src/build/mkgamedata.c | 8 ++++---- src/build/skiplist.h | 3 ++- 5 files changed, 23 insertions(+), 22 deletions(-) (limited to 'src/build') diff --git a/src/build/cmeta.c b/src/build/cmeta.c index 0c9b3ca..8a2416d 100644 --- a/src/build/cmeta.c +++ b/src/build/cmeta.c @@ -1,5 +1,5 @@ /* - * Copyright © 2023 Michael Smith + * Copyright © 2024 Michael Smith * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -102,15 +102,15 @@ static void die(const char *s) { exit(100); } -static char *readsource(const os_char *f) { - int fd = os_open(f, O_RDONLY); - if (fd == -1) return 0; +static char *readsource(const os_char *path) { + int f = os_open_read(path); + if (f == -1) return 0; uint bufsz = 8192; char *buf = malloc(bufsz); if (!buf) die("couldn't allocate memory"); int nread; int off = 0; - while ((nread = read(fd, buf + off, bufsz - off)) > 0) { + while ((nread = os_read(f, buf + off, bufsz - off)) > 0) { off += nread; if (off == bufsz) { bufsz *= 2; @@ -122,24 +122,24 @@ static char *readsource(const os_char *f) { } if (nread == -1) die("couldn't read file"); buf[off] = 0; - close(fd); + os_close(f); return buf; } // as per cmeta.h this is totally opaque; it's actually just a Token in disguise struct cmeta; -const struct cmeta *cmeta_loadfile(const os_char *f) { - char *buf = readsource(f); +const struct cmeta *cmeta_loadfile(const os_char *path) { + char *buf = readsource(path); if (!buf) return 0; #ifdef _WIN32 - char *realname = malloc(wcslen(f) + 1); + char *realname = malloc(wcslen(path) + 1); if (!realname) die("couldn't allocate memory"); // XXX: being lazy about Unicode right now; a general purpose tool should // implement WTF8 or something. SST itself doesn't have any unicode paths // though, so don't really care as much. - *realname = *f; - for (const ushort *p = f + 1; p[-1]; ++p) realname[p - f] = *p; + *realname = *path; + for (const ushort *p = path + 1; p[-1]; ++p) realname[p - path] = *p; #else const char *realname = f; #endif diff --git a/src/build/cmeta.h b/src/build/cmeta.h index 48a2d6d..86ae0ec 100644 --- a/src/build/cmeta.h +++ b/src/build/cmeta.h @@ -1,5 +1,5 @@ /* - * Copyright © 2022 Michael Smith + * Copyright © 2024 Michael Smith * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,7 +21,7 @@ struct cmeta; -const struct cmeta *cmeta_loadfile(const os_char *f); +const struct cmeta *cmeta_loadfile(const os_char *path); /* * Iterates through all the #include directives in a file, passing each one in diff --git a/src/build/mkentprops.c b/src/build/mkentprops.c index 0781f25..2043a9b 100644 --- a/src/build/mkentprops.c +++ b/src/build/mkentprops.c @@ -174,13 +174,13 @@ _( "}") int OS_MAIN(int argc, os_char *argv[]) { for (++argv; *argv; ++argv) { - int fd = os_open(*argv, O_RDONLY); - if (fd == -1) die("couldn't open file"); + int f = os_open_read(*argv); + if (f == -1) die("couldn't open file"); struct kv_parser kv = {0}; struct parsestate state = {*argv, &kv}; char buf[1024]; int nread; - while (nread = read(fd, buf, sizeof(buf))) { + while (nread = os_read(f, buf, sizeof(buf))) { if (nread == -1) die("couldn't read file"); if (!kv_parser_feed(&kv, buf, nread, &kv_cb, &state)) goto ep; } @@ -189,7 +189,7 @@ ep: fprintf(stderr, "mkentprops: %" fS ":%d:%d: bad syntax: %s\n", *argv, kv.line, kv.col, kv.errmsg); exit(1); } - close(fd); + os_close(f); } FILE *out = fopen(".build/include/entprops.gen.h", "wb"); diff --git a/src/build/mkgamedata.c b/src/build/mkgamedata.c index d49eef5..bf359b2 100644 --- a/src/build/mkgamedata.c +++ b/src/build/mkgamedata.c @@ -1,5 +1,5 @@ /* - * Copyright © 2022 Michael Smith + * Copyright © 2024 Michael Smith * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -211,13 +211,13 @@ _( "}") int OS_MAIN(int argc, os_char *argv[]) { for (++argv; *argv; ++argv) { - int fd = os_open(*argv, O_RDONLY); + int fd = os_open_read(*argv); if (fd == -1) die("couldn't open file"); struct kv_parser kv = {0}; struct parsestate state = {*argv, &kv, &root}; char buf[1024]; int nread; - while (nread = read(fd, buf, sizeof(buf))) { + while (nread = os_read(fd, buf, sizeof(buf))) { if (nread == -1) die("couldn't read file"); if (!kv_parser_feed(&kv, buf, nread, &kv_cb, &state)) goto ep; } @@ -226,7 +226,7 @@ ep: fprintf(stderr, "mkgamedata: %" fS ":%d:%d: bad syntax: %s\n", *argv, kv.line, kv.col, kv.errmsg); exit(1); } - close(fd); + os_close(fd); } FILE *out = fopen(".build/include/gamedata.gen.h", "wb"); diff --git a/src/build/skiplist.h b/src/build/skiplist.h index b747d89..ab6a920 100644 --- a/src/build/skiplist.h +++ b/src/build/skiplist.h @@ -1,5 +1,5 @@ /* - * Copyright © 2022 Michael Smith + * Copyright © 2024 Michael Smith * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -24,6 +24,7 @@ #ifdef _WIN32 static inline int _skiplist_ffs(uint x) { + uchar _BitScanForward(ulong *idx, ulong mask); uint ret; // on Windows, sizeof(ulong) == sizeof(uint) if (_BitScanForward((ulong *)&ret, x)) return ret + 1; else return 0; -- cgit v1.2.3