Debian Codegraph @

IGC/OCLFE/igd_fcl_mcl/headers/RegistryAccess.h

raw symbols SCIP
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2017-2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 /*****************************************************************************\
10 Abstract: Provides all the registry reading/writing code for Intel
11 registry keys.
12 \*****************************************************************************/
13
14 #pragma once
15
16 #if defined(_DEBUG) || defined(_INTERNAL)
17
18 #if defined _WIN32
19 #define OCLRT_REGKEY "Software\\Intel\\IGFX\\OCL"
20
21 // Prevent windows.h from defining max macro; its definition conflicts with MathExtras.h's usage.
22 #ifndef NOMINMAX
23 #define NOMINMAX
24 #endif
25 #include <windows.h>
26
27 #include <windef.h>
28 #include <stdio.h>
29 #else
30 #include <string.h>
31 #endif
32
33 #include <stdlib.h>
34
35 namespace llvm {
36 // TO REMOVE
37 // typedef unsigned char byte;
38
39 /*****************************************************************************\
40 Windows
41
42 Inline Function: ReadRegistry
43
44 Description: Reads and returns a DWORD value from the key path specified.
45 If the key path cannot be found or cannot be opened the default
46 value supplied will be returned.
47
48 \*****************************************************************************/
49 #if defined _WIN32
50 inline DWORD ReadRegistry(const char *pKeyName, const DWORD defaultValue) {
51 HKEY Key;
52 DWORD value = defaultValue;
53 DWORD success = ERROR_SUCCESS;
54
55 success = RegOpenKeyExA(HKEY_LOCAL_MACHINE, OCLRT_REGKEY, 0, KEY_READ, &Key);
56
57 if (ERROR_SUCCESS == success) {
58 DWORD regType;
59 DWORD size = sizeof(ULONG);
60
61 success = RegQueryValueExA(Key, pKeyName, NULL, &regType, (unsigned char *)&value, &size);
62
63 // close key
64 RegCloseKey(Key);
65 }
66
67 return value;
68 }
69
70 #else
71
72 /*****************************************************************************\
73 Unix Based Systems
74
75 Inline Function: ReadRegistry
76
77 Description: Reads and returns a uint32_t value from the environment variable
78 specified. If the variable is not defined then the value
79 supplied will be returned.
80
81 \*****************************************************************************/
82 inline uint32_t ReadRegistry(const char *pKeyName, const uint32_t defaultValue) {
83 #ifdef ANDROID
84 const char *envVal = getenv(pKeyName);
85 #else
86 std::string pKey = "OCL_";
87 pKey += pKeyName;
88 const char *envVal = getenv(pKey.c_str());
89 #endif
90 uint32_t value = defaultValue;
91
92 if (envVal)
93 value = (uint32_t)atoi(envVal);
94
95 return value;
96 }
97 #endif
98
99 #ifdef _WIN32
100 /*****************************************************************************\
101 Windows
102
103 Inline Function: ReadRegistryBinary
104
105 Description: Reads and returns a value from the key path specified.
106 If the key path cannot be found or cannot be opened the default
107 value supplied will be returned.
108
109 Use this for reading non-DWORD key values.
110
111 false is returned only if the supplied buffer is too small
112 or NULL pointers were supplied.
113
114 \*****************************************************************************/
115 inline bool ReadRegistryBinary(const char *pKeyName, DWORD regType, void *&pData, DWORD *pDataSize) {
116 HKEY Key;
117 DWORD success = ERROR_SUCCESS;
118
119 if (!pKeyName || !pData || !pDataSize)
120 return false;
121
122 success = RegOpenKeyExA(HKEY_LOCAL_MACHINE, OCLRT_REGKEY, 0, KEY_READ, &Key);
123
124 if (ERROR_SUCCESS == success) {
125 success = RegQueryValueExA(Key, pKeyName, NULL, &regType, (BYTE *)pData, pDataSize);
126
127 // close key
128 RegCloseKey(Key);
129
130 // If supplied buffer was too small, it is now undefined and shouldn't be used
131 if (ERROR_MORE_DATA == success)
132 return false;
133 }
134
135 return true; /* Also returns TRUE when key is not found */
136 }
137
138 #else
139
140 /*****************************************************************************\
141 Unix Based Systems
142
143 Inline Function: ReadRegistryBinary
144
145 Description: Reads and returns a value from the environment variable specified.
146 If the key is not present the default value supplied will be
147 returned.
148
149 Use this for reading NULL-terminated key values.
150
151 Returns false if supplied buffer is too small to receive data or
152 NULL pointers were provided. Returns true when caller is safe to
153 use data stored in pData.
154
155 \*****************************************************************************/
156 inline bool ReadRegistryBinary(const char *pKeyName, uint32_t regType, void *&pData, uint32_t *pDataSize) {
157 bool rvalue = true;
158
159 if (!pData || !pKeyName || !pDataSize)
160 return false;
161
162 #ifdef ANDROID
163 const char *envVal = getenv(pKeyName);
164 #else
165 std::string pKey = "OCL_";
166 pKey += pKeyName;
167 const char *envVal = getenv(pKey.c_str());
168 #endif
169
170 if (envVal) {
171 if (*pDataSize <= strlen((const char *)pData)) {
172 *pDataSize = 0;
173 rvalue = false;
174 } else {
175 strncpy((char *)pData, envVal, *pDataSize);
176 }
177 }
178
179 return rvalue; /* Also returns TRUE when variable is not found */
180 }
181 #endif
182
183 #if defined _WIN32
184 /*****************************************************************************\
185
186 Inline Function: WriteRegistry
187
188 Description: Writes a DWORD to the specified key path. Returns "true" if
189 successful and "false" if the key cannot be written.
190
191 \*****************************************************************************/
192 inline bool WriteRegistry(const char *pKeyName, const unsigned long value) {
193 HKEY Key;
194
195 if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE, OCLRT_REGKEY, 0, KEY_WRITE, &Key)) {
196 // ULONG data = 0;
197 DWORD size = sizeof(ULONG);
198
199 RegSetValueExA(Key, pKeyName, 0, REG_DWORD, (unsigned char *)&value, size);
200
201 RegCloseKey(Key);
202
203 return true;
204 }
205
206 return false;
207 }
208 #endif
209
210 } // namespace llvm
211
212 #endif