forked from GPUOpen-Tools/gpu_performance_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_perf_api_interface_loader.h
More file actions
692 lines (575 loc) · 23.5 KB
/
Copy pathgpu_perf_api_interface_loader.h
File metadata and controls
692 lines (575 loc) · 23.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//==============================================================================
// Copyright (c) 2017-2020 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief GPA Interface Loader Utility header file
//==============================================================================
// Note: For usage, copy and paste (and then uncomment) the
// following four lines into a compilation unit that uses
// this header file. These are needed to initialize
// static/extern data declared in this header file:
// #ifdef __cplusplus
// GPAApiManager* GPAApiManager::m_pGpaApiManager = nullptr;
// #endif
// GPAFuncTableInfo* g_pFuncTableInfo = NULL;
// In order to use this header file with a debug build of GPA
// the "USE_DEBUG_GPA" preprocessor macro should be defined before
// including this header file
// In order to use this header file with an internal build of GPA
// the "USE_INTERNAL_GPA" preprocessor macro should be defined before
// including this header file
#ifndef _GPA_INTERFACE_LOADER_H_
#define _GPA_INTERFACE_LOADER_H_
#ifdef _WIN32
#include <Windows.h>
#define GPA_MAX_PATH MAX_PATH ///< Macro for max path length
#ifdef _WIN64
#define GPA_IS_64_BIT ///< Macro specifying 64-bit build
#else
#define GPA_IS_32_BIT ///< Macro specifying 32-bit build
#endif
#else
#include <dlfcn.h>
#include <unistd.h>
#define GPA_MAX_PATH 4096 ///< Macro for max path length
#ifdef __x86_64__
#define GPA_IS_64_BIT ///< Macro specifying 64-bit build
#else
#define GPA_IS_32_BIT ///< Macro specifying 32-bit build
#endif
#endif
#include <wchar.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
#include <string>
#endif
#include "gpu_perf_api.h"
#ifdef UNICODE
typedef wchar_t LocaleChar; ///< Typedef for ANSI vs. Unicode character
#ifdef __cplusplus
typedef std::wstring LocaleString; ///< Typedef for ANSI vs. Unicode string
#endif
#define TFORMAT2(x) L##x ///< Macro for string expansion
#define TFORMAT(x) TFORMAT2(x) ///< Macro for string expansion
#define STR_CAT(dest, destSize, src) wcscat_s(dest, destSize, src) ///< Macro for safe strcat
#define STR_COPY(dest, destSize, src) wcscpy_s(dest, destSize, src) ///< Macro for safe strcpy
#define STR_NCOPY(dest, destSize, src, count) wcsncpy_s(dest, destSize, src, count); ///< Macro for safe strncpy
#define STR_LEN(str, strLength) wcsnlen_s(str, strLength) ///< Macro for safe strnlen
#define MEM_CPY(dest, src, count) wmemcpy(dest, src, count)
#define MEM_SET(ptr, wc, num) wmemset(ptr, wc, num)
#else
typedef char LocaleChar; ///< Typedef for ANSI vs. Unicode character
#ifdef __cplusplus
typedef std::string LocaleString; ///< Typedef for ANSI vs. Unicode string
#endif
#define TFORMAT(x) (x) ///< Macro for string expansion
#define STR_CAT(dest, destSize, src) strcat_s(dest, destSize, src) ///< Macro for safe strcat
#define STR_COPY(dest, destSize, src) strcpy_s(dest, destSize, src) ///< Macro for safe strcpy
#define STR_NCOPY(dest, destSize, src, count) strncpy_s(dest, destSize, src, count); ///< Macro for safe strncpy
#define STR_LEN(str, strLength) strnlen_s(str, strLength) ///< Macro for safe strnlen
#define MEM_CPY(dest, src, count) memcpy(dest, src, count)
#define MEM_SET(ptr, wc, num) memset(ptr, wc, num)
#endif
#define GPA_OPENCL_LIB TFORMAT("GPUPerfAPICL") ///< Macro for base name of GPA OpenCL library
#define GPA_OPENGL_LIB TFORMAT("GPUPerfAPIGL") ///< Macro for base name of GPA OpenGL library
#define GPA_DIRECTX11_LIB TFORMAT("GPUPerfAPIDX11") ///< Macro for base name of GPA DirectX 11 library
#define GPA_DIRECTX12_LIB TFORMAT("GPUPerfAPIDX12") ///< Macro for base name of GPA DirectX 12 library
#define GPA_VULKAN_LIB TFORMAT("GPUPerfAPIVK") ///< Macro for base name of GPA Vulkan library
#ifdef _WIN32
#define GPA_LIB_PREFIX TFORMAT("") ///< Macro for platform-specific lib file prefix
#define GPA_LIB_SUFFIX TFORMAT(".dll") ///< Macro for platform-specific lib file suffix
#define GPA_X64_ARCH_SUFFIX TFORMAT("-x64") ///< Macro for 64-bit lib file architecture suffix
#define GPA_X86_ARCH_SUFFIX TFORMAT("") ///< Macro for 32-bit lib file architecture suffix
#else
#define GPA_LIB_PREFIX TFORMAT("lib") ///< Macro for platform-specific lib file prefix
#define GPA_LIB_SUFFIX TFORMAT(".so") ///< Macro for platform-specific lib file suffix
#define GPA_X64_ARCH_SUFFIX TFORMAT("") ///< Macro for 64-bit lib file architecture suffix
#define GPA_X86_ARCH_SUFFIX TFORMAT("32") ///< Macro for 32-bit lib file architecture suffix
#endif
#define GPA_DEBUG_SUFFIX TFORMAT("-d") ///< Macro for debug suffix
#define GPA_INTERNAL_SUFFIX TFORMAT("-Internal") ///< Macro for internal build lib file suffix
#define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0])) ///< Macro to calculate array length
#define GPA_GET_FUNCTION_TABLE_FUNCTION_NAME "GPA_GetFuncTable" ///< Macro for GPA_GetFuncTable entry point
/// GPAFunctionTableInfo structure
typedef struct _GPAFuncTableInfo
{
GPA_API_Type m_gpaApiType; ///< API type
GPAFunctionTable* m_pGpaFuncTable; ///< GPA function table
LibHandle m_libHandle; ///< library handle
void* m_pNext; ///< pointer to next function table info
} GPAFuncTableInfo;
extern GPAFuncTableInfo* g_pFuncTableInfo; ///< global instance of GPA function table info
/// Replaces the Windows style path separator to Unix style
/// \param[in] pFilePath file path
/// \param[out] pLastSeparatorPosition if not null, last separator position in the path string will be returned
static inline void Win2UnixPathSeparator(LocaleChar* pFilePath, unsigned int* pLastSeparatorPosition)
{
unsigned int counter = 0;
unsigned int lastSlashPos = 0;
while ('\0' != pFilePath[counter])
{
if ('\\' == pFilePath[counter])
{
pFilePath[counter] = '/';
}
if ('/' == pFilePath[counter])
{
lastSlashPos = counter;
}
++counter;
}
if (NULL != pLastSeparatorPosition)
{
*pLastSeparatorPosition = lastSlashPos;
}
}
/// Get the current working directory
/// \return the current working directory
static const LocaleChar* GPAIL_GetWorkingDirectoryPath()
{
static LocaleChar workingDirectoryStaticString[GPA_MAX_PATH];
workingDirectoryStaticString[0] = 0;
LocaleChar tempWorkingDirectoryPath[GPA_MAX_PATH] = {0};
#ifdef _WIN32
GetModuleFileName(NULL, tempWorkingDirectoryPath, ARRAY_LENGTH(tempWorkingDirectoryPath));
#else
int len;
char tempWorkingDirectoryPathInChar[GPA_MAX_PATH] = {0};
len = readlink("/proc/self/exe", tempWorkingDirectoryPathInChar, ARRAY_LENGTH(tempWorkingDirectoryPathInChar) - 1);
if (len != -1)
{
tempWorkingDirectoryPath[len] = '\0';
}
#ifdef UNICODE
mbstowcs(tempWorkingDirectoryPath, tempWorkingDirectoryPathInChar, ARRAY_LENGTH(tempWorkingDirectoryPathInChar));
#else
MEM_CPY(tempWorkingDirectoryPath, tempWorkingDirectoryPathInChar, ARRAY_LENGTH(tempWorkingDirectoryPathInChar));
#endif
#endif
unsigned int lastSlashPos = 0;
Win2UnixPathSeparator(tempWorkingDirectoryPath, &lastSlashPos);
MEM_SET(workingDirectoryStaticString, 0, ARRAY_LENGTH(workingDirectoryStaticString));
STR_NCOPY(workingDirectoryStaticString, ARRAY_LENGTH(workingDirectoryStaticString), tempWorkingDirectoryPath, lastSlashPos);
return workingDirectoryStaticString;
}
/// Gets the library file name for the given API
/// \param[in] pApiType type of the API
/// \return library file name
static inline const LocaleChar* GPAIL_GetLibraryFileName(GPA_API_Type pApiType)
{
static LocaleChar filenameStaticString[GPA_MAX_PATH];
filenameStaticString[0] = 0;
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_LIB_PREFIX);
switch (pApiType)
{
#ifdef _WIN32
case GPA_API_DIRECTX_11:
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_DIRECTX11_LIB);
break;
case GPA_API_DIRECTX_12:
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_DIRECTX12_LIB);
break;
case GPA_API_OPENCL:
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_OPENCL_LIB);
break;
#endif
case GPA_API_OPENGL:
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_OPENGL_LIB);
break;
case GPA_API_VULKAN:
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_VULKAN_LIB);
break;
default:
MEM_SET(filenameStaticString, 0, ARRAY_LENGTH(filenameStaticString));
return filenameStaticString;
}
#ifdef GPA_IS_64_BIT
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_X64_ARCH_SUFFIX);
#else
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_X86_ARCH_SUFFIX);
#endif
#ifdef USE_DEBUG_GPA
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_DEBUG_SUFFIX);
#endif
#ifdef USE_INTERNAL_GPA
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_INTERNAL_SUFFIX);
#endif
STR_CAT(filenameStaticString, ARRAY_LENGTH(filenameStaticString), GPA_LIB_SUFFIX);
return filenameStaticString;
}
/// Get the library full path
/// \param[in] apiType type of the API
/// \param[in] pLibPath local path to the library files
/// \return library with full path
static inline const LocaleChar* GPAIL_GetLibraryFullPath(GPA_API_Type apiType, const LocaleChar* pLibPath)
{
static LocaleChar libPathStaticString[GPA_MAX_PATH];
libPathStaticString[0] = 0;
const LocaleChar* pLibName = GPAIL_GetLibraryFileName(apiType);
if (STR_LEN(pLibName, GPA_MAX_PATH) > 1)
{
LocaleChar tempLibFileName[GPA_MAX_PATH] = {0};
LocaleChar tempWorkingDirectory[GPA_MAX_PATH] = {0};
STR_COPY(tempLibFileName, ARRAY_LENGTH(tempLibFileName), pLibName);
if (NULL == pLibPath)
{
const LocaleChar* pWorkingDirectoryPath = GPAIL_GetWorkingDirectoryPath();
STR_COPY(tempWorkingDirectory, ARRAY_LENGTH(tempWorkingDirectory), pWorkingDirectoryPath);
}
else
{
STR_COPY(tempWorkingDirectory, ARRAY_LENGTH(tempWorkingDirectory), pLibPath);
Win2UnixPathSeparator(tempWorkingDirectory, NULL);
}
size_t stringLength = STR_LEN(tempWorkingDirectory, ARRAY_LENGTH(tempWorkingDirectory));
if (tempWorkingDirectory[stringLength - 1] != '/')
{
tempWorkingDirectory[stringLength] = '/';
tempWorkingDirectory[stringLength + 1] = '\0';
}
MEM_SET(libPathStaticString, 0, ARRAY_LENGTH(libPathStaticString));
STR_COPY(libPathStaticString, ARRAY_LENGTH(libPathStaticString), tempWorkingDirectory);
STR_CAT(libPathStaticString, ARRAY_LENGTH(libPathStaticString), tempLibFileName);
}
return libPathStaticString;
}
/// Loads the dll and initialize the function table for the given API
/// \param[in] apiType type of the API to be loaded
/// \param[in] pLibPath path to the folder containing dll
/// \return GPA_STATUS_OK upon successful operation
static inline GPA_Status GPAIL_LoadApi(GPA_API_Type apiType, const LocaleChar* pLibPath)
{
#if DISABLE_GPA
UNREFERENCED_PARAMETER(apiType);
UNREFERENCED_PARAMETER(pLibPath);
if (NULL == g_pFuncTableInfo)
{
g_pFuncTableInfo = (GPAFuncTableInfo*)malloc(sizeof(GPAFuncTableInfo));
g_pFuncTableInfo->m_pGpaFuncTable = (GPAFunctionTable*)malloc(sizeof(GPAFunctionTable));
}
g_pFuncTableInfo->m_gpaApiType = GPA_API__LAST;
g_pFuncTableInfo->m_libHandle = NULL;
g_pFuncTableInfo->m_pGpaFuncTable->m_majorVer = GPA_FUNCTION_TABLE_MAJOR_VERSION_NUMBER;
g_pFuncTableInfo->m_pGpaFuncTable->m_minorVer = GPA_FUNCTION_TABLE_MINOR_VERSION_NUMBER;
GPA_Status status = GPA_GetFuncTable(g_pFuncTableInfo->m_pGpaFuncTable);
g_pFuncTableInfo->m_pNext = NULL;
#else
if (NULL == g_pFuncTableInfo)
{
g_pFuncTableInfo = (GPAFuncTableInfo*)malloc(sizeof(GPAFuncTableInfo));
if (NULL != g_pFuncTableInfo)
{
g_pFuncTableInfo->m_gpaApiType = GPA_API_NO_SUPPORT;
g_pFuncTableInfo->m_libHandle = NULL;
g_pFuncTableInfo->m_pGpaFuncTable = NULL;
g_pFuncTableInfo->m_pNext = NULL;
}
}
GPA_Status status = GPA_STATUS_ERROR_FAILED;
if (NULL != g_pFuncTableInfo)
{
status = GPA_STATUS_OK;
if (apiType >= GPA_API__START && apiType < GPA_API_NO_SUPPORT)
{
const LocaleChar* pLibName = GPAIL_GetLibraryFileName(apiType);
if (NULL == pLibName)
{
return GPA_STATUS_ERROR_API_NOT_SUPPORTED;
}
{
GPAFuncTableInfo* pFunctionTableInfo = g_pFuncTableInfo;
while (NULL != pFunctionTableInfo)
{
if (pFunctionTableInfo->m_gpaApiType == apiType)
{
return GPA_STATUS_ERROR_LIB_ALREADY_LOADED;
}
pFunctionTableInfo = (GPAFuncTableInfo*)pFunctionTableInfo->m_pNext;
}
}
const LocaleChar* pLibFullPath = GPAIL_GetLibraryFullPath(apiType, pLibPath);
LibHandle libHandle = NULL;
#ifdef _WIN32
libHandle = LoadLibrary(pLibFullPath);
#else
#ifdef UNICODE
char libFullPathChar[GPA_MAX_PATH];
int ret = wcstombs(libFullPathChar, pLibFullPath, GPA_MAX_PATH);
libFullPathChar[ret] = '\0';
libHandle = dlopen(libFullPathChar, RTLD_LAZY);
#else
libHandle = dlopen(pLibFullPath, RTLD_LAZY);
#endif
#endif
GPA_GetFuncTablePtrType funcTableFn;
if (NULL != libHandle)
{
#ifdef _WIN32
funcTableFn = (GPA_GetFuncTablePtrType)(GetProcAddress(libHandle, GPA_GET_FUNCTION_TABLE_FUNCTION_NAME));
#else
funcTableFn = (GPA_GetFuncTablePtrType)(dlsym(libHandle, GPA_GET_FUNCTION_TABLE_FUNCTION_NAME));
#endif
if (NULL != funcTableFn)
{
GPAFunctionTable* pGPAFuncTable = (GPAFunctionTable*)malloc(sizeof(GPAFunctionTable));
if (NULL == pGPAFuncTable)
{
return GPA_STATUS_ERROR_FAILED;
}
pGPAFuncTable->m_majorVer = GPA_FUNCTION_TABLE_MAJOR_VERSION_NUMBER;
pGPAFuncTable->m_minorVer = GPA_FUNCTION_TABLE_MINOR_VERSION_NUMBER;
status = funcTableFn((void*)(pGPAFuncTable));
if (GPA_STATUS_OK == status)
{
if (NULL == g_pFuncTableInfo->m_pGpaFuncTable)
{
g_pFuncTableInfo->m_gpaApiType = apiType;
g_pFuncTableInfo->m_pGpaFuncTable = pGPAFuncTable;
g_pFuncTableInfo->m_libHandle = libHandle;
g_pFuncTableInfo->m_pNext = NULL;
}
else
{
GPAFuncTableInfo* pNewTableInfo = (GPAFuncTableInfo*)malloc(sizeof(GPAFuncTableInfo));
if (NULL == pNewTableInfo)
{
return GPA_STATUS_ERROR_FAILED;
}
pNewTableInfo->m_gpaApiType = apiType;
pNewTableInfo->m_pGpaFuncTable = pGPAFuncTable;
pNewTableInfo->m_libHandle = libHandle;
pNewTableInfo->m_pNext = NULL;
GPAFuncTableInfo* pFunctionTableInfo = g_pFuncTableInfo;
while (NULL != pFunctionTableInfo->m_pNext)
{
pFunctionTableInfo = (GPAFuncTableInfo*)pFunctionTableInfo->m_pNext;
}
pFunctionTableInfo->m_pNext = pNewTableInfo;
}
}
else
{
free(pGPAFuncTable);
}
}
else
{
status = GPA_STATUS_ERROR_LIB_LOAD_FAILED;
}
}
else
{
status = GPA_STATUS_ERROR_LIB_LOAD_FAILED;
}
}
else
{
status = GPA_STATUS_ERROR_API_NOT_SUPPORTED;
}
}
#endif
return status;
}
/// Get the function table for the given API
/// \param[in] apiType API type
/// \return returns pointer to the API function table if loaded otherwise null pointer
static inline const GPAFunctionTable* GPAIL_GetFunctionTable(GPA_API_Type apiType)
{
#if DISABLE_GPA
UNREFERENCED_PARAMETER(apiType);
return g_pFuncTableInfo->m_pGpaFuncTable;
#else
GPAFuncTableInfo* pFunctionTableInfo = g_pFuncTableInfo;
GPAFunctionTable* pRetFuncTable = NULL;
while (NULL != pFunctionTableInfo)
{
if (pFunctionTableInfo->m_gpaApiType == apiType)
{
pRetFuncTable = pFunctionTableInfo->m_pGpaFuncTable;
break;
}
pFunctionTableInfo = (GPAFuncTableInfo*)pFunctionTableInfo->m_pNext;
}
return pRetFuncTable;
#endif
}
/// Unloads the function table for the given API
/// \param[in] apiType API type
/// \return GPA_STATUS_OK upon successful operation
static inline GPA_Status GPAIL_UnLoadApi(GPA_API_Type apiType)
{
#if DISABLE_GPA
UNREFERENCED_PARAMETER(apiType);
return GPA_STATUS_OK;
#else
GPA_Status status = GPA_STATUS_ERROR_FAILED;
GPAFuncTableInfo* pFunctionTableInfo = g_pFuncTableInfo;
while (NULL != pFunctionTableInfo)
{
if (pFunctionTableInfo->m_gpaApiType == apiType)
{
free(pFunctionTableInfo->m_pGpaFuncTable);
LibHandle libHandle = pFunctionTableInfo->m_libHandle;
if (NULL != libHandle)
{
#ifdef _WIN32
FreeLibrary(libHandle);
#else
dlclose(libHandle);
#endif
pFunctionTableInfo->m_libHandle = NULL;
pFunctionTableInfo->m_pGpaFuncTable = NULL;
status = GPA_STATUS_OK;
break;
}
}
pFunctionTableInfo = (GPAFuncTableInfo*)pFunctionTableInfo->m_pNext;
}
return status;
#endif
}
/// Clears the loader
static inline void GPAIL_ClearLoader()
{
#if DISABLE_GPA
g_pFuncTableInfo->m_gpaApiType = GPA_API_NO_SUPPORT;
g_pFuncTableInfo->m_libHandle = NULL;
g_pFuncTableInfo->m_pNext = NULL;
g_pFuncTableInfo->m_pGpaFuncTable = NULL;
#else
if (NULL != g_pFuncTableInfo)
{
while (NULL != g_pFuncTableInfo->m_pNext)
{
GPAFuncTableInfo* pFunctionTableInfo = (GPAFuncTableInfo*)(g_pFuncTableInfo->m_pNext);
if (NULL != pFunctionTableInfo->m_libHandle)
{
#ifdef _WIN32
FreeLibrary(pFunctionTableInfo->m_libHandle);
#else
dlclose(pFunctionTableInfo->m_libHandle);
#endif
pFunctionTableInfo->m_libHandle = NULL;
}
if (NULL != pFunctionTableInfo->m_pGpaFuncTable)
{
free(pFunctionTableInfo->m_pGpaFuncTable);
pFunctionTableInfo->m_pGpaFuncTable = NULL;
}
if (NULL != pFunctionTableInfo->m_pNext)
{
g_pFuncTableInfo->m_pNext = pFunctionTableInfo->m_pNext;
free(pFunctionTableInfo);
}
else
{
g_pFuncTableInfo->m_pNext = NULL;
}
}
if (NULL != g_pFuncTableInfo->m_libHandle)
{
#ifdef _WIN32
FreeLibrary(g_pFuncTableInfo->m_libHandle);
#else
dlclose(g_pFuncTableInfo->m_libHandle);
#endif
g_pFuncTableInfo->m_libHandle = NULL;
}
if (NULL != g_pFuncTableInfo->m_pGpaFuncTable)
{
free(g_pFuncTableInfo->m_pGpaFuncTable);
g_pFuncTableInfo->m_pGpaFuncTable = NULL;
}
g_pFuncTableInfo->m_gpaApiType = GPA_API_NO_SUPPORT;
free(g_pFuncTableInfo);
g_pFuncTableInfo = NULL;
}
#endif
}
#ifdef __cplusplus
/// Singleton Class to handle the loading and unloading the possible APIs
class GPAApiManager
{
public:
/// Returns the instance of the GPAApiManger
/// \return returns the instance of the GPAApiManager
static GPAApiManager* Instance()
{
if (nullptr == m_pGpaApiManager)
{
m_pGpaApiManager = new (std::nothrow) GPAApiManager();
}
return m_pGpaApiManager;
}
/// Deletes the static instance instance
static void DeleteInstance()
{
if (nullptr != m_pGpaApiManager)
{
delete m_pGpaApiManager;
m_pGpaApiManager = nullptr;
}
}
/// Loads the dll and initialize the function table for the passed API type
/// \param[in] apiType type of the API to be loaded
/// \param[in] libPath path to the folder containing dll
/// \return returns appropriate status of the operation
GPA_Status LoadApi(const GPA_API_Type& apiType, const LocaleString libPath = LocaleString()) const
{
LocaleChar libPathAsChar[GPA_MAX_PATH] = {0};
bool localPathGiven = false;
if (!libPath.empty())
{
STR_COPY(libPathAsChar, ARRAY_LENGTH(libPathAsChar), libPath.c_str());
localPathGiven = true;
}
return GPAIL_LoadApi(apiType, localPathGiven ? libPathAsChar : NULL);
}
/// Unloads the function table for the passed API
/// \param[in] apiType API type
void UnloadApi(const GPA_API_Type& apiType) const
{
GPAIL_UnLoadApi(apiType);
}
/// Get the function table for the passed API
/// \param[in] apiType API type
/// \return returns pointer to the API function table if loaded otherwise null pointer
GPAFunctionTable* GetFunctionTable(const GPA_API_Type& apiType) const
{
return const_cast<GPAFunctionTable*>(GPAIL_GetFunctionTable(apiType));
}
/// Gets the library file name for the given API
/// \param[in] apiType type of the API
/// \return library file name string
LocaleString GetLibraryFileName(const GPA_API_Type& apiType) const
{
return LocaleString(GPAIL_GetLibraryFileName(apiType));
}
/// Get the library full path
/// \param[in] apiType type of the API
/// \param[in,opt] libPath local path to the library files
/// \return library with full path string
LocaleString GetLibraryFullPath(const GPA_API_Type& apiType, const LocaleString libPath = LocaleString()) const
{
LocaleChar libPathAsChar[GPA_MAX_PATH] = {0};
bool localPathGiven = false;
if (!libPath.empty())
{
STR_COPY(libPathAsChar, ARRAY_LENGTH(libPathAsChar), libPath.c_str());
localPathGiven = true;
}
return LocaleString(GPAIL_GetLibraryFullPath(apiType, localPathGiven ? libPathAsChar : NULL));
}
private:
/// Constructor
GPAApiManager() = default;
/// Destructor
~GPAApiManager()
{
GPAIL_ClearLoader();
}
static GPAApiManager* m_pGpaApiManager; ///< GPA Api Manager pointer
};
#endif //__cplusplus
#endif