diff --git a/src/common/dlopencl.c b/src/common/dlopencl.c index 0f3cfb4f1de6..effa7d727d65 100644 --- a/src/common/dlopencl.c +++ b/src/common/dlopencl.c @@ -1,6 +1,6 @@ /* This file is part of darktable, - Copyright (C) 2011-2023 darktable developers. + Copyright (C) 2011-2026 darktable developers. darktable is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,6 +22,10 @@ #include "common/dynload.h" #include "common/dlopencl.h" +#ifdef __APPLE__ +#include +#endif + #include #include #include @@ -36,6 +40,19 @@ static const char *ocllib[] = { "/System/Library/Frameworks/OpenCL.framework/Ver static const char *ocllib[] = { "libOpenCL", "libOpenCL.so", "libOpenCL.so.1", NULL }; #endif +void dt_dlopencl_close(dt_dlopencl_t *ocl) +{ + free(ocl->symbols); + g_free(ocl->library); +#ifndef __APPLE__ + if(!g_module_close(ocl->gmodule)) + dt_print(DT_DEBUG_OPENCL, "Couldn't close OpenCL library"); +#else + if(dlclose(ocl->gmodule)) + dt_print(DT_DEBUG_OPENCL, "Couldn't close OpenCL library"); +#endif + free(ocl); +} /* only for debugging: default noop function for all unassigned function pointers */ void dt_dlopencl_noop(void) @@ -63,9 +80,9 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name) library = name; module = dt_gmodule_open(library); if(module == NULL) - dt_print(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find specified opencl runtime library '%s'", library); + dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] could not find specified opencl runtime library '%s'\n", library); else - dt_print(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found specified opencl runtime library '%s'", library); + dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found specified opencl runtime library '%s'\n", library); } else { @@ -75,15 +92,18 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name) library = *iter; module = dt_gmodule_open(library); if(module == NULL) - dt_print(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find default opencl runtime library '%s'", library); + dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] could not find default opencl runtime library '%s'\n", library); else - dt_print(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found default opencl runtime library '%s'", library); + dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found default opencl runtime library '%s'\n", library); iter++; } } if(module == NULL) + { + dt_print_nts(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find any opencl runtime library\n"); return NULL; + } /* now bind symbols */ ocl = malloc(sizeof(dt_dlopencl_t)); @@ -98,6 +118,7 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name) if(ocl->symbols == NULL) { + dt_print_nts(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find symbols in opencl runtime library\n"); free(ocl); free(module); return NULL; @@ -209,7 +230,9 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name) ocl->have_opencl = success; if(!success) - dt_print(DT_DEBUG_OPENCL, "[opencl_init] could not load all required symbols from library"); + dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] could not load all required symbols from library\n"); + else + ocl->gmodule = module->gmodule; free(module); diff --git a/src/common/dlopencl.h b/src/common/dlopencl.h index b30703857204..5f93c1451b4c 100644 --- a/src/common/dlopencl.h +++ b/src/common/dlopencl.h @@ -1,6 +1,6 @@ /* This file is part of darktable, - Copyright (C) 2011-2023 darktable developers. + Copyright (C) 2011-2026 darktable developers. darktable is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -215,6 +215,11 @@ typedef struct dt_dlopencl_t gboolean have_opencl; dt_dlopencl_symbols_t *symbols; char *library; +#ifndef __APPLE__ + GModule *gmodule; +#else + void *gmodule; +#endif } dt_dlopencl_t; /* default noop function for all unassigned function pointers */ @@ -223,6 +228,9 @@ void dt_dlopencl_noop(void); /* dynamically load OpenCL library and bind needed functions */ dt_dlopencl_t *dt_dlopencl_init(const char *); +/* dynamically close OpenCL library opened by dt_dlopencl_init() */ +void dt_dlopencl_close(dt_dlopencl_t *ocl); + #endif // HAVE_OPENCL // clang-format off diff --git a/src/common/opencl.c b/src/common/opencl.c index a1f5ca4a6cc2..661224296370 100644 --- a/src/common/opencl.c +++ b/src/common/opencl.c @@ -1659,9 +1659,7 @@ void dt_opencl_cleanup(dt_opencl_t *cl) if(cl->dlocl) { - free(cl->dlocl->symbols); - g_free(cl->dlocl->library); - free(cl->dlocl); + dt_dlopencl_close(cl->dlocl); } free(cl->dev);