Commit: 22c274a875d377725f40cdc3b74ca89bba21c5b3
Parent: 2ae20332c5382025f4007425ab5313ba5c20b041
Author: Randy Palamar
Date: Thu, 15 Aug 2024 14:05:02 -0600
add os specific library loading code
Diffstat:
M | main.c | | | 22 | ++++------------------ |
M | os_unix.c | | | 31 | +++++++++++++++++++++++++++++++ |
M | os_win32.c | | | 28 | ++++++++++++++++++++++++++++ |
3 files changed, 63 insertions(+), 18 deletions(-)
diff --git a/main.c b/main.c
@@ -14,11 +14,10 @@ static char *compute_shader_paths[CS_LAST] = {
static void do_debug(void) { }
#else
-#include <dlfcn.h>
#include <time.h>
static char *libname = "./beamformer.so";
-static void *libhandle;
+static os_library_handle libhandle;
typedef void do_beamformer_fn(BeamformerCtx *, Arena);
static do_beamformer_fn *do_beamformer;
@@ -37,21 +36,6 @@ filetime_is_newer(struct timespec a, struct timespec b)
}
static void
-load_library(const char *lib)
-{
- /* NOTE: glibc is buggy gnuware so we need to check this */
- if (libhandle)
- dlclose(libhandle);
- libhandle = dlopen(lib, RTLD_NOW|RTLD_LOCAL);
- if (!libhandle)
- TraceLog(LOG_ERROR, "do_debug: dlopen: %s\n", dlerror());
-
- do_beamformer = dlsym(libhandle, "do_beamformer");
- if (!do_beamformer)
- TraceLog(LOG_ERROR, "do_debug: dlsym: %s\n", dlerror());
-}
-
-static void
do_debug(void)
{
static os_filetime updated_time;
@@ -59,7 +43,9 @@ do_debug(void)
if (filetime_is_newer(test_time, updated_time)) {
struct timespec sleep_time = {.tv_sec = 0, .tv_nsec = 100e6};
nanosleep(&sleep_time, &sleep_time);
- load_library(libname);
+ os_close_library(libhandle);
+ libhandle = os_load_library(libname);
+ do_beamformer = os_lookup_dynamic_symbol(libhandle, "do_beamformer");
updated_time = test_time;
}
}
diff --git a/os_unix.c b/os_unix.c
@@ -1,3 +1,4 @@
+#include <dlfcn.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/mman.h>
@@ -13,6 +14,8 @@ typedef struct {
typedef struct timespec os_filetime;
+typedef void *os_library_handle;
+
typedef struct {
size filesize;
os_filetime timestamp;
@@ -134,3 +137,31 @@ os_remove_shared_memory(char *name)
{
shm_unlink(name);
}
+
+static os_library_handle
+os_load_library(char *name)
+{
+ os_library_handle res = dlopen(name, RTLD_NOW|RTLD_LOCAL);
+ if (!res)
+ TraceLog(LOG_WARNING, "os_load_library(%s): %s\n", name, dlerror());
+ return res;
+}
+
+static void *
+os_lookup_dynamic_symbol(os_library_handle h, char *name)
+{
+ void *res = dlsym(h, name);
+ if (!res)
+ TraceLog(LOG_WARNING, "os_lookup_dynamic_symbol(%s): %s\n", name, dlerror());
+ return res;
+}
+
+#ifdef _DEBUG
+static void
+os_close_library(os_library_handle h)
+{
+ /* NOTE: glibc is buggy gnuware so we need to check this */
+ if (h)
+ dlclose(h);
+}
+#endif /* _DEBUG */
diff --git a/os_win32.c b/os_win32.c
@@ -1,6 +1,7 @@
/* See LICENSE for license details. */
#include <fileapi.h>
#include <handleapi.h>
+#include <libloaderapi.h>
#include <memoryapi.h>
#include <sysinfoapi.h>
@@ -18,6 +19,7 @@ typedef struct {
} os_pipe;
typedef FILETIME os_filetime;
+typedef HANDLE os_library_handle;
typedef struct {
size filesize;
@@ -141,3 +143,29 @@ static void
os_remove_shared_memory(char *name)
{
}
+
+static os_library_handle
+os_load_library(char *name)
+{
+ os_library_handle res = LoadLibraryA(name);
+ if (!res)
+ TraceLog(LOG_WARNING, "os_load_library(%s): %d\n", name, GetLastError());
+ return res;
+}
+
+static void *
+os_lookup_dynamic_symbol(os_library_handle h, char *name)
+{
+ void *res = GetProcAddress(h, name);
+ if (!res)
+ TraceLog(LOG_WARNING, "os_lookup_dynamic_symbol(%s): %s\n", name, GetLastError());
+ return res;
+}
+
+#ifdef _DEBUG
+static void
+os_close_library(os_library_handle h)
+{
+ FreeLibrary(h);
+}
+#endif /* _DEBUG */