Skip to content

Commit 6ca4b53

Browse files
committed
Library: refactor FF_LIBRARY_LOAD to use __cleanup__
1 parent cabd36d commit 6ca4b53

File tree

19 files changed

+27
-96
lines changed

19 files changed

+27
-96
lines changed

src/common/dbus.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static bool loadLibSymbols(const FFinstance* instance, FFDBusLibrary* lib)
2424
FF_LIBRARY_LOAD_SYMBOL_PTR(dbus, lib, dbus_pending_call_block, false)
2525
FF_LIBRARY_LOAD_SYMBOL_PTR(dbus, lib, dbus_pending_call_steal_reply, false)
2626
FF_LIBRARY_LOAD_SYMBOL_PTR(dbus, lib, dbus_pending_call_unref, false)
27-
27+
dbus = NULL; // don't auto dlclose
2828
return true;
2929
}
3030

@@ -178,7 +178,7 @@ DBusMessage* ffDBusGetProperty(FFDBusData* dbus, const char* busName, const char
178178
return NULL;
179179
}
180180

181-
DBusMessage* reply = dbus->lib->ffdbus_connection_send_with_reply_and_block(dbus->connection, message, FF_DBUS_TIMEOUT_MILLISECONDS, NULL);
181+
DBusMessage* reply = dbus->lib->ffdbus_connection_send_with_reply_and_block(dbus->connection, message, FF_DBUS_TIMEOUT_MILLISECONDS, NULL);
182182

183183
dbus->lib->ffdbus_message_unref(message);
184184

src/common/library.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "fastfetch.h"
77
#include "util/FFcheckmacros.h"
88

9-
#if defined(_WIN32) //We don't force MSYS using LoadLibrary because dlopen also searches $LD_LIBRARY_PATH
9+
#if defined(_WIN32)
1010
#include <libloaderapi.h>
1111
#define FF_DLOPEN_FLAGS 0
1212
FF_C_NODISCARD static inline void* dlopen(const char* path, int mode) { FF_UNUSED(mode); return LoadLibraryA(path); }
@@ -24,29 +24,30 @@
2424
#define FF_LIBRARY_EXTENSION ".so"
2525
#endif
2626

27+
static inline void ffLibraryUnload(void** handle)
28+
{
29+
assert(handle);
30+
if (*handle)
31+
dlclose(*handle);
32+
}
33+
2734
#define FF_LIBRARY_SYMBOL(symbolName) \
2835
__typeof__(&symbolName) ff ## symbolName;
2936

3037
#define FF_LIBRARY_LOAD(libraryObjectName, userLibraryName, returnValue, ...) \
31-
void* libraryObjectName = ffLibraryLoad(userLibraryName, __VA_ARGS__, NULL);\
38+
void* __attribute__((__cleanup__(ffLibraryUnload))) libraryObjectName = ffLibraryLoad(userLibraryName, __VA_ARGS__, NULL);\
3239
if(libraryObjectName == NULL) \
3340
return returnValue;
3441

3542
#define FF_LIBRARY_LOAD_SYMBOL_ADDRESS(library, symbolMapping, symbolName, returnValue) \
3643
symbolMapping = dlsym(library, #symbolName); \
3744
if(symbolMapping == NULL) \
38-
{ \
39-
dlclose(library); \
40-
return returnValue; \
41-
}
45+
return returnValue;
4246

4347
#define FF_LIBRARY_LOAD_SYMBOL_ADDRESS2(library, symbolMapping, symbolName, alternateName, returnValue) \
4448
symbolMapping = dlsym(library, #symbolName); \
4549
if(symbolMapping == NULL && !(symbolMapping = dlsym(library, #alternateName))) \
46-
{ \
47-
dlclose(library); \
48-
return returnValue; \
49-
}
50+
return returnValue;
5051

5152
#define FF_LIBRARY_LOAD_SYMBOL(library, symbolName, returnValue) \
5253
__typeof__(&symbolName) FF_LIBRARY_LOAD_SYMBOL_ADDRESS(library, ff ## symbolName, symbolName, returnValue);

src/detection/displayserver/linux/wayland.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,14 @@ bool detectWayland(const FFinstance* instance, FFDisplayServerResult* result)
160160

161161
data.display = ffwl_display_connect(NULL);
162162
if(data.display == NULL)
163-
{
164-
dlclose(wayland);
165163
return false;
166-
}
167164

168165
waylandDetectWM(ffwl_display_get_fd(data.display), result);
169166

170167
struct wl_proxy* registry = ffwl_proxy_marshal_constructor((struct wl_proxy*) data.display, WL_DISPLAY_GET_REGISTRY, ffwl_registry_interface, NULL);
171168
if(registry == NULL)
172169
{
173170
ffwl_display_disconnect(data.display);
174-
dlclose(wayland);
175171
return false;
176172
}
177173

@@ -188,7 +184,6 @@ bool detectWayland(const FFinstance* instance, FFDisplayServerResult* result)
188184

189185
data.ffwl_proxy_destroy(registry);
190186
ffwl_display_disconnect(data.display);
191-
dlclose(wayland);
192187

193188
//We successfully connected to wayland and detected the display.
194189
//So we can set set the session type to wayland.

src/detection/displayserver/linux/xcb.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ void ffdsConnectXcb(const FFinstance* instance, FFDisplayServerResult* result)
103103

104104
xcb_connection_t* connection = ffxcb_connect(NULL, NULL);
105105
if(connection == NULL)
106-
{
107-
dlclose(xcb);
108106
return;
109-
}
110107

111108
xcb_screen_iterator_t iterator = ffxcb_setup_roots_iterator(ffxcb_get_setup(connection));
112109

@@ -127,7 +124,6 @@ void ffdsConnectXcb(const FFinstance* instance, FFDisplayServerResult* result)
127124
}
128125

129126
ffxcb_disconnect(connection);
130-
dlclose(xcb);
131127

132128
//If wayland hasn't set this, connection failed for it. So we are running only a X Server, not XWayland.
133129
if(result->wmProtocolName.length == 0)
@@ -368,10 +364,7 @@ void ffdsConnectXcbRandr(const FFinstance* instance, FFDisplayServerResult* resu
368364

369365
data.connection = ffxcb_connect(NULL, NULL);
370366
if(data.connection == NULL)
371-
{
372-
dlclose(xcbRandr);
373367
return;
374-
}
375368

376369
data.result = result;
377370

@@ -387,7 +380,6 @@ void ffdsConnectXcbRandr(const FFinstance* instance, FFDisplayServerResult* resu
387380
}
388381

389382
ffxcb_disconnect(data.connection);
390-
dlclose(xcbRandr);
391383

392384
//If wayland hasn't set this, connection failed for it. So we are running only a X Server, not XWayland.
393385
if(result->wmProtocolName.length == 0)

src/detection/displayserver/linux/xlib.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ void ffdsConnectXlib(const FFinstance* instance, FFDisplayServerResult* result)
6969

7070
Display* display = ffXOpenDisplay(x11);
7171
if(display == NULL)
72-
{
73-
dlclose(x11);
7472
return;
75-
}
7673

7774
if(propertyDataInitialized && ScreenCount(display) > 0)
7875
x11DetectWMFromEWMH(&propertyData, display, result);
@@ -91,7 +88,6 @@ void ffdsConnectXlib(const FFinstance* instance, FFDisplayServerResult* result)
9188
}
9289

9390
ffXCloseDisplay(display);
94-
dlclose(x11);
9591

9692
//If wayland hasn't set this, connection failed for it. So we are running only a X Server, not XWayland.
9793
if(result->wmProtocolName.length == 0)
@@ -298,10 +294,7 @@ void ffdsConnectXrandr(const FFinstance* instance, FFDisplayServerResult* result
298294

299295
data.display = ffXOpenDisplay(NULL);
300296
if(data.display == NULL)
301-
{
302-
dlclose(xrandr);
303297
return;
304-
}
305298

306299
if(propertyDataInitialized && ScreenCount(data.display) > 0)
307300
x11DetectWMFromEWMH(&propertyData, data.display, result);
@@ -312,7 +305,6 @@ void ffdsConnectXrandr(const FFinstance* instance, FFDisplayServerResult* result
312305
xrandrHandleScreen(&data, ScreenOfDisplay(data.display, i));
313306

314307
ffXCloseDisplay(data.display);
315-
dlclose(xrandr);
316308

317309
//If wayland hasn't set this, connection failed for it. So we are running only a X Server, not XWayland.
318310
if(result->wmProtocolName.length == 0)

src/detection/gpu/gpu_linux.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ static const char* pciDetectGPUs(const FFinstance* instance, FFlist* gpus)
251251
}
252252

253253
ffpci_cleanup(pci.access);
254-
dlclose(libpci);
255254
return NULL;
256255
}
257256

src/detection/opengl/opengl_linux.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ static const char* eglPrint(FFinstance* instance, FFOpenGLResult* result)
133133
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(egl, eglData, eglDestroySurface);
134134
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(egl, eglData, eglTerminate);
135135

136-
const char* error = eglHandleData(result, &eglData);
137-
dlclose(egl);
138-
return error;
136+
return eglHandleData(result, &eglData);
139137
}
140138

141139
#endif //FF_HAVE_EGL
@@ -252,9 +250,7 @@ static const char* glxPrint(FFinstance* instance, FFOpenGLResult* result)
252250
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(glx, data, XCloseDisplay);
253251
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(glx, data, XFree);
254252

255-
const char* error = glxHandleData(result, &data);
256-
dlclose(glx);
257-
return error;
253+
return glxHandleData(result, &data);
258254
}
259255

260256
#endif //FF_HAVE_GLX
@@ -310,9 +306,7 @@ static const char* osMesaPrint(FFinstance* instance, FFOpenGLResult* result)
310306
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(osmesa, data, OSMesaMakeCurrent);
311307
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(osmesa, data, OSMesaDestroyContext);
312308

313-
const char* error = osMesaHandleData(result, &data);
314-
dlclose(osmesa);
315-
return error;
309+
return osMesaHandleData(result, &data);
316310
}
317311

318312
#endif //FF_HAVE_OSMESA

src/detection/packages/packages_linux.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,31 +229,23 @@ static uint32_t getRpmFromLibrpm(const FFinstance* instance)
229229
ffrpmlogSetMask(RPMLOG_MASK(RPMLOG_EMERG));
230230

231231
if(ffrpmReadConfigFiles(NULL, NULL) != 0)
232-
{
233-
dlclose(rpm);
234232
return 0;
235-
}
236233

237234
rpmts ts = ffrpmtsCreate();
238235
if(ts == NULL)
239-
{
240-
dlclose(rpm);
241236
return 0;
242-
}
243237

244238
rpmdbMatchIterator mi = ffrpmtsInitIterator(ts, RPMDBI_LABEL, NULL, 0);
245239
if(mi == NULL)
246240
{
247241
ffrpmtsFree(ts);
248-
dlclose(rpm);
249242
return 0;
250243
}
251244

252245
int count = ffrpmdbGetIteratorCount(mi);
253246

254247
ffrpmdbFreeIterator(mi);
255248
ffrpmtsFree(ts);
256-
dlclose(rpm);
257249

258250
return count > 0 ? (uint32_t) count : 0;
259251
}

src/detection/sound/sound_linux.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ static const char* detectSound(const FFinstance* instance, FFlist* devices)
114114

115115
ffpa_context_unref(context);
116116
ffpa_mainloop_free(mainloop);
117-
dlclose(pulse);
118117
return NULL;
119118
}
120119

src/detection/terminalfont/terminalfont.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ static const char* detectFromWTImpl(const FFinstance* instance, FFstrbuf* conten
177177

178178
exit:
179179
cjsonData.ffcJSON_Delete(root);
180-
dlclose(libcjson);
181180
return error;
182181
}
183182

0 commit comments

Comments
 (0)