1 /*
2  *
3  * Copyright (C) 2019-2021 Intel Corporation
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  */
8 
9 #if defined(__cplusplus)
10 #pragma once
11 #endif
12 #include <stdlib.h>
13 #include <string.h>
14 #include <string>
15 
16 ///////////////////////////////////////////////////////////////////////////////
17 #if defined(_WIN32)
18 #  include <Windows.h>
19 #  define MAKE_LIBRARY_NAME(NAME, VERSION)    NAME".dll"
20 #  define MAKE_LAYER_NAME(NAME)    NAME".dll"
21 #  define LOAD_DRIVER_LIBRARY(NAME) LoadLibraryExA(NAME, nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)
22 #  define FREE_DRIVER_LIBRARY(LIB)  if(LIB) FreeLibrary(LIB)
23 #  define GET_FUNCTION_PTR(LIB, FUNC_NAME) GetProcAddress(LIB, FUNC_NAME)
24 #else
25 #  include <dlfcn.h>
26 #  define HMODULE void*
27 #  define MAKE_LIBRARY_NAME(NAME, VERSION)    "lib" NAME ".so." VERSION
28 #  define MAKE_LAYER_NAME(NAME)    "lib" NAME ".so." L0_VALIDATION_LAYER_SUPPORTED_VERSION
29 #  ifdef RTLD_DEEPBIND
30 #  define LOAD_DRIVER_LIBRARY(NAME) dlopen(NAME, RTLD_LAZY|RTLD_LOCAL|RTLD_DEEPBIND)
31 #  else
32 #  define LOAD_DRIVER_LIBRARY(NAME) dlopen(NAME, RTLD_LAZY|RTLD_LOCAL)
33 #  endif
34 #  define FREE_DRIVER_LIBRARY(LIB)  if(LIB) dlclose(LIB)
35 #  define GET_FUNCTION_PTR(LIB, FUNC_NAME) dlsym(LIB, FUNC_NAME)
36 #endif
37 
create_library_path(const char * name,const char * path)38 inline std::string create_library_path(const char *name, const char *path){
39     std::string library_path;
40     if (path && (strcmp("", path) != 0)) {
41         library_path.assign(path);
42 #ifdef _WIN32
43         library_path.append("\\");
44 #else
45         library_path.append("/");
46 #endif
47         library_path.append(name);
48     } else {
49         library_path.assign(name);
50     }
51     return library_path;
52 }
53 
54 #ifdef _WIN32
readLevelZeroLoaderLibraryPath()55 inline std::string readLevelZeroLoaderLibraryPath() {
56     std::string LoaderRegKeyPath = "";
57     HKEY regKey = {};
58     DWORD regValueType = {};
59     DWORD pathSize = {};
60     std::string loaderMajorVersionString = std::to_string(LOADER_VERSION_MAJOR);
61     std::string loaderRegistryKeyPath = "Software\\Intel\\oneAPI\\LevelZero\\";
62     loaderRegistryKeyPath.append(loaderMajorVersionString);
63     static constexpr char levelZeroLoaderPathKey[] = "LevelZeroLoaderPath";
64 
65     LSTATUS regOpenStatus = RegOpenKeyA(HKEY_LOCAL_MACHINE, loaderRegistryKeyPath.c_str(), &regKey);
66 
67     if (ERROR_SUCCESS != regOpenStatus) {
68         return LoaderRegKeyPath;
69     }
70 
71     LSTATUS regOpStatus = RegQueryValueExA(regKey, levelZeroLoaderPathKey, NULL,
72                                            &regValueType, NULL, &pathSize);
73 
74     if ((ERROR_SUCCESS == regOpStatus) && (REG_SZ == regValueType)) {
75         LoaderRegKeyPath.resize(pathSize);
76         regOpStatus = RegQueryValueExA(regKey, levelZeroLoaderPathKey, NULL,
77                                        &regValueType, (LPBYTE) & *LoaderRegKeyPath.begin(),
78                                        &pathSize);
79         if (ERROR_SUCCESS != regOpStatus) {
80             LoaderRegKeyPath.clear();
81             LoaderRegKeyPath.assign("");
82         }
83     }
84 
85     return LoaderRegKeyPath;
86 }
87 #endif
88 
89 //////////////////////////////////////////////////////////////////////////
90 #if !defined(_WIN32) && (__GNUC__ >= 4)
91 #define __zedlllocal  __attribute__ ((visibility ("hidden")))
92 #else
93 #define __zedlllocal
94 #endif
95 
96 ///////////////////////////////////////////////////////////////////////////////
97 #if ZE_ENABLE_OCL_INTEROP
98 typedef struct _cl_mem* cl_mem;
99 typedef struct _cl_command_queue* cl_command_queue;
100 typedef struct _cl_context* cl_context;
101 typedef struct _cl_program* cl_program;
102 #endif
103 
104 ///////////////////////////////////////////////////////////////////////////////
getenv_tobool(const char * name)105 inline bool getenv_tobool( const char* name )
106 {
107     const char* env = nullptr;
108 
109 #if defined(_WIN32)
110     char buffer[8];
111     auto rc = GetEnvironmentVariable(name, buffer, 8);
112     if (0 != rc && rc <= 8) {
113         env = buffer;
114     }
115 #else
116     env = getenv(name);
117 #endif
118 
119     if( ( nullptr == env ) || ( 0 == strcmp( "0", env ) ) )
120         return false;
121     return ( 0 == strcmp( "1", env ) );
122 }
123