Skip to content

Commit 396fc7b

Browse files
anyj0527again4you
authored andcommitted
[fix] Keep global copy of opened backends.
- Maintaining a global copy of opened backends in GHashTable to mitigate memory fragmentation in iterations of [hal_ml_create - hal_ml_destroy] which load and unload backend libraries. - This extra reference prevents the underlying library from being unloaded when the user calls hal_ml_destroy. - Adding a destructor function to release pinned backends. This would resolve [this issue](https://github.sec.samsung.net/OASIS/ML-Service-API-DA-Examples/issues/12) Signed-off-by: Yongjoo Ahn <yongjoo1.ahn@samsung.com>
1 parent c885ba6 commit 396fc7b

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

src/hal-api-ml.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,36 @@ hal_ml_exit_backend (void *data, void *user_data)
6262

6363
static int hal_ml_backend_count = 0;
6464
static gchar **hal_ml_backend_names = NULL;
65+
static GHashTable *hal_ml_cached_backends = NULL;
66+
G_LOCK_DEFINE_STATIC (hal_ml_cached_backends_lock);
67+
68+
/**
69+
* @brief Destructor to clean up global resources when the library is unloaded.
70+
*/
71+
static void __attribute__ ((destructor))
72+
hal_ml_cleanup_global (void)
73+
{
74+
if (hal_ml_cached_backends) {
75+
GHashTableIter iter;
76+
gpointer key, value;
77+
78+
g_hash_table_iter_init (&iter, hal_ml_cached_backends);
79+
while (g_hash_table_iter_next (&iter, &key, &value)) {
80+
hal_backend_ml_funcs *funcs = (hal_backend_ml_funcs *) value;
81+
gchar *lib_name = (gchar *) key;
82+
83+
hal_common_put_backend_with_library_name_v2 (HAL_MODULE_ML,
84+
(void *) funcs, NULL, hal_ml_exit_backend, lib_name);
85+
}
86+
g_hash_table_destroy (hal_ml_cached_backends);
87+
hal_ml_cached_backends = NULL;
88+
}
89+
90+
if (hal_ml_backend_names) {
91+
g_clear_pointer (&hal_ml_backend_names, g_strfreev);
92+
hal_ml_backend_count = 0;
93+
}
94+
}
6595

6696
#define MAX_LIB_NAME_LENGTH 256
6797

@@ -211,6 +241,29 @@ hal_ml_create (const char *backend_name, hal_ml_h *handle)
211241
for (int i = 0; i < hal_ml_backend_count; i++) {
212242
if (g_strrstr (hal_ml_backend_names[i], backend_name) != NULL) {
213243
gchar *backend_lib_name = hal_ml_backend_names[i];
244+
245+
G_LOCK (hal_ml_cached_backends_lock);
246+
if (!hal_ml_cached_backends) {
247+
hal_ml_cached_backends
248+
= g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
249+
}
250+
251+
if (!g_hash_table_lookup (hal_ml_cached_backends, backend_lib_name)) {
252+
hal_backend_ml_funcs *cached_funcs = NULL;
253+
int ret = hal_common_get_backend_with_library_name_v2 (HAL_MODULE_ML,
254+
(void **) &cached_funcs, NULL, hal_ml_create_backend,
255+
backend_lib_name);
256+
257+
if (ret == 0 && cached_funcs) {
258+
_I ("Backend %s cached.", backend_lib_name);
259+
g_hash_table_insert (hal_ml_cached_backends,
260+
g_strdup (backend_lib_name), cached_funcs);
261+
} else {
262+
_W ("Failed to cache backend %s", backend_lib_name);
263+
}
264+
}
265+
G_UNLOCK (hal_ml_cached_backends_lock);
266+
214267
hal_ml_s *new_handle = g_new0 (hal_ml_s, 1);
215268
if (!new_handle) {
216269
return HAL_ML_ERROR_OUT_OF_MEMORY;

0 commit comments

Comments
 (0)