mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-01 10:32:35 +01:00
Library: refactor FF_LIBRARY_LOAD to use __cleanup__
This commit is contained in:
+2
-2
@@ -24,7 +24,7 @@ static bool loadLibSymbols(const FFinstance* instance, FFDBusLibrary* lib)
|
||||
FF_LIBRARY_LOAD_SYMBOL_PTR(dbus, lib, dbus_pending_call_block, false)
|
||||
FF_LIBRARY_LOAD_SYMBOL_PTR(dbus, lib, dbus_pending_call_steal_reply, false)
|
||||
FF_LIBRARY_LOAD_SYMBOL_PTR(dbus, lib, dbus_pending_call_unref, false)
|
||||
|
||||
dbus = NULL; // don't auto dlclose
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ DBusMessage* ffDBusGetProperty(FFDBusData* dbus, const char* busName, const char
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DBusMessage* reply = dbus->lib->ffdbus_connection_send_with_reply_and_block(dbus->connection, message, FF_DBUS_TIMEOUT_MILLISECONDS, NULL);
|
||||
DBusMessage* reply = dbus->lib->ffdbus_connection_send_with_reply_and_block(dbus->connection, message, FF_DBUS_TIMEOUT_MILLISECONDS, NULL);
|
||||
|
||||
dbus->lib->ffdbus_message_unref(message);
|
||||
|
||||
|
||||
+11
-10
@@ -6,7 +6,7 @@
|
||||
#include "fastfetch.h"
|
||||
#include "util/FFcheckmacros.h"
|
||||
|
||||
#if defined(_WIN32) //We don't force MSYS using LoadLibrary because dlopen also searches $LD_LIBRARY_PATH
|
||||
#if defined(_WIN32)
|
||||
#include <libloaderapi.h>
|
||||
#define FF_DLOPEN_FLAGS 0
|
||||
FF_C_NODISCARD static inline void* dlopen(const char* path, int mode) { FF_UNUSED(mode); return LoadLibraryA(path); }
|
||||
@@ -24,29 +24,30 @@
|
||||
#define FF_LIBRARY_EXTENSION ".so"
|
||||
#endif
|
||||
|
||||
static inline void ffLibraryUnload(void** handle)
|
||||
{
|
||||
assert(handle);
|
||||
if (*handle)
|
||||
dlclose(*handle);
|
||||
}
|
||||
|
||||
#define FF_LIBRARY_SYMBOL(symbolName) \
|
||||
__typeof__(&symbolName) ff ## symbolName;
|
||||
|
||||
#define FF_LIBRARY_LOAD(libraryObjectName, userLibraryName, returnValue, ...) \
|
||||
void* libraryObjectName = ffLibraryLoad(userLibraryName, __VA_ARGS__, NULL);\
|
||||
void* __attribute__((__cleanup__(ffLibraryUnload))) libraryObjectName = ffLibraryLoad(userLibraryName, __VA_ARGS__, NULL);\
|
||||
if(libraryObjectName == NULL) \
|
||||
return returnValue;
|
||||
|
||||
#define FF_LIBRARY_LOAD_SYMBOL_ADDRESS(library, symbolMapping, symbolName, returnValue) \
|
||||
symbolMapping = dlsym(library, #symbolName); \
|
||||
if(symbolMapping == NULL) \
|
||||
{ \
|
||||
dlclose(library); \
|
||||
return returnValue; \
|
||||
}
|
||||
return returnValue;
|
||||
|
||||
#define FF_LIBRARY_LOAD_SYMBOL_ADDRESS2(library, symbolMapping, symbolName, alternateName, returnValue) \
|
||||
symbolMapping = dlsym(library, #symbolName); \
|
||||
if(symbolMapping == NULL && !(symbolMapping = dlsym(library, #alternateName))) \
|
||||
{ \
|
||||
dlclose(library); \
|
||||
return returnValue; \
|
||||
}
|
||||
return returnValue;
|
||||
|
||||
#define FF_LIBRARY_LOAD_SYMBOL(library, symbolName, returnValue) \
|
||||
__typeof__(&symbolName) FF_LIBRARY_LOAD_SYMBOL_ADDRESS(library, ff ## symbolName, symbolName, returnValue);
|
||||
|
||||
@@ -160,10 +160,7 @@ bool detectWayland(const FFinstance* instance, FFDisplayServerResult* result)
|
||||
|
||||
data.display = ffwl_display_connect(NULL);
|
||||
if(data.display == NULL)
|
||||
{
|
||||
dlclose(wayland);
|
||||
return false;
|
||||
}
|
||||
|
||||
waylandDetectWM(ffwl_display_get_fd(data.display), result);
|
||||
|
||||
@@ -171,7 +168,6 @@ bool detectWayland(const FFinstance* instance, FFDisplayServerResult* result)
|
||||
if(registry == NULL)
|
||||
{
|
||||
ffwl_display_disconnect(data.display);
|
||||
dlclose(wayland);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -188,7 +184,6 @@ bool detectWayland(const FFinstance* instance, FFDisplayServerResult* result)
|
||||
|
||||
data.ffwl_proxy_destroy(registry);
|
||||
ffwl_display_disconnect(data.display);
|
||||
dlclose(wayland);
|
||||
|
||||
//We successfully connected to wayland and detected the display.
|
||||
//So we can set set the session type to wayland.
|
||||
|
||||
@@ -103,10 +103,7 @@ void ffdsConnectXcb(const FFinstance* instance, FFDisplayServerResult* result)
|
||||
|
||||
xcb_connection_t* connection = ffxcb_connect(NULL, NULL);
|
||||
if(connection == NULL)
|
||||
{
|
||||
dlclose(xcb);
|
||||
return;
|
||||
}
|
||||
|
||||
xcb_screen_iterator_t iterator = ffxcb_setup_roots_iterator(ffxcb_get_setup(connection));
|
||||
|
||||
@@ -127,7 +124,6 @@ void ffdsConnectXcb(const FFinstance* instance, FFDisplayServerResult* result)
|
||||
}
|
||||
|
||||
ffxcb_disconnect(connection);
|
||||
dlclose(xcb);
|
||||
|
||||
//If wayland hasn't set this, connection failed for it. So we are running only a X Server, not XWayland.
|
||||
if(result->wmProtocolName.length == 0)
|
||||
@@ -368,10 +364,7 @@ void ffdsConnectXcbRandr(const FFinstance* instance, FFDisplayServerResult* resu
|
||||
|
||||
data.connection = ffxcb_connect(NULL, NULL);
|
||||
if(data.connection == NULL)
|
||||
{
|
||||
dlclose(xcbRandr);
|
||||
return;
|
||||
}
|
||||
|
||||
data.result = result;
|
||||
|
||||
@@ -387,7 +380,6 @@ void ffdsConnectXcbRandr(const FFinstance* instance, FFDisplayServerResult* resu
|
||||
}
|
||||
|
||||
ffxcb_disconnect(data.connection);
|
||||
dlclose(xcbRandr);
|
||||
|
||||
//If wayland hasn't set this, connection failed for it. So we are running only a X Server, not XWayland.
|
||||
if(result->wmProtocolName.length == 0)
|
||||
|
||||
@@ -69,10 +69,7 @@ void ffdsConnectXlib(const FFinstance* instance, FFDisplayServerResult* result)
|
||||
|
||||
Display* display = ffXOpenDisplay(x11);
|
||||
if(display == NULL)
|
||||
{
|
||||
dlclose(x11);
|
||||
return;
|
||||
}
|
||||
|
||||
if(propertyDataInitialized && ScreenCount(display) > 0)
|
||||
x11DetectWMFromEWMH(&propertyData, display, result);
|
||||
@@ -91,7 +88,6 @@ void ffdsConnectXlib(const FFinstance* instance, FFDisplayServerResult* result)
|
||||
}
|
||||
|
||||
ffXCloseDisplay(display);
|
||||
dlclose(x11);
|
||||
|
||||
//If wayland hasn't set this, connection failed for it. So we are running only a X Server, not XWayland.
|
||||
if(result->wmProtocolName.length == 0)
|
||||
@@ -298,10 +294,7 @@ void ffdsConnectXrandr(const FFinstance* instance, FFDisplayServerResult* result
|
||||
|
||||
data.display = ffXOpenDisplay(NULL);
|
||||
if(data.display == NULL)
|
||||
{
|
||||
dlclose(xrandr);
|
||||
return;
|
||||
}
|
||||
|
||||
if(propertyDataInitialized && ScreenCount(data.display) > 0)
|
||||
x11DetectWMFromEWMH(&propertyData, data.display, result);
|
||||
@@ -312,7 +305,6 @@ void ffdsConnectXrandr(const FFinstance* instance, FFDisplayServerResult* result
|
||||
xrandrHandleScreen(&data, ScreenOfDisplay(data.display, i));
|
||||
|
||||
ffXCloseDisplay(data.display);
|
||||
dlclose(xrandr);
|
||||
|
||||
//If wayland hasn't set this, connection failed for it. So we are running only a X Server, not XWayland.
|
||||
if(result->wmProtocolName.length == 0)
|
||||
|
||||
@@ -251,7 +251,6 @@ static const char* pciDetectGPUs(const FFinstance* instance, FFlist* gpus)
|
||||
}
|
||||
|
||||
ffpci_cleanup(pci.access);
|
||||
dlclose(libpci);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -133,9 +133,7 @@ static const char* eglPrint(FFinstance* instance, FFOpenGLResult* result)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(egl, eglData, eglDestroySurface);
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(egl, eglData, eglTerminate);
|
||||
|
||||
const char* error = eglHandleData(result, &eglData);
|
||||
dlclose(egl);
|
||||
return error;
|
||||
return eglHandleData(result, &eglData);
|
||||
}
|
||||
|
||||
#endif //FF_HAVE_EGL
|
||||
@@ -252,9 +250,7 @@ static const char* glxPrint(FFinstance* instance, FFOpenGLResult* result)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(glx, data, XCloseDisplay);
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(glx, data, XFree);
|
||||
|
||||
const char* error = glxHandleData(result, &data);
|
||||
dlclose(glx);
|
||||
return error;
|
||||
return glxHandleData(result, &data);
|
||||
}
|
||||
|
||||
#endif //FF_HAVE_GLX
|
||||
@@ -310,9 +306,7 @@ static const char* osMesaPrint(FFinstance* instance, FFOpenGLResult* result)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(osmesa, data, OSMesaMakeCurrent);
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(osmesa, data, OSMesaDestroyContext);
|
||||
|
||||
const char* error = osMesaHandleData(result, &data);
|
||||
dlclose(osmesa);
|
||||
return error;
|
||||
return osMesaHandleData(result, &data);
|
||||
}
|
||||
|
||||
#endif //FF_HAVE_OSMESA
|
||||
|
||||
@@ -229,23 +229,16 @@ static uint32_t getRpmFromLibrpm(const FFinstance* instance)
|
||||
ffrpmlogSetMask(RPMLOG_MASK(RPMLOG_EMERG));
|
||||
|
||||
if(ffrpmReadConfigFiles(NULL, NULL) != 0)
|
||||
{
|
||||
dlclose(rpm);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rpmts ts = ffrpmtsCreate();
|
||||
if(ts == NULL)
|
||||
{
|
||||
dlclose(rpm);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rpmdbMatchIterator mi = ffrpmtsInitIterator(ts, RPMDBI_LABEL, NULL, 0);
|
||||
if(mi == NULL)
|
||||
{
|
||||
ffrpmtsFree(ts);
|
||||
dlclose(rpm);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -253,7 +246,6 @@ static uint32_t getRpmFromLibrpm(const FFinstance* instance)
|
||||
|
||||
ffrpmdbFreeIterator(mi);
|
||||
ffrpmtsFree(ts);
|
||||
dlclose(rpm);
|
||||
|
||||
return count > 0 ? (uint32_t) count : 0;
|
||||
}
|
||||
|
||||
@@ -114,7 +114,6 @@ static const char* detectSound(const FFinstance* instance, FFlist* devices)
|
||||
|
||||
ffpa_context_unref(context);
|
||||
ffpa_mainloop_free(mainloop);
|
||||
dlclose(pulse);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -177,7 +177,6 @@ static const char* detectFromWTImpl(const FFinstance* instance, FFstrbuf* conten
|
||||
|
||||
exit:
|
||||
cjsonData.ffcJSON_Delete(root);
|
||||
dlclose(libcjson);
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,6 @@ const char* detectTermux(const FFinstance* instance, FFTerminalFontResult* termi
|
||||
exit:
|
||||
if(face) ffFT_Done_Face(face);
|
||||
if(library) ffFT_Done_FreeType(library);
|
||||
dlclose(freetype);
|
||||
|
||||
return error;
|
||||
|
||||
|
||||
@@ -97,11 +97,7 @@ static const char* detectVulkan(const FFinstance* instance, FFVulkanResult* resu
|
||||
.ppEnabledExtensionNames = NULL,
|
||||
.flags = 0
|
||||
}, NULL, &vkInstance) != VK_SUCCESS)
|
||||
{
|
||||
dlclose(vulkan);
|
||||
return "ffvkCreateInstance() failed";
|
||||
}
|
||||
|
||||
|
||||
//if instance creation succeeded, but vkEnumerateInstanceVersion didn't, this means we are running against a vulkan 1.0 implementation
|
||||
//explicitly set this version, if no device is found, so we still have at least this info
|
||||
@@ -113,7 +109,6 @@ static const char* detectVulkan(const FFinstance* instance, FFVulkanResult* resu
|
||||
if(ffvkEnumeratePhysicalDevices(vkInstance, &physicalDeviceCount, physicalDevices) != VK_SUCCESS)
|
||||
{
|
||||
ffvkDestroyInstance(vkInstance, NULL);
|
||||
dlclose(vulkan);
|
||||
return "ffvkEnumeratePhysicalDevices() failed";
|
||||
}
|
||||
|
||||
@@ -234,7 +229,6 @@ static const char* detectVulkan(const FFinstance* instance, FFVulkanResult* resu
|
||||
ffVersionToPretty(&maxDeviceConformanceVersion, &result->conformanceVersion);
|
||||
|
||||
ffvkDestroyInstance(vkInstance, NULL);
|
||||
dlclose(vulkan);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,10 +45,7 @@ static const char* detectWifiWithLibnm(const FFinstance* instance, FFlist* resul
|
||||
|
||||
NMClient* client = ffnm_client_new(NULL, NULL);
|
||||
if(!client)
|
||||
{
|
||||
dlclose(nm);
|
||||
return "Could not create NMClient";
|
||||
}
|
||||
|
||||
/* Get all devices managed by NetworkManager */
|
||||
const GPtrArray* devices = ffnm_client_get_devices(client);
|
||||
@@ -199,7 +196,6 @@ static const char* detectWifiWithLibnm(const FFinstance* instance, FFlist* resul
|
||||
}
|
||||
|
||||
ffg_object_unref(client);
|
||||
dlclose(nm);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -207,7 +207,6 @@ const char* ffDetectWifi(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* res
|
||||
exit:
|
||||
if(ifList) ffWlanFreeMemory(ifList);
|
||||
if(hClient) ffWlanCloseHandle(hClient, NULL);
|
||||
dlclose(wlanapi);
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,15 +17,10 @@ FFLogoImageResult ffLogoPrintImageIM6(FFinstance* instance, FFLogoRequestData* r
|
||||
FF_LIBRARY_LOAD(imageMagick, &instance->config.libImageMagick, FF_LOGO_IMAGE_RESULT_INIT_ERROR, "libMagickCore-6.Q16HDRI" FF_LIBRARY_EXTENSION, 8, "libMagickCore-6.Q16" FF_LIBRARY_EXTENSION, 8)
|
||||
FF_LIBRARY_LOAD_SYMBOL_ADDRESS(imageMagick, ffResizeImage, ResizeImage, FF_LOGO_IMAGE_RESULT_INIT_ERROR);
|
||||
|
||||
FFIMData imData;
|
||||
imData.resizeFunc = logoResize;
|
||||
imData.library = imageMagick;
|
||||
|
||||
FFLogoImageResult result = ffLogoPrintImageImpl(instance, requestData, &imData);
|
||||
|
||||
dlclose(imageMagick);
|
||||
|
||||
return result;
|
||||
return ffLogoPrintImageImpl(instance, requestData, &(FFIMData) {
|
||||
.resizeFunc = logoResize,
|
||||
.library = imageMagick,
|
||||
});
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -21,15 +21,10 @@ FFLogoImageResult ffLogoPrintImageIM7(FFinstance* instance, FFLogoRequestData* r
|
||||
)
|
||||
FF_LIBRARY_LOAD_SYMBOL_ADDRESS(imageMagick, ffResizeImage, ResizeImage, FF_LOGO_IMAGE_RESULT_INIT_ERROR);
|
||||
|
||||
FFIMData imData;
|
||||
imData.resizeFunc = logoResize;
|
||||
imData.library = imageMagick;
|
||||
|
||||
FFLogoImageResult result = ffLogoPrintImageImpl(instance, requestData, &imData);
|
||||
|
||||
dlclose(imageMagick);
|
||||
|
||||
return result;
|
||||
return ffLogoPrintImageImpl(instance, requestData, &(FFIMData) {
|
||||
.resizeFunc = logoResize,
|
||||
.library = imageMagick,
|
||||
});
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+1
-10
@@ -131,14 +131,9 @@ static bool compressBlob(const FFinstance* instance, void** blob, size_t* length
|
||||
uLong compressedLength = ffcompressBound(*length);
|
||||
void* compressed = malloc(compressedLength);
|
||||
if(compressed == NULL)
|
||||
{
|
||||
dlclose(zlib);
|
||||
return false;
|
||||
}
|
||||
|
||||
int compressResult = ffcompress2(compressed, &compressedLength, *blob, *length, Z_BEST_COMPRESSION);
|
||||
dlclose(zlib);
|
||||
if(compressResult != Z_OK)
|
||||
if(ffcompress2(compressed, &compressedLength, *blob, *length, Z_BEST_COMPRESSION) != Z_OK)
|
||||
{
|
||||
free(compressed);
|
||||
return false;
|
||||
@@ -336,10 +331,7 @@ static bool printImageChafa(FFinstance* instance, FFLogoRequestData* requestData
|
||||
size_t length;
|
||||
void* blob = imageData->ffImageToBlob(imageData->imageInfo, imageData->image, &length, imageData->exceptionInfo);
|
||||
if(!checkAllocationResult(blob, length))
|
||||
{
|
||||
dlclose(chafa);
|
||||
return false;
|
||||
}
|
||||
|
||||
ChafaSymbolMap* symbolMap = ffchafa_symbol_map_new();
|
||||
GError* error = NULL;
|
||||
@@ -408,7 +400,6 @@ static bool printImageChafa(FFinstance* instance, FFLogoRequestData* requestData
|
||||
ffchafa_canvas_unref(canvas);
|
||||
ffchafa_canvas_config_unref(canvasConfig);
|
||||
ffchafa_symbol_map_unref(symbolMap);
|
||||
dlclose(chafa);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -94,9 +94,7 @@ static const char* printOpenCL(FFinstance* instance)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceIDs);
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceInfo);
|
||||
|
||||
const char* error = openCLHandleData(instance, &data);
|
||||
dlclose(opencl);
|
||||
return error;
|
||||
return openCLHandleData(instance, &data);
|
||||
}
|
||||
|
||||
#elif defined(__APPLE__) // FF_HAVE_OPENCL
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
static inline void ffWrapFree(void* pPtr)
|
||||
{
|
||||
assert(pPtr);
|
||||
if(*(void**)pPtr)
|
||||
free(*(void**)pPtr);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user