11/*
22 This file is part of darktable,
3- Copyright (C) 2011-2023 darktable developers.
3+ Copyright (C) 2011-2026 darktable developers.
44
55 darktable is free software: you can redistribute it and/or modify
66 it under the terms of the GNU General Public License as published by
2222#include "common/dynload.h"
2323#include "common/dlopencl.h"
2424
25+ #ifdef __APPLE__
26+ #include <dlfcn.h>
27+ #endif
28+
2529#include <assert.h>
2630#include <signal.h>
2731#include <stdio.h>
@@ -36,6 +40,19 @@ static const char *ocllib[] = { "/System/Library/Frameworks/OpenCL.framework/Ver
3640static const char * ocllib [] = { "libOpenCL" , "libOpenCL.so" , "libOpenCL.so.1" , NULL };
3741#endif
3842
43+ void dt_dlopencl_close (dt_dlopencl_t * ocl )
44+ {
45+ free (ocl -> symbols );
46+ g_free (ocl -> library );
47+ #ifndef __APPLE__
48+ if (!g_module_close (ocl -> gmodule ))
49+ dt_print (DT_DEBUG_OPENCL , "Couldn't close OpenCL library" );
50+ #else
51+ if (dlclose (ocl -> gmodule ))
52+ dt_print (DT_DEBUG_OPENCL , "Couldn't close OpenCL library" );
53+ #endif
54+ free (ocl );
55+ }
3956
4057/* only for debugging: default noop function for all unassigned function pointers */
4158void dt_dlopencl_noop (void )
@@ -63,9 +80,9 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
6380 library = name ;
6481 module = dt_gmodule_open (library );
6582 if (module == NULL )
66- dt_print (DT_DEBUG_OPENCL , "[dt_dlopencl_init] could not find specified opencl runtime library '%s'" , library );
83+ dt_print_nts (DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE , "[dt_dlopencl_init] could not find specified opencl runtime library '%s'\n " , library );
6784 else
68- dt_print (DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE , "[dt_dlopencl_init] found specified opencl runtime library '%s'" , library );
85+ dt_print_nts (DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE , "[dt_dlopencl_init] found specified opencl runtime library '%s'\n " , library );
6986 }
7087 else
7188 {
@@ -75,15 +92,18 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
7592 library = * iter ;
7693 module = dt_gmodule_open (library );
7794 if (module == NULL )
78- dt_print (DT_DEBUG_OPENCL , "[dt_dlopencl_init] could not find default opencl runtime library '%s'" , library );
95+ dt_print_nts (DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE , "[dt_dlopencl_init] could not find default opencl runtime library '%s'\n " , library );
7996 else
80- dt_print (DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE , "[dt_dlopencl_init] found default opencl runtime library '%s'" , library );
97+ dt_print_nts (DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE , "[dt_dlopencl_init] found default opencl runtime library '%s'\n " , library );
8198 iter ++ ;
8299 }
83100 }
84101
85102 if (module == NULL )
103+ {
104+ dt_print_nts (DT_DEBUG_OPENCL , "[dt_dlopencl_init] could not find any opencl runtime library\n" );
86105 return NULL ;
106+ }
87107
88108 /* now bind symbols */
89109 ocl = malloc (sizeof (dt_dlopencl_t ));
@@ -98,6 +118,7 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
98118
99119 if (ocl -> symbols == NULL )
100120 {
121+ dt_print_nts (DT_DEBUG_OPENCL , "[dt_dlopencl_init] could not find symbols in opencl runtime library\n" );
101122 free (ocl );
102123 free (module );
103124 return NULL ;
@@ -209,7 +230,9 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
209230 ocl -> have_opencl = success ;
210231
211232 if (!success )
212- dt_print (DT_DEBUG_OPENCL , "[opencl_init] could not load all required symbols from library" );
233+ dt_print_nts (DT_DEBUG_OPENCL , "[opencl_init] could not load all required symbols from library\n" );
234+ else
235+ ocl -> gmodule = module -> gmodule ;
213236
214237 free (module );
215238
0 commit comments