| 15 | #endif |
| 16 | |
| 17 | cache_t *create_cache_external(const char *const cache_alg_name, |
| 18 | common_cache_params_t cc_params, |
| 19 | void *cache_specific_params) { |
| 20 | void *handle; |
| 21 | char *error; |
| 22 | cache_t *(*cache_init)(common_cache_params_t, void *); |
| 23 | |
| 24 | char shared_lib_path[256]; |
| 25 | char cache_init_func_name[256]; |
| 26 | sprintf(shared_lib_path, "./lib%s.so", cache_alg_name); |
| 27 | sprintf(cache_init_func_name, "%s_init", cache_alg_name); |
| 28 | |
| 29 | handle = dlopen(shared_lib_path, RTLD_LAZY); |
| 30 | if (!handle) { |
| 31 | fprintf(stderr, "%s\n", dlerror()); |
| 32 | exit(EXIT_FAILURE); |
| 33 | } |
| 34 | dlerror(); /* Clear any existing error */ |
| 35 | |
| 36 | // ISO C compliant way to convert void* to function pointer |
| 37 | union { |
| 38 | void *obj_ptr; |
| 39 | cache_t *(*func_ptr)(common_cache_params_t, void *); |
| 40 | } dlsym_ptr; |
| 41 | |
| 42 | dlsym_ptr.obj_ptr = dlsym(handle, cache_init_func_name); |
| 43 | cache_init = dlsym_ptr.func_ptr; |
| 44 | |
| 45 | if ((error = dlerror()) != NULL) { |
| 46 | fprintf(stderr, "%s\n", error); |
| 47 | exit(EXIT_FAILURE); |
| 48 | } else { |
| 49 | INFO("external cache %s loaded\n", cache_alg_name); |
| 50 | } |
| 51 | cache_t *cache = cache_init(cc_params, cache_specific_params); |
| 52 | |
| 53 | // disable dlclose for now, we need a global pool of handles that we can track |
| 54 | // and close |
| 55 | // dlclose(handle); |
| 56 | return cache; |
| 57 | } |
| 58 | |
| 59 | cache_t *create_cache_internal(const char *const cache_alg_name, |
| 60 | common_cache_params_t cc_params, |