Commit: aab6bcdfb4852018775831a6663901dafe01adcd
Parent: 266448a5f5056b5cc502040a21f560bf1df35f75
Author: Randy Palamar
Date: Mon, 7 Oct 2024 14:32:27 -0600
remove stdio stderr and vfprintf from our code
Diffstat:
M | main.c | | | 53 | ++++++++++++++++++++++++++++++++++------------------- |
M | os_unix.c | | | 22 | ++++++++++++++++------ |
M | os_win32.c | | | 33 | ++++++++++++++++++++++++--------- |
M | util.c | | | 15 | +++++---------- |
4 files changed, 79 insertions(+), 44 deletions(-)
diff --git a/main.c b/main.c
@@ -1,13 +1,13 @@
/* See LICENSE for license details. */
#include "beamformer.h"
-static char *compute_shader_paths[CS_LAST] = {
- [CS_HADAMARD] = "shaders/hadamard.glsl",
- [CS_HERCULES] = "shaders/hercules.glsl",
- [CS_DEMOD] = "shaders/demod.glsl",
- [CS_MIN_MAX] = "shaders/min_max.glsl",
- [CS_SUM] = "shaders/sum.glsl",
- [CS_UFORCES] = "shaders/uforces.glsl",
+static s8 compute_shader_paths[CS_LAST] = {
+ [CS_HADAMARD] = s8("shaders/hadamard.glsl"),
+ [CS_HERCULES] = s8("shaders/hercules.glsl"),
+ [CS_DEMOD] = s8("shaders/demod.glsl"),
+ [CS_MIN_MAX] = s8("shaders/min_max.glsl"),
+ [CS_SUM] = s8("shaders/sum.glsl"),
+ [CS_UFORCES] = s8("shaders/uforces.glsl"),
};
#ifndef _DEBUG
@@ -65,14 +65,21 @@ gl_debug_logger(u32 src, u32 type, u32 id, u32 lvl, i32 len, const char *msg, co
static void
get_gl_params(GLParams *gl)
{
- const u8 *vendor = glGetString(GL_VENDOR);
- if (!vendor)
- die("Failed to determine GL Vendor\n");
+ char *vendor = (char *)glGetString(GL_VENDOR);
+ if (!vendor) {
+ os_write_err_msg(s8("Failed to determine GL Vendor\n"));
+ os_fail();
+ }
switch (vendor[0]) {
- case 'A': gl->vendor_id = GL_VENDOR_AMD; break;
- case 'I': gl->vendor_id = GL_VENDOR_INTEL; break;
- case 'N': gl->vendor_id = GL_VENDOR_NVIDIA; break;
- default: die("Unknown GL Vendor: %s\n", vendor); break;
+ case 'A': gl->vendor_id = GL_VENDOR_AMD; break;
+ case 'I': gl->vendor_id = GL_VENDOR_INTEL; break;
+ case 'N': gl->vendor_id = GL_VENDOR_NVIDIA; break;
+ default: {
+ os_write_err_msg(s8("Unknown GL Vendor: "));
+ os_write_err_msg(cstr_to_s8(vendor));
+ os_write_err_msg(s8("\n"));
+ os_fail();
+ } break;
}
glGetIntegerv(GL_MAJOR_VERSION, &gl->version_major);
@@ -87,8 +94,10 @@ static void
validate_gl_requirements(GLParams *gl)
{
ASSERT(gl->max_ubo_size >= sizeof(BeamformerParameters));
- if (gl->version_major < 4 || (gl->version_major == 4 && gl->version_minor < 5))
- die("Only OpenGL Versions 4.5 or newer are supported!\n");
+ if (gl->version_major < 4 || (gl->version_major == 4 && gl->version_minor < 5)) {
+ os_write_err_msg(s8("Only OpenGL Versions 4.5 or newer are supported!\n"));
+ os_fail();
+ }
}
static void
@@ -164,12 +173,18 @@ reload_shaders(BeamformerCtx *ctx, Arena a)
{
ComputeShaderCtx *csctx = &ctx->csctx;
for (u32 i = 0; i < ARRAY_COUNT(csctx->programs); i++) {
- if (!compute_shader_paths[i])
+ if (!compute_shader_paths[i].len)
continue;
Arena tmp = a;
- FileStats fs = os_get_file_stats(compute_shader_paths[i]);
- s8 shader_text = os_read_file(&tmp, compute_shader_paths[i], fs.filesize);
+ FileStats fs = os_get_file_stats((char *)compute_shader_paths[i].data);
+ s8 shader_text = os_read_file(&tmp, (char *)compute_shader_paths[i].data, fs.filesize);
+ if (shader_text.len == -1) {
+ os_write_err_msg(s8("failed to read shader: "));
+ os_write_err_msg(compute_shader_paths[i]);
+ os_write_err_msg(s8("\n"));
+ os_fail();
+ }
u32 shader_id = compile_shader(tmp, GL_COMPUTE_SHADER, shader_text);
if (shader_id) {
diff --git a/os_unix.c b/os_unix.c
@@ -20,6 +20,12 @@ os_write_err_msg(s8 msg)
write(STDERR_FILENO, msg.data, msg.len);
}
+static void __attribute__((noreturn))
+os_fail(void)
+{
+ _exit(1);
+}
+
static Arena
os_alloc_arena(Arena a, size capacity)
{
@@ -35,8 +41,10 @@ os_alloc_arena(Arena a, size capacity)
munmap(a.beg, oldsize);
a.beg = mmap(0, capacity, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
- if (a.beg == MAP_FAILED)
- die("os_alloc_arena: couldn't allocate memory\n");
+ if (a.beg == MAP_FAILED) {
+ os_write_err_msg(s8("os_alloc_arena: couldn't allocate memory\n"));
+ os_fail();
+ }
a.end = a.beg + capacity;
return a;
}
@@ -44,17 +52,19 @@ os_alloc_arena(Arena a, size capacity)
static s8
os_read_file(Arena *a, char *fname, size fsize)
{
+ if (fsize < 0)
+ return (s8){.len = -1};
+
i32 fd = open(fname, O_RDONLY);
if (fd < 0)
- die("os_read_file: couldn't open file: %s\n", fname);
-
- s8 ret = s8alloc(a, fsize);
+ return (s8){.len = -1};
+ s8 ret = s8alloc(a, fsize);
size rlen = read(fd, ret.data, ret.len);
close(fd);
if (rlen != ret.len)
- die("os_read_file: couldn't read file: %s\n", fname);
+ return (s8){.len = -1};
return ret;
}
diff --git a/os_win32.c b/os_win32.c
@@ -55,6 +55,7 @@ W32(void *) CreateFileA(c8 *, u32, u32, void *, u32, u32, void *);
W32(void *) CreateFileMappingA(void *, void *, u32, u32, u32, c8 *);
W32(void *) CreateNamedPipeA(c8 *, u32, u32, u32, u32, u32, u32, void *);
W32(b32) DeleteFileA(c8 *);
+W32(void) ExitProcess(i32);
W32(b32) FreeLibrary(void *);
W32(b32) GetFileInformationByHandle(void *, void *);
W32(i32) GetLastError(void);
@@ -78,6 +79,12 @@ typedef struct {
typedef void *os_library_handle;
+static void __attribute__((noreturn))
+os_fail(void)
+{
+ ExitProcess(1);
+}
+
static void
os_write_err_msg(s8 msg)
{
@@ -104,8 +111,10 @@ os_alloc_arena(Arena a, size capacity)
VirtualFree(a.beg, oldsize, MEM_RELEASE);
a.beg = VirtualAlloc(0, capacity, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
- if (a.beg == NULL)
- die("os_alloc_arena: couldn't allocate memory\n");
+ if (a.beg == NULL) {
+ os_write_err_msg("os_alloc_arena: couldn't allocate memory\n");
+ os_fail();
+ }
a.end = a.beg + capacity;
return a;
}
@@ -113,20 +122,26 @@ os_alloc_arena(Arena a, size capacity)
static s8
os_read_file(Arena *a, char *fname, size fsize)
{
- if (fsize > (size)U32_MAX)
- die("os_read_file: %s\nHandling files >4GB is not yet "
- "handled in win32 code\n", fname);
+ if (fsize < 0)
+ return (s8){.len = -1};
+
+ if (fsize > (size)U32_MAX) {
+ os_write_err_msg(s8("os_read_file: Handling files >4GB is not yet handled"
+ "in win32 code\n"));
+ return (s8){.len = -1};
+ }
void *h = CreateFileA(fname, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (h == INVALID_HANDLE_VALUE)
- die("os_read_file: couldn't open file: %s\n", fname);
+ return (s8){.len = -1};
s8 ret = s8alloc(a, fsize);
- i32 rlen = 0;
- if (!ReadFile(h, ret.data, ret.len, &rlen, 0) && rlen != ret.len)
- die("os_read_file: couldn't read file: %s\n", fname);
+ i32 rlen = 0;
+ b32 error = !ReadFile(h, ret.data, ret.len, &rlen, 0) || rlen != ret.len;
CloseHandle(h);
+ if (error)
+ return (s8){.len = -1};
return ret;
}
diff --git a/util.c b/util.c
@@ -103,17 +103,12 @@ stream_append_f32_e(Stream *s, f32 f)
s->widx += snprintf((char *)(s->data + s->widx), remaining, "%0.02e", f);
}
-
-static void __attribute__((noreturn))
-die(char *fmt, ...)
+static s8
+cstr_to_s8(char *cstr)
{
- va_list ap;
-
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
-
- exit(1);
+ s8 result = {.data = (u8 *)cstr};
+ while (*cstr) { result.len++; cstr++; }
+ return result;
}
static s8