diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-22 15:39:40 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-08-22 15:39:40 +0200 |
| commit | 209373b1f5770dc175e06996a152df6484f59af2 (patch) | |
| tree | 93801515752e81a7ce9e5de91bf625d03bb9b4fa /dev/CompilerKit/utils | |
| parent | a7b7a7d499578660be897313b30a13963cc9ffd5 (diff) | |
feat: implement `CompilerKitDylibTraits` container for future and
current frontend
tools.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
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..31e0b68 100644 --- a/dev/CompilerKit/utils/DylibHelpers.h +++ b/dev/CompilerKit/utils/DylibHelpers.h @@ -7,7 +7,57 @@ #pragma once #include <CompilerKit/Defines.h> +#include <mutex> #include <dlfcn.h> +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; + } +}; |
