diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-28 09:06:29 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-28 09:06:29 +0200 |
| commit | 891bc2653b911a4553a39d03bae4d62d866dbd07 (patch) | |
| tree | 3d32f48d0c181fa7cc31173b122dbfc885c67217 /dev/CompilerKit/utils | |
| parent | 5467549fa5d656afd0c6bf12c6c3928a8c919591 (diff) | |
| parent | a7b43769b2f6dae5abdda4cb2649e43b02fbeea7 (diff) | |
Merge pull request #10 from nekernel-org/dev
v0.0.3e1 — Ogre
Diffstat (limited to 'dev/CompilerKit/utils')
| -rw-r--r-- | dev/CompilerKit/utils/DylibHelpers.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/dev/CompilerKit/utils/DylibHelpers.h b/dev/CompilerKit/utils/DylibHelpers.h index 1ae7c03..fede406 100644 --- a/dev/CompilerKit/utils/DylibHelpers.h +++ b/dev/CompilerKit/utils/DylibHelpers.h @@ -8,6 +8,56 @@ #include <CompilerKit/Defines.h> #include <dlfcn.h> +#include <mutex> + +struct CompilerKitDylibTraits; typedef Int32 (*CompilerKitEntrypoint)(Int32 argc, Char const* argv[]); typedef VoidPtr CompilerKitDylib; + +struct CompilerKitDylibTraits final { + CompilerKitDylib fDylib{nullptr}; + CompilerKitEntrypoint fEntrypoint{nullptr}; + std::mutex fMutex; + + CompilerKitDylibTraits& operator()(const Char* path, const Char* fEntrypoint) { + std::lock_guard<std::mutex> lock(this->fMutex); + + if (!path || !fEntrypoint) return *this; + + if (this->fDylib) { + dlclose(this->fDylib); + this->fDylib = nullptr; + } + + this->fDylib = dlopen(path, RTLD_LAZY); + + if (!this->fDylib) { + return *this; + } + + this->fEntrypoint = (CompilerKitEntrypoint) dlsym(this->fDylib, fEntrypoint); + + if (!this->fEntrypoint) { + dlclose(this->fDylib); + this->fDylib = nullptr; + + return *this; + } + + return *this; + } + + NECTI_COPY_DELETE(CompilerKitDylibTraits); + + CompilerKitDylibTraits() = default; + + ~CompilerKitDylibTraits() { + if (this->fDylib) { + dlclose(this->fDylib); + this->fDylib = nullptr; + } + + this->fEntrypoint = nullptr; + } +}; |
