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;
@@ -426,14 +426,26 @@ static void *my_dlsym(void *library, const char *function_name,
426
426
}
427
427
428
428
static void my_dlclose (void *library) {
429
- acl_assert_locked ();
430
429
#ifdef _WIN32
431
430
FreeLibrary ((HMODULE)library);
432
431
#else
433
432
dlclose (library);
434
433
#endif
435
434
}
436
435
436
+ struct acl_mmd_library {
437
+ struct deleter {
438
+ void operator ()(void *library) { my_dlclose (library); }
439
+ };
440
+
441
+ using pointer = std::unique_ptr<void , deleter>;
442
+
443
+ pointer ptr;
444
+ acl_mmd_dispatch_t dispatch;
445
+ };
446
+
447
+ static std::vector<acl_mmd_library> internal_mmd_dispatch;
448
+
437
449
cl_bool l_load_board_functions (acl_mmd_dispatch_t *mmd_dispatch,
438
450
const char *library_name, void *mmd_library,
439
451
char *error_msg) {
@@ -564,7 +576,7 @@ cl_bool l_load_single_board_library(const char *library_name,
564
576
return CL_FALSE;
565
577
}
566
578
#endif
567
- auto * mmd_library = my_dlopen (library_name, &error_msg);
579
+ acl_mmd_library::pointer mmd_library{ my_dlopen (library_name, &error_msg)} ;
568
580
if (!mmd_library) {
569
581
std::cout << " Error: Could not load board library " << library_name;
570
582
if (error_msg && error_msg[0 ] != ' \0 ' ) {
@@ -575,14 +587,15 @@ cl_bool l_load_single_board_library(const char *library_name,
575
587
}
576
588
577
589
if (load_libraries) {
578
- auto result =
579
- l_load_board_functions (&( internal_mmd_dispatch[num_boards_found]) ,
580
- library_name, mmd_library, error_msg);
590
+ auto result = l_load_board_functions (
591
+ & internal_mmd_dispatch[num_boards_found]. dispatch , library_name ,
592
+ mmd_library. get () , error_msg);
581
593
if (result == CL_FALSE) {
582
594
std::cout << " Error: Could not load board library " << library_name
583
595
<< " due to failure to load symbols\n " ;
584
596
return result;
585
597
}
598
+ std::swap (mmd_library, internal_mmd_dispatch[num_boards_found].ptr );
586
599
}
587
600
++num_boards_found;
588
601
@@ -1104,7 +1117,7 @@ void l_close_device(unsigned int physical_device_id,
1104
1117
return ;
1105
1118
}
1106
1119
1107
- static acl_mmd_dispatch_t * get_msim_mmd_layer () {
1120
+ static std::optional<acl_mmd_library> get_msim_mmd_layer () {
1108
1121
#ifdef _WIN32
1109
1122
1110
1123
const char *acl_root_dir = acl_getenv (" INTELFPGAOCLSDKROOT" );
@@ -1129,7 +1142,7 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
1129
1142
ipa_simulator ? mmd_lib_name_ipa : mmd_lib_name_aoc;
1130
1143
1131
1144
char *error_msg = nullptr ;
1132
- auto * mmd_lib = my_dlopen (mmd_lib_name, &error_msg);
1145
+ acl_mmd_library::pointer mmd_lib{ my_dlopen (mmd_lib_name, &error_msg)} ;
1133
1146
typedef acl_mmd_dispatch_t *(*fcn_type)();
1134
1147
if (!mmd_lib) {
1135
1148
std::cout << " Error: Could not load simulation MMD library "
@@ -1138,17 +1151,17 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
1138
1151
std::cout << " (error_msg: " << error_msg << " )" ;
1139
1152
}
1140
1153
std::cout << " \n " ;
1141
- return nullptr ;
1154
+ return {} ;
1142
1155
}
1143
- auto *sym = my_dlsym (mmd_lib, sym_name, &error_msg);
1156
+ auto *sym = my_dlsym (mmd_lib. get () , sym_name, &error_msg);
1144
1157
if (!sym) {
1145
1158
std::cout << " Error: Symbol " << sym_name
1146
1159
<< " not found in simulation MMD library " ;
1147
1160
if (error_msg && error_msg[0 ] != ' \0 ' ) {
1148
1161
std::cout << " (message: " << error_msg << " )" ;
1149
1162
}
1150
1163
std::cout << " \n " ;
1151
- return nullptr ;
1164
+ return {} ;
1152
1165
}
1153
1166
1154
1167
// Now call the function. Ignore the Windows cast to fcn pointer
@@ -1157,10 +1170,13 @@ static acl_mmd_dispatch_t *get_msim_mmd_layer() {
1157
1170
#pragma warning(push)
1158
1171
#pragma warning(disable : 4055)
1159
1172
#endif
1160
- return ((fcn_type)sym)();
1173
+ acl_mmd_dispatch_t *mmd_dispatch = ((fcn_type)sym)();
1161
1174
#ifdef _MSC_VER
1162
1175
#pragma warning(pop)
1163
1176
#endif
1177
+
1178
+ return std::make_optional<acl_mmd_library>(
1179
+ {std::move (mmd_lib), *mmd_dispatch});
1164
1180
}
1165
1181
1166
1182
ACL_HAL_EXPORT const acl_hal_t *
@@ -1225,11 +1241,11 @@ acl_mmd_get_system_definition(acl_system_def_t *sys,
1225
1241
if (use_offline_only == ACL_CONTEXT_MPSIM) {
1226
1242
1227
1243
// Substitute the simulator MMD layer.
1228
- auto * result = get_msim_mmd_layer ();
1229
- if (!result)
1244
+ auto result = get_msim_mmd_layer ();
1245
+ if (!result) {
1230
1246
return nullptr ;
1231
- else
1232
- internal_mmd_dispatch.push_back (*result);
1247
+ }
1248
+ internal_mmd_dispatch.push_back (std::move ( *result) );
1233
1249
1234
1250
ACL_HAL_DEBUG_MSG_VERBOSE (1 , " Use simulation MMD\n " );
1235
1251
num_board_pkgs = 1 ;
@@ -1239,7 +1255,7 @@ acl_mmd_get_system_definition(acl_system_def_t *sys,
1239
1255
internal_mmd_dispatch.resize (num_board_pkgs);
1240
1256
ACL_HAL_DEBUG_MSG_VERBOSE (1 , " Board MMD is statically linked\n " );
1241
1257
1242
- acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[0 ];
1258
+ acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[0 ]. dispatch ;
1243
1259
1244
1260
dispatch.library_name = " runtime_static" ;
1245
1261
dispatch.mmd_library = nullptr ;
@@ -1315,7 +1331,7 @@ acl_mmd_get_system_definition(acl_system_def_t *sys,
1315
1331
sys->num_devices = 0 ;
1316
1332
num_physical_devices = 0 ;
1317
1333
for (unsigned iboard = 0 ; iboard < num_board_pkgs; ++iboard) {
1318
- acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard];
1334
+ acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard]. dispatch ;
1319
1335
1320
1336
dispatch.aocl_mmd_get_offline_info (AOCL_MMD_VERSION, sizeof (buf), buf,
1321
1337
NULL );
@@ -1387,7 +1403,7 @@ int acl_hal_mmd_try_devices(cl_uint num_devices, const cl_device_id *devices,
1387
1403
// names might get cached by various routines
1388
1404
1389
1405
for (unsigned iboard = 0 ; iboard < num_board_pkgs; ++iboard) {
1390
- acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard];
1406
+ acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard]. dispatch ;
1391
1407
1392
1408
dispatch.aocl_mmd_get_offline_info (AOCL_MMD_BOARD_NAMES,
1393
1409
MAX_BOARD_NAMES_LEN, buf, NULL );
@@ -1457,7 +1473,7 @@ int acl_hal_mmd_try_devices(cl_uint num_devices, const cl_device_id *devices,
1457
1473
physical_device_id = 0 ;
1458
1474
1459
1475
for (unsigned iboard = 0 ; iboard < num_board_pkgs; ++iboard) {
1460
- acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard];
1476
+ acl_mmd_dispatch_t &dispatch = internal_mmd_dispatch[iboard]. dispatch ;
1461
1477
1462
1478
dispatch.aocl_mmd_get_offline_info (AOCL_MMD_BOARD_NAMES,
1463
1479
MAX_BOARD_NAMES_LEN, buf, NULL );
0 commit comments