@@ -188,7 +188,41 @@ unsigned acl_convert_mmd_capabilities(unsigned mmd_capabilities);
188188const static size_t MIN_SOF_SIZE = 1 ;
189189const static size_t MIN_PLL_CONFIG_SIZE = 1 ;
190190
191+ // Wrapper for dl libraries
192+ struct dl_wrapper_t {
193+ std::vector<void *> mmd_libs;
194+ dl_wrapper_t () { ; }
195+ ~dl_wrapper_t () {
196+ for (void *mmd_lib : this ->mmd_libs ) {
197+ if (mmd_lib) {
198+ #ifdef _WIN32
199+ FreeLibrary ((HMODULE)mmd_lib);
200+ #else
201+ dlclose (mmd_lib);
202+ #endif
203+ }
204+ }
205+ }
206+
207+ // destroy last element of mmd_libs
208+ void pop_back () {
209+ if (this ->mmd_libs .back ()) {
210+ #ifdef _WIN32
211+ FreeLibrary ((HMODULE)this ->mmd_libs .back ());
212+ #else
213+ dlclose (this ->mmd_libs .back ());
214+ #endif
215+ }
216+ this ->mmd_libs .pop_back ();
217+ }
218+
219+ // prohibit copying to avoid double-close of handle
220+ dl_wrapper_t (const dl_wrapper_t &) = delete ;
221+ dl_wrapper_t &operator =(const dl_wrapper_t &) = delete ;
222+ };
223+
191224std::vector<acl_mmd_dispatch_t > internal_mmd_dispatch;
225+ static dl_wrapper_t dl_wrapper;
192226
193227// Dynamically load board mmd & symbols
194228static size_t num_board_pkgs;
@@ -568,8 +602,9 @@ cl_bool l_load_single_board_library(const char *library_name,
568602 return CL_FALSE;
569603 }
570604#endif
571- auto *mmd_library = my_dlopen (library_name, &error_msg);
572- if (!mmd_library) {
605+ dl_wrapper.mmd_libs .push_back (my_dlopen (library_name, &error_msg));
606+ if (!dl_wrapper.mmd_libs .back ()) {
607+ dl_wrapper.pop_back ();
573608 std::cout << " Error: Could not load board library " << library_name;
574609 if (error_msg && error_msg[0 ] != ' \0 ' ) {
575610 std::cout << " (error_msg: " << error_msg << " )" ;
@@ -578,18 +613,19 @@ cl_bool l_load_single_board_library(const char *library_name,
578613 return CL_FALSE;
579614 }
580615
581- auto *test_symbol =
582- my_dlsym (mmd_library, " aocl_mmd_get_offline_info" , &error_msg);
616+ auto *test_symbol = my_dlsym (dl_wrapper. mmd_libs . back (),
617+ " aocl_mmd_get_offline_info" , &error_msg);
583618 if (!test_symbol) {
584619 // On Linux, for custom libraries close the library (which was opened
585620 // locally) and then reopen globally. For Windows, there is no option (i.e.
586621 // it is always global)
587622#ifdef __linux__
588- my_dlclose (mmd_library );
623+ my_dlclose (dl_wrapper. mmd_libs . back () );
589624 ACL_HAL_DEBUG_MSG_VERBOSE (
590625 1 , " This library is a custom library. Opening globally.\n " );
591- mmd_library = my_dlopen_global (library_name, &error_msg);
592- if (!mmd_library) {
626+ dl_wrapper.mmd_libs .back () = my_dlopen_global (library_name, &error_msg);
627+ if (!dl_wrapper.mmd_libs .back ()) {
628+ dl_wrapper.pop_back ();
593629 std::cout << " Error: Could not load custom library " << library_name;
594630 if (error_msg && error_msg[0 ] != ' \0 ' ) {
595631 std::cout << " (error_msg: " << error_msg << " )" ;
@@ -600,9 +636,9 @@ cl_bool l_load_single_board_library(const char *library_name,
600636#endif
601637 } else {
602638 if (load_libraries) {
603- auto result =
604- l_load_board_functions ( &(internal_mmd_dispatch[num_boards_found]),
605- library_name, mmd_library , error_msg);
639+ auto result = l_load_board_functions (
640+ &(internal_mmd_dispatch[num_boards_found]), library_name ,
641+ dl_wrapper. mmd_libs . back () , error_msg);
606642 if (result == CL_FALSE) {
607643 std::cout << " Error: Could not load board library " << library_name
608644 << " due to failure to load symbols\n " ;
@@ -855,6 +891,7 @@ cl_bool l_load_board_libraries(cl_bool load_libraries) {
855891 }
856892 fin.close ();
857893 }
894+ closedir (dir);
858895 }
859896
860897 if (num_vendor_files_found == 0 ) {
@@ -876,8 +913,6 @@ cl_bool l_load_board_libraries(cl_bool load_libraries) {
876913 }
877914 }
878915
879- if (dir)
880- closedir (dir);
881916 return num_boards_found == 0 ? CL_FALSE : CL_TRUE;
882917}
883918#endif
@@ -1156,9 +1191,10 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
11561191 ipa_simulator ? mmd_lib_name_ipa : mmd_lib_name_aoc;
11571192
11581193 char *error_msg = nullptr ;
1159- auto *mmd_lib = my_dlopen (mmd_lib_name, &error_msg);
1194+ dl_wrapper. mmd_libs . push_back ( my_dlopen (mmd_lib_name, &error_msg) );
11601195 typedef acl_mmd_dispatch_t *(*fcn_type)();
1161- if (!mmd_lib) {
1196+ if (!dl_wrapper.mmd_libs .back ()) {
1197+ dl_wrapper.pop_back ();
11621198 std::cout << " Error: Could not load simulation MMD library "
11631199 << mmd_lib_name;
11641200 if (error_msg && error_msg[0 ] != ' \0 ' ) {
@@ -1167,8 +1203,9 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
11671203 std::cout << " \n " ;
11681204 return nullptr ;
11691205 }
1170- auto *sym = my_dlsym (mmd_lib , sym_name, &error_msg);
1206+ auto *sym = my_dlsym (dl_wrapper. mmd_libs . back () , sym_name, &error_msg);
11711207 if (!sym) {
1208+ dl_wrapper.pop_back ();
11721209 std::cout << " Error: Symbol " << sym_name
11731210 << " not found in simulation MMD library " ;
11741211 if (error_msg && error_msg[0 ] != ' \0 ' ) {
0 commit comments