diff --git a/source/backend/plugin/CarlaPluginInternal.cpp b/source/backend/plugin/CarlaPluginInternal.cpp index 0cc22f1e6..30246a160 100644 --- a/source/backend/plugin/CarlaPluginInternal.cpp +++ b/source/backend/plugin/CarlaPluginInternal.cpp @@ -684,11 +684,6 @@ bool CarlaPlugin::ProtectedData::libClose() noexcept return ret; } -void* CarlaPlugin::ProtectedData::libSymbol(const char* const symbol) const noexcept -{ - return lib_symbol(lib, symbol); -} - bool CarlaPlugin::ProtectedData::uiLibOpen(const char* const fname, const bool canDelete) noexcept { uiLib = sLibCounter.open(fname, canDelete); @@ -702,11 +697,6 @@ bool CarlaPlugin::ProtectedData::uiLibClose() noexcept return ret; } -void* CarlaPlugin::ProtectedData::uiLibSymbol(const char* const symbol) const noexcept -{ - return lib_symbol(uiLib, symbol); -} - // ----------------------------------------------------------------------- void CarlaPlugin::ProtectedData::tryTransient() noexcept diff --git a/source/backend/plugin/CarlaPluginInternal.hpp b/source/backend/plugin/CarlaPluginInternal.hpp index 6d23706b1..7919680d9 100644 --- a/source/backend/plugin/CarlaPluginInternal.hpp +++ b/source/backend/plugin/CarlaPluginInternal.hpp @@ -21,6 +21,7 @@ #include "CarlaPlugin.hpp" #include "CarlaPluginThread.hpp" +#include "CarlaLibUtils.hpp" #include "CarlaOscUtils.hpp" #include "CarlaStateUtils.hpp" @@ -330,13 +331,23 @@ struct CarlaPlugin::ProtectedData { static const char* libError(const char* const filename) noexcept; - bool libOpen(const char* const filename) noexcept; - bool libClose() noexcept; - void* libSymbol(const char* const symbol) const noexcept; + bool libOpen(const char* const filename) noexcept; + bool libClose() noexcept; - bool uiLibOpen(const char* const filename, const bool canDelete) noexcept; - bool uiLibClose() noexcept; - void* uiLibSymbol(const char* const symbol) const noexcept; + bool uiLibOpen(const char* const filename, const bool canDelete) noexcept; + bool uiLibClose() noexcept; + + template + Func libSymbol(const char* const symbol) const noexcept + { + return lib_symbol(lib, symbol); + } + + template + Func uiLibSymbol(const char* const symbol) const noexcept + { + return lib_symbol(uiLib, symbol); + } // ------------------------------------------------------------------- // Misc diff --git a/source/backend/plugin/DssiPlugin.cpp b/source/backend/plugin/DssiPlugin.cpp index f5adb2ab7..0bfa5eaac 100644 --- a/source/backend/plugin/DssiPlugin.cpp +++ b/source/backend/plugin/DssiPlugin.cpp @@ -2025,7 +2025,7 @@ public: // --------------------------------------------------------------- // get DLL main entry - const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)pData->libSymbol("dssi_descriptor"); + const DSSI_Descriptor_Function descFn = pData->libSymbol("dssi_descriptor"); if (descFn == nullptr) { diff --git a/source/backend/plugin/LadspaPlugin.cpp b/source/backend/plugin/LadspaPlugin.cpp index 4efbcafb1..9ed14f413 100644 --- a/source/backend/plugin/LadspaPlugin.cpp +++ b/source/backend/plugin/LadspaPlugin.cpp @@ -1551,7 +1551,7 @@ public: // --------------------------------------------------------------- // get DLL main entry - const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)pData->libSymbol("ladspa_descriptor"); + const LADSPA_Descriptor_Function descFn = pData->libSymbol("ladspa_descriptor"); if (descFn == nullptr) { diff --git a/source/backend/plugin/Lv2Plugin.cpp b/source/backend/plugin/Lv2Plugin.cpp index b9e2a7545..2f878c5fe 100644 --- a/source/backend/plugin/Lv2Plugin.cpp +++ b/source/backend/plugin/Lv2Plugin.cpp @@ -4581,7 +4581,7 @@ public: // --------------------------------------------------------------- // try to get DLL main entry via new mode - if (const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)pData->libSymbol("lv2_lib_descriptor")) + if (const LV2_Lib_Descriptor_Function libDescFn = pData->libSymbol("lv2_lib_descriptor")) { // ----------------------------------------------------------- // all ok, get lib descriptor @@ -4609,7 +4609,7 @@ public: // ----------------------------------------------------------- // get DLL main entry (old mode) - const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor"); + const LV2_Descriptor_Function descFn = pData->libSymbol("lv2_descriptor"); if (descFn == nullptr) { @@ -5110,7 +5110,7 @@ public: // --------------------------------------------------------------- // get UI DLL main entry - LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)pData->uiLibSymbol("lv2ui_descriptor"); + LV2UI_DescriptorFunction uiDescFn = pData->uiLibSymbol("lv2ui_descriptor"); if (uiDescFn == nullptr) { diff --git a/source/backend/plugin/VstPlugin.cpp b/source/backend/plugin/VstPlugin.cpp index bc3e26a63..a5c794512 100644 --- a/source/backend/plugin/VstPlugin.cpp +++ b/source/backend/plugin/VstPlugin.cpp @@ -2212,11 +2212,11 @@ public: // --------------------------------------------------------------- // get DLL main entry - VST_Function vstFn = (VST_Function)pData->libSymbol("VSTPluginMain"); + VST_Function vstFn = pData->libSymbol("VSTPluginMain"); if (vstFn == nullptr) { - vstFn = (VST_Function)pData->libSymbol("main"); + vstFn = pData->libSymbol("main"); if (vstFn == nullptr) { diff --git a/source/bridges-ui/CarlaBridgeClient.cpp b/source/bridges-ui/CarlaBridgeClient.cpp index 8ba3317ad..c7e78662e 100644 --- a/source/bridges-ui/CarlaBridgeClient.cpp +++ b/source/bridges-ui/CarlaBridgeClient.cpp @@ -245,14 +245,6 @@ bool CarlaBridgeClient::uiLibClose() return closed; } -void* CarlaBridgeClient::uiLibSymbol(const char* const symbol) -{ - CARLA_SAFE_ASSERT_RETURN(fUI.lib != nullptr, nullptr); - carla_debug("CarlaBridgeClient::uiLibSymbol(\"%s\")", symbol); - - return lib_symbol(fUI.lib, symbol); -} - const char* CarlaBridgeClient::uiLibError() { carla_debug("CarlaBridgeClient::uiLibError()"); diff --git a/source/bridges-ui/CarlaBridgeClient.hpp b/source/bridges-ui/CarlaBridgeClient.hpp index 282683222..b9efa2328 100644 --- a/source/bridges-ui/CarlaBridgeClient.hpp +++ b/source/bridges-ui/CarlaBridgeClient.hpp @@ -20,6 +20,7 @@ #include "CarlaBridgeOsc.hpp" #include "CarlaBridgeToolkit.hpp" +#include "CarlaLibUtils.hpp" CARLA_BRIDGE_START_NAMESPACE @@ -91,11 +92,22 @@ protected: void* getContainerId(); void* getContainerId2(); - bool uiLibOpen(const char* const filename); - bool uiLibClose(); - void* uiLibSymbol(const char* const symbol); + + // --------------------------------------------------------------------- + + bool uiLibOpen(const char* const filename); + bool uiLibClose(); const char* uiLibError(); + template + Func uiLibSymbol(const char* const symbol) + { + CARLA_SAFE_ASSERT_RETURN(fUI.lib != nullptr, nullptr); + carla_debug("CarlaBridgeClient::uiLibSymbol(\"%s\")", symbol); + + return lib_symbol(fUI.lib, symbol); + } + // --------------------------------------------------------------------- private: diff --git a/source/bridges-ui/CarlaBridgeUI-LV2.cpp b/source/bridges-ui/CarlaBridgeUI-LV2.cpp index e819597bb..ef9f6bf86 100644 --- a/source/bridges-ui/CarlaBridgeUI-LV2.cpp +++ b/source/bridges-ui/CarlaBridgeUI-LV2.cpp @@ -415,7 +415,7 @@ public: // ----------------------------------------------------------------- // get DLL main entry - const LV2UI_DescriptorFunction ui_descFn = (LV2UI_DescriptorFunction)uiLibSymbol("lv2ui_descriptor"); + const LV2UI_DescriptorFunction ui_descFn = uiLibSymbol("lv2ui_descriptor"); if (ui_descFn == nullptr) return false; diff --git a/source/bridges-ui/CarlaBridgeUI-VST.cpp b/source/bridges-ui/CarlaBridgeUI-VST.cpp index d16639a13..c4f881e23 100644 --- a/source/bridges-ui/CarlaBridgeUI-VST.cpp +++ b/source/bridges-ui/CarlaBridgeUI-VST.cpp @@ -72,10 +72,10 @@ public: // ----------------------------------------------------------------- // get DLL main entry - VST_Function vstFn = (VST_Function)uiLibSymbol("VSTPluginMain"); + VST_Function vstFn = uiLibSymbol("VSTPluginMain"); if (vstFn == nullptr) - vstFn = (VST_Function)uiLibSymbol("main"); + vstFn = uiLibSymbol("main"); if (vstFn == nullptr) return false; diff --git a/source/discovery/carla-discovery.cpp b/source/discovery/carla-discovery.cpp index e5a88bbe2..1d8f52642 100644 --- a/source/discovery/carla-discovery.cpp +++ b/source/discovery/carla-discovery.cpp @@ -403,7 +403,7 @@ private: static void do_ladspa_check(void*& libHandle, const char* const filename, const bool doInit) { - LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)lib_symbol(libHandle, "ladspa_descriptor"); + LADSPA_Descriptor_Function descFn = lib_symbol(libHandle, "ladspa_descriptor"); if (descFn == nullptr) { @@ -443,7 +443,7 @@ static void do_ladspa_check(void*& libHandle, const char* const filename, const return; } - descFn = (LADSPA_Descriptor_Function)lib_symbol(libHandle, "ladspa_descriptor"); + descFn = lib_symbol(libHandle, "ladspa_descriptor"); if (descFn == nullptr) { @@ -636,7 +636,7 @@ static void do_ladspa_check(void*& libHandle, const char* const filename, const static void do_dssi_check(void*& libHandle, const char* const filename, const bool doInit) { - DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)lib_symbol(libHandle, "dssi_descriptor"); + DSSI_Descriptor_Function descFn = lib_symbol(libHandle, "dssi_descriptor"); if (descFn == nullptr) { @@ -684,7 +684,7 @@ static void do_dssi_check(void*& libHandle, const char* const filename, const bo return; } - descFn = (DSSI_Descriptor_Function)lib_symbol(libHandle, "dssi_descriptor"); + descFn = lib_symbol(libHandle, "dssi_descriptor"); if (descFn == nullptr) { @@ -1144,11 +1144,11 @@ static void do_lv2_check(const char* const bundle, const bool doInit) #ifndef CARLA_OS_MAC static void do_vst_check(void*& libHandle, const bool doInit) { - VST_Function vstFn = (VST_Function)lib_symbol(libHandle, "VSTPluginMain"); + VST_Function vstFn = lib_symbol(libHandle, "VSTPluginMain"); if (vstFn == nullptr) { - vstFn = (VST_Function)lib_symbol(libHandle, "main"); + vstFn = lib_symbol(libHandle, "main"); if (vstFn == nullptr) { diff --git a/source/includes/CarlaDefines.h b/source/includes/CarlaDefines.h index 6e0d5fb38..f297921dc 100644 --- a/source/includes/CarlaDefines.h +++ b/source/includes/CarlaDefines.h @@ -228,7 +228,7 @@ private: \ # ifdef BUILDING_CARLA # define CARLA_API __declspec (dllexport) # else -# define CARLA_API __declspec (imexport) +# define CARLA_API __declspec (dllimport) # endif # else # define CARLA_API __attribute__ ((visibility("default"))) diff --git a/source/modules/jackbridge/JackBridge1.cpp b/source/modules/jackbridge/JackBridge1.cpp index 416d44359..197a9a174 100644 --- a/source/modules/jackbridge/JackBridge1.cpp +++ b/source/modules/jackbridge/JackBridge1.cpp @@ -368,7 +368,7 @@ struct JackBridge { } #define JOIN(a, b) a ## b - #define LIB_SYMBOL(NAME) JOIN(NAME, _ptr) = (jacksym_##NAME)lib_symbol(lib, "jack_" #NAME); + #define LIB_SYMBOL(NAME) JOIN(NAME, _ptr) = lib_symbol(lib, "jack_" #NAME); LIB_SYMBOL(get_version) LIB_SYMBOL(get_version_string) diff --git a/source/modules/jackbridge/JackBridgeExport.cpp b/source/modules/jackbridge/JackBridgeExport.cpp index ce9bf7803..58d3e210b 100644 --- a/source/modules/jackbridge/JackBridgeExport.cpp +++ b/source/modules/jackbridge/JackBridgeExport.cpp @@ -34,7 +34,7 @@ public: #endif CARLA_SAFE_ASSERT_RETURN(lib != nullptr,); - func = (jackbridge_exported_function_type)lib_symbol(lib, "jackbridge_get_exported_functions"); + func = lib_symbol(lib, "jackbridge_get_exported_functions"); CARLA_SAFE_ASSERT_RETURN(func != nullptr,); } diff --git a/source/tests/CarlaUtils3.cpp b/source/tests/CarlaUtils3.cpp index 5f788fd23..74ad2dd1f 100644 --- a/source/tests/CarlaUtils3.cpp +++ b/source/tests/CarlaUtils3.cpp @@ -71,6 +71,8 @@ static void test_CarlaJuceUtils() // ----------------------------------------------------------------------- +typedef void (*nullFunc)(); + static void test_CarlaLibUtils() noexcept { void* const libNot = lib_open("/libzzzzz..."); @@ -80,7 +82,7 @@ static void test_CarlaLibUtils() noexcept void* const lib = lib_open("/usr/lib/liblo.so"); CARLA_SAFE_ASSERT_RETURN(lib != nullptr,); - void* const libS = lib_symbol(lib, "lo_server_new"); + const nullFunc libS = lib_symbol(lib, "lo_server_new"); CARLA_SAFE_ASSERT(libS != nullptr); const bool closed = lib_close(lib); diff --git a/source/tests/Makefile b/source/tests/Makefile index d01026b1f..fa1cbb82d 100644 --- a/source/tests/Makefile +++ b/source/tests/Makefile @@ -52,7 +52,9 @@ TARGETS += ansi-pedantic-test_cxx11 TARGETS += CarlaRingBuffer TARGETS += CarlaString TARGETS += CarlaUtils1 +ifneq ($(WIN32),true) TARGETS += CarlaUtils2 +endif TARGETS += CarlaUtils3 TARGETS += CarlaUtils4 TARGETS += Exceptions @@ -82,15 +84,21 @@ ansi-pedantic-test_cxx11: ansi-pedantic-test.cpp ../backend/Carla*.h ../includes CarlaRingBuffer: CarlaRingBuffer.cpp ../utils/CarlaRingBuffer.hpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ +ifneq ($(WIN32),true) ./$@ && valgrind --leak-check=full ./$@ +endif CarlaString: CarlaString.cpp ../utils/CarlaString.hpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ +ifneq ($(WIN32),true) ./$@ && valgrind --leak-check=full ./$@ +endif CarlaUtils1: CarlaUtils1.cpp ../utils/*.hpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ +ifneq ($(WIN32),true) ./$@ && valgrind --leak-check=full ./$@ +endif CarlaUtils2: CarlaUtils2.cpp ../utils/*.hpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ \ @@ -99,24 +107,34 @@ CarlaUtils2: CarlaUtils2.cpp ../utils/*.hpp CarlaUtils3: CarlaUtils3.cpp ../utils/*.hpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ -ldl -lrt +ifneq ($(WIN32),true) ./$@ && valgrind --leak-check=full ./$@ +endif CarlaUtils4: CarlaUtils4.cpp ../utils/CarlaStateUtils.cpp ../utils/*.hpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ \ ../modules/juce_core.a -ldl -lpthread -lrt +ifneq ($(WIN32),true) ./$@ && valgrind --leak-check=full ./$@ +endif Exceptions: Exceptions.cpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ +ifneq ($(WIN32),true) ./$@ && valgrind --leak-check=full ./$@ +endif Print: Print.cpp ../utils/CarlaUtils.hpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ +ifneq ($(WIN32),true) ./$@ && valgrind --leak-check=full ./$@ +endif RDF: RDF.cpp ../modules/ladspa_rdf.hpp ../modules/lv2_rdf.hpp $(CXX) $< $(PEDANTIC_CXX_FLAGS) -o $@ +ifneq ($(WIN32),true) ./$@ && valgrind --leak-check=full ./$@ +endif # -------------------------------------------------------------- diff --git a/source/utils/CarlaLibUtils.hpp b/source/utils/CarlaLibUtils.hpp index 71675b47c..c77c9f831 100644 --- a/source/utils/CarlaLibUtils.hpp +++ b/source/utils/CarlaLibUtils.hpp @@ -67,17 +67,20 @@ bool lib_close(void* const lib) noexcept * Get a library symbol (must not be null). * Returns null if the symbol is not found. */ +template static inline -void* lib_symbol(void* const lib, const char* const symbol) noexcept +Func lib_symbol(void* const lib, const char* const symbol) noexcept { CARLA_SAFE_ASSERT_RETURN(lib != nullptr, nullptr); CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', nullptr); + try { #ifdef CARLA_OS_WIN - return (void*)::GetProcAddress((HMODULE)lib, symbol); + return (Func)::GetProcAddress((HMODULE)lib, symbol); #else - return ::dlsym(lib, symbol); + return (Func)(uintptr_t)::dlsym(lib, symbol); #endif + } CARLA_SAFE_EXCEPTION_RETURN("lib_symbol", nullptr); } /* diff --git a/source/utils/CarlaShmUtils.hpp b/source/utils/CarlaShmUtils.hpp index 9d77650d2..e26523b8d 100644 --- a/source/utils/CarlaShmUtils.hpp +++ b/source/utils/CarlaShmUtils.hpp @@ -117,7 +117,7 @@ shm_t carla_shm_attach(const char* const filename) noexcept try { #ifdef CARLA_OS_WIN - ret.shm = ::CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); + ret.shm = ::CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); ret.map = nullptr; #else ret.fd = ::shm_open(filename, O_RDWR, 0); @@ -174,7 +174,7 @@ void* carla_shm_map(shm_t& shm, const std::size_t size) noexcept try { #ifdef CARLA_OS_WIN - const HANDLE map = ::CreateFileMapping(shm.shm, NULL, PAGE_READWRITE, size, size, NULL); + const HANDLE map = ::CreateFileMapping(shm.shm, nullptr, PAGE_READWRITE, size, size, nullptr); CARLA_SAFE_ASSERT_RETURN(map != nullptr, nullptr); HANDLE ptr = ::MapViewOfFile(map, FILE_MAP_COPY, 0, 0, size);