16
16
#include < cstring>
17
17
#include < fstream>
18
18
#include < iostream>
19
+ #include < memory>
20
+ #include < optional>
19
21
#include < string>
20
22
#include < vector>
21
23
#ifdef __linux__
@@ -188,8 +190,6 @@ unsigned acl_convert_mmd_capabilities(unsigned mmd_capabilities);
188
190
const static size_t MIN_SOF_SIZE = 1 ;
189
191
const static size_t MIN_PLL_CONFIG_SIZE = 1 ;
190
192
191
- std::vector<acl_mmd_dispatch_t > internal_mmd_dispatch;
192
-
193
193
// Dynamically load board mmd & symbols
194
194
static size_t num_board_pkgs;
195
195
static double min_MMD_version = DBL_MAX;
@@ -434,14 +434,26 @@ static void *my_dlsym(void *library, const char *function_name,
434
434
}
435
435
436
436
static void my_dlclose (void *library) {
437
- acl_assert_locked ();
438
437
#ifdef _WIN32
439
438
FreeLibrary ((HMODULE)library);
440
439
#else
441
440
dlclose (library);
442
441
#endif
443
442
}
444
443
444
+ struct acl_mmd_library {
445
+ struct deleter {
446
+ void operator ()(void *library) { my_dlclose (library); }
447
+ };
448
+
449
+ using pointer = std::unique_ptr<void , deleter>;
450
+
451
+ pointer ptr;
452
+ acl_mmd_dispatch_t dispatch;
453
+ };
454
+
455
+ static std::vector<acl_mmd_library> internal_mmd_dispatch;
456
+
445
457
cl_bool l_load_board_functions (acl_mmd_dispatch_t *mmd_dispatch,
446
458
const char *library_name, void *mmd_library,
447
459
char *error_msg) {
@@ -572,7 +584,7 @@ cl_bool l_load_single_board_library(const char *library_name,
572
584
return CL_FALSE;
573
585
}
574
586
#endif
575
- auto * mmd_library = my_dlopen (library_name, &error_msg);
587
+ acl_mmd_library::pointer mmd_library{ my_dlopen (library_name, &error_msg)} ;
576
588
if (!mmd_library) {
577
589
std::cout << " Error: Could not load board library " << library_name;
578
590
if (error_msg && error_msg[0 ] != ' \0 ' ) {
@@ -583,16 +595,16 @@ cl_bool l_load_single_board_library(const char *library_name,
583
595
}
584
596
585
597
auto *test_symbol =
586
- my_dlsym (mmd_library, " aocl_mmd_get_offline_info" , &error_msg);
598
+ my_dlsym (mmd_library. get () , " aocl_mmd_get_offline_info" , &error_msg);
587
599
if (!test_symbol) {
588
600
// On Linux, for custom libraries close the library (which was opened
589
601
// locally) and then reopen globally. For Windows, there is no option (i.e.
590
602
// it is always global)
591
603
#ifdef __linux__
592
- my_dlclose ( mmd_library);
604
+ mmd_library. reset ( );
593
605
ACL_HAL_DEBUG_MSG_VERBOSE (
594
606
1 , " This library is a custom library. Opening globally.\n " );
595
- mmd_library = my_dlopen_global (library_name, &error_msg);
607
+ mmd_library. reset ( my_dlopen_global (library_name, &error_msg) );
596
608
if (!mmd_library) {
597
609
std::cout << " Error: Could not load custom library " << library_name;
598
610
if (error_msg && error_msg[0 ] != ' \0 ' ) {
@@ -601,17 +613,20 @@ cl_bool l_load_single_board_library(const char *library_name,
601
613
std::cout << " \n " ;
602
614
return CL_FALSE;
603
615
}
616
+ // FIXME mmd_library goes out of scope at the end of this function. How is
617
+ // this custom library used and where should the handle be stored?
604
618
#endif
605
619
} else {
606
620
if (load_libraries) {
607
- auto result =
608
- l_load_board_functions (&( internal_mmd_dispatch[num_boards_found]) ,
609
- library_name, mmd_library, error_msg);
621
+ auto result = l_load_board_functions (
622
+ & internal_mmd_dispatch[num_boards_found]. dispatch , library_name ,
623
+ mmd_library. get () , error_msg);
610
624
if (result == CL_FALSE) {
611
625
std::cout << " Error: Could not load board library " << library_name
612
626
<< " due to failure to load symbols\n " ;
613
627
return result;
614
628
}
629
+ std::swap (mmd_library, internal_mmd_dispatch[num_boards_found].ptr );
615
630
}
616
631
++num_boards_found;
617
632
}
@@ -1134,7 +1149,7 @@ void l_close_device(unsigned int physical_device_id,
1134
1149
return ;
1135
1150
}
1136
1151
1137
- static acl_mmd_dispatch_t * get_msim_mmd_layer () {
1152
+ static std::optional<acl_mmd_library> get_msim_mmd_layer () {
1138
1153
#ifdef _WIN32
1139
1154
1140
1155
const char *acl_root_dir = acl_getenv (" INTELFPGAOCLSDKROOT" );
@@ -1159,7 +1174,7 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
1159
1174
ipa_simulator ? mmd_lib_name_ipa : mmd_lib_name_aoc;
1160
1175
1161
1176
char *error_msg = nullptr ;
1162
- auto * mmd_lib = my_dlopen (mmd_lib_name, &error_msg);
1177
+ acl_mmd_library::pointer mmd_lib{ my_dlopen (mmd_lib_name, &error_msg)} ;
1163
1178
typedef acl_mmd_dispatch_t *(*fcn_type)();
1164
1179
if (!mmd_lib) {
1165
1180
std::cout << " Error: Could not load simulation MMD library "
@@ -1168,17 +1183,17 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
1168
1183
std::cout << " (error_msg: " << error_msg << " )" ;
1169
1184
}
1170
1185
std::cout << " \n " ;
1171
- return nullptr ;
1186
+ return {} ;
1172
1187
}
1173
- auto *sym = my_dlsym (mmd_lib, sym_name, &error_msg);
1188
+ auto *sym = my_dlsym (mmd_lib. get () , sym_name, &error_msg);
1174
1189
if (!sym) {
1175
1190
std::cout << " Error: Symbol " << sym_name
1176
1191
<< " not found in simulation MMD library " ;
1177
1192
if (error_msg && error_msg[0 ] != ' \0 ' ) {
1178
1193
std::cout << " (message: " << error_msg << " )" ;
1179
1194
}
1180
1195
std::cout << " \n " ;
1181
- return nullptr ;
1196
+ return {} ;
1182
1197
}
1183
1198
1184
1199
// Now call the function. Ignore the Windows cast to fcn pointer
@@ -1187,10 +1202,15 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
1187
1202
#pragma warning(push)
1188
1203
#pragma warning(disable : 4055)
1189
1204
#endif
1190
- return ((fcn_type)sym)();
1205
+ acl_mmd_dispatch_t *mmd_dispatch = ((fcn_type)sym)();
1191
1206
#ifdef _MSC_VER
1192
1207
#pragma warning(pop)
1193
1208
#endif
1209
+
1210
+ auto library = std::make_optional<acl_mmd_library>();
1211
+ std::swap (library->ptr , mmd_lib);
1212
+ library->dispatch = *mmd_dispatch;
1213
+ return library;
1194
1214
}
1195
1215
1196
1216
ACL_HAL_EXPORT const acl_hal_t *
@@ -1255,11 +1275,11 @@ acl_mmd_get_system_definition(acl_system_def_t *sys,
1255
1275
if (use_offline_only == ACL_CONTEXT_MPSIM) {
1256
1276
1257
1277
// Substitute the simulator MMD layer.
1258
- auto * result = get_msim_mmd_layer ();
1259
- if (!result)
1278
+ auto result = get_msim_mmd_layer ();
1279
+ if (!result) {
1260
1280
return nullptr ;
1261
- else
1262
- internal_mmd_dispatch.push_back (*result);
1281
+ }
1282
+ internal_mmd_dispatch.push_back (std::move ( *result) );
1263
1283
1264
1284
ACL_HAL_DEBUG_MSG_VERBOSE (1 , " Use simulation MMD\n " );
1265
1285
num_board_pkgs = 1 ;
@@ -1269,7 +1289,7 @@ acl_mmd_get_system_definition(acl_system_def_t *sys,
1269
1289
internal_mmd_dispatch.resize (num_board_pkgs);
1270
1290
ACL_HAL_DEBUG_MSG_VERBOSE (1 , " Board MMD is statically linked\n " );
1271
1291
1272
- acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[0 ];
1292
+ acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[0 ]. dispatch ;
1273
1293
1274
1294
dispatch.library_name = " runtime_static" ;
1275
1295
dispatch.mmd_library = nullptr ;
@@ -1345,7 +1365,7 @@ acl_mmd_get_system_definition(acl_system_def_t *sys,
1345
1365
sys->num_devices = 0 ;
1346
1366
num_physical_devices = 0 ;
1347
1367
for (unsigned iboard = 0 ; iboard < num_board_pkgs; ++iboard) {
1348
- acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard];
1368
+ acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard]. dispatch ;
1349
1369
1350
1370
dispatch.aocl_mmd_get_offline_info (AOCL_MMD_VERSION, sizeof (buf), buf,
1351
1371
NULL );
@@ -1417,7 +1437,7 @@ int acl_hal_mmd_try_devices(cl_uint num_devices, const cl_device_id *devices,
1417
1437
// names might get cached by various routines
1418
1438
1419
1439
for (unsigned iboard = 0 ; iboard < num_board_pkgs; ++iboard) {
1420
- acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard];
1440
+ acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard]. dispatch ;
1421
1441
1422
1442
dispatch.aocl_mmd_get_offline_info (AOCL_MMD_BOARD_NAMES,
1423
1443
MAX_BOARD_NAMES_LEN, buf, NULL );
@@ -1487,7 +1507,7 @@ int acl_hal_mmd_try_devices(cl_uint num_devices, const cl_device_id *devices,
1487
1507
physical_device_id = 0 ;
1488
1508
1489
1509
for (unsigned iboard = 0 ; iboard < num_board_pkgs; ++iboard) {
1490
- acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard];
1510
+ acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard]. dispatch ;
1491
1511
1492
1512
dispatch.aocl_mmd_get_offline_info (AOCL_MMD_BOARD_NAMES,
1493
1513
MAX_BOARD_NAMES_LEN, buf, NULL );
0 commit comments