@@ -188,7 +188,41 @@ unsigned acl_convert_mmd_capabilities(unsigned mmd_capabilities);
188
188
const static size_t MIN_SOF_SIZE = 1 ;
189
189
const static size_t MIN_PLL_CONFIG_SIZE = 1 ;
190
190
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
+
191
224
std::vector<acl_mmd_dispatch_t > internal_mmd_dispatch;
225
+ static dl_wrapper_t dl_wrapper;
192
226
193
227
// Dynamically load board mmd & symbols
194
228
static size_t num_board_pkgs;
@@ -568,8 +602,9 @@ cl_bool l_load_single_board_library(const char *library_name,
568
602
return CL_FALSE;
569
603
}
570
604
#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 ();
573
608
std::cout << " Error: Could not load board library " << library_name;
574
609
if (error_msg && error_msg[0 ] != ' \0 ' ) {
575
610
std::cout << " (error_msg: " << error_msg << " )" ;
@@ -578,18 +613,19 @@ cl_bool l_load_single_board_library(const char *library_name,
578
613
return CL_FALSE;
579
614
}
580
615
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);
583
618
if (!test_symbol) {
584
619
// On Linux, for custom libraries close the library (which was opened
585
620
// locally) and then reopen globally. For Windows, there is no option (i.e.
586
621
// it is always global)
587
622
#ifdef __linux__
588
- my_dlclose (mmd_library );
623
+ my_dlclose (dl_wrapper. mmd_libs . back () );
589
624
ACL_HAL_DEBUG_MSG_VERBOSE (
590
625
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 ();
593
629
std::cout << " Error: Could not load custom library " << library_name;
594
630
if (error_msg && error_msg[0 ] != ' \0 ' ) {
595
631
std::cout << " (error_msg: " << error_msg << " )" ;
@@ -600,9 +636,9 @@ cl_bool l_load_single_board_library(const char *library_name,
600
636
#endif
601
637
} else {
602
638
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);
606
642
if (result == CL_FALSE) {
607
643
std::cout << " Error: Could not load board library " << library_name
608
644
<< " due to failure to load symbols\n " ;
@@ -1155,9 +1191,10 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
1155
1191
ipa_simulator ? mmd_lib_name_ipa : mmd_lib_name_aoc;
1156
1192
1157
1193
char *error_msg = nullptr ;
1158
- auto *mmd_lib = my_dlopen (mmd_lib_name, &error_msg);
1194
+ dl_wrapper. mmd_libs . push_back ( my_dlopen (mmd_lib_name, &error_msg) );
1159
1195
typedef acl_mmd_dispatch_t *(*fcn_type)();
1160
- if (!mmd_lib) {
1196
+ if (!dl_wrapper.mmd_libs .back ()) {
1197
+ dl_wrapper.pop_back ();
1161
1198
std::cout << " Error: Could not load simulation MMD library "
1162
1199
<< mmd_lib_name;
1163
1200
if (error_msg && error_msg[0 ] != ' \0 ' ) {
@@ -1166,8 +1203,9 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
1166
1203
std::cout << " \n " ;
1167
1204
return nullptr ;
1168
1205
}
1169
- auto *sym = my_dlsym (mmd_lib , sym_name, &error_msg);
1206
+ auto *sym = my_dlsym (dl_wrapper. mmd_libs . back () , sym_name, &error_msg);
1170
1207
if (!sym) {
1208
+ dl_wrapper.pop_back ();
1171
1209
std::cout << " Error: Symbol " << sym_name
1172
1210
<< " not found in simulation MMD library " ;
1173
1211
if (error_msg && error_msg[0 ] != ' \0 ' ) {
0 commit comments