-
Notifications
You must be signed in to change notification settings - Fork 851
Expand file tree
/
Copy pathGlobal.h
More file actions
379 lines (330 loc) · 17.6 KB
/
Global.h
File metadata and controls
379 lines (330 loc) · 17.6 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
///////////////////////////////////////////////////////////////////////////////
// //
// Global.h //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Provides important declarations global to all DX Compiler code. //
// //
///////////////////////////////////////////////////////////////////////////////
#pragma once
#ifdef _WIN32
// Redeclare some macros to not depend on winerror.h
#define DXC_FAILED(hr) (((HRESULT)(hr)) < 0)
#ifndef _HRESULT_DEFINED
#define _HRESULT_DEFINED
#ifndef _Return_type_success_
typedef long HRESULT;
#else
typedef _Return_type_success_(return >= 0) long HRESULT;
#endif // _Return_type_success_
#endif // !_HRESULT_DEFINED
#endif // _WIN32
#include "dxc/Support/exception.h"
#include "dxc/WinAdapter.h"
#include <stdarg.h>
#include <system_error>
///////////////////////////////////////////////////////////////////////////////
// Memory allocation support.
//
// This mechanism ties into the C++ new and delete operators.
//
// Other allocators may be used in specific situations, eg sub-allocators or
// the COM allocator for interop. This is the preferred allocator in general,
// however, as it eventually allows the library user to specify their own.
//
struct IMalloc;
// Used by DllMain to set up and tear down per-thread tracking.
HRESULT DxcInitThreadMalloc() throw();
void DxcCleanupThreadMalloc() throw();
// Used by APIs entry points to set up per-thread/invocation allocator.
// Setting the IMalloc on the thread increases the reference count,
// clearing it decreases it.
void DxcSetThreadMallocToDefault() throw();
void DxcClearThreadMalloc() throw();
// Used to retrieve the current invocation's allocator or perform an
// alloc/free/realloc.
IMalloc *DxcGetThreadMallocNoRef() throw();
// Common implementation of operators new and delete
void *DxcNew(std::size_t size) throw();
void DxcDelete(void *ptr) throw();
class DxcThreadMalloc {
public:
explicit DxcThreadMalloc(IMalloc *pMallocOrNull) throw();
~DxcThreadMalloc();
IMalloc *GetInstalledAllocator() const { return p; }
private:
// Copy constructor and assignment are dangerous and should always be
// deleted...
DxcThreadMalloc(const DxcThreadMalloc &) = delete;
DxcThreadMalloc &operator=(const DxcThreadMalloc &) = delete;
// Move constructor and assignment should be OK to be added if needed.
DxcThreadMalloc(DxcThreadMalloc &&) = delete;
DxcThreadMalloc &operator=(DxcThreadMalloc &&) = delete;
IMalloc *p;
IMalloc *pPrior;
};
///////////////////////////////////////////////////////////////////////////////
// Error handling support.
void CheckLLVMErrorCode(const std::error_code &ec);
/******************************************************************************
Project-wide macros
******************************************************************************/
#define SAFE_RELEASE(p) \
{ \
if (p) { \
(p)->Release(); \
(p) = nullptr; \
} \
}
#define SAFE_ADDREF(p) \
{ \
if (p) { \
(p)->AddRef(); \
} \
}
#define SAFE_DELETE_ARRAY(p) \
{ \
delete[](p); \
p = nullptr; \
}
#define SAFE_DELETE(p) \
{ \
delete (p); \
p = nullptr; \
}
// VH is used in other DXC projects, but it's also a typedef in llvm.
// Use the IFC (IfFailedCleanup) set of conventions.
#define IFC(x) \
{ \
hr = (x); \
if (DXC_FAILED(hr)) \
goto Cleanup; \
}
#define IFR(x) \
{ \
HRESULT __hr = (x); \
if (DXC_FAILED(__hr)) \
return __hr; \
}
#define IFRBOOL(x, y) \
{ \
if (!(x)) \
return (y); \
}
#define IFCBOOL(x, y) \
{ \
if (!(x)) { \
hr = (y); \
goto Cleanup; \
} \
}
#define IFCOOM(x) \
{ \
if (nullptr == (x)) { \
hr = E_OUTOFMEMORY; \
goto Cleanup; \
} \
}
#define IFROOM(x) \
{ \
if (nullptr == (x)) { \
return E_OUTOFMEMORY; \
} \
}
#define IFCPTR(x) \
{ \
if (nullptr == (x)) { \
hr = E_POINTER; \
goto Cleanup; \
} \
}
#define IFT(x) \
{ \
HRESULT __hr = (x); \
if (DXC_FAILED(__hr)) \
throw ::hlsl::Exception(__hr); \
}
#define IFTBOOL(x, y) \
{ \
if (!(x)) \
throw ::hlsl::Exception(y); \
}
#define IFTOOM(x) \
{ \
if (nullptr == (x)) { \
throw ::hlsl::Exception(E_OUTOFMEMORY); \
} \
}
#define IFTPTR(x) \
{ \
if (nullptr == (x)) { \
throw ::hlsl::Exception(E_POINTER); \
} \
}
#define IFTARG(x) \
{ \
if (!(x)) { \
throw ::hlsl::Exception(E_INVALIDARG); \
} \
}
#define IFTLLVM(x) \
{ CheckLLVMErrorCode(x); }
#define IFTMSG(x, msg) \
{ \
HRESULT __hr = (x); \
if (DXC_FAILED(__hr)) \
throw ::hlsl::Exception(__hr, msg); \
}
#define IFTBOOLMSG(x, y, msg) \
{ \
if (!(x)) \
throw ::hlsl::Exception(y, msg); \
}
// Propagate an C++ exception into an HRESULT.
#define CATCH_CPP_ASSIGN_HRESULT() \
catch (std::bad_alloc &) { \
hr = E_OUTOFMEMORY; \
} \
catch (hlsl::Exception & _hlsl_exception_) { \
hr = _hlsl_exception_.hr; \
} \
catch (...) { \
hr = E_FAIL; \
}
#define CATCH_CPP_RETURN_HRESULT() \
catch (std::bad_alloc &) { \
return E_OUTOFMEMORY; \
} \
catch (hlsl::Exception & _hlsl_exception_) { \
return _hlsl_exception_.hr; \
} \
catch (...) { \
return E_FAIL; \
}
template <typename T> T *VerifyNullAndThrow(T *p) {
if (p == nullptr)
throw std::bad_alloc();
return p;
}
#define VNT(__p) VerifyNullAndThrow(__p)
#ifdef _WIN32
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(
const char *msg);
inline void OutputDebugBytes(const void *ptr, size_t len) {
const char digits[] = "0123456789abcdef";
const unsigned char *pBytes = (const unsigned char *)ptr;
constexpr size_t bytesPerLine = 16;
constexpr size_t bufferSize = bytesPerLine * 3 + 2 + 1;
char buffer[bufferSize];
buffer[bufferSize - 3] = '\r';
buffer[bufferSize - 2] = '\n';
buffer[bufferSize - 1] = '\0';
char *pWrite = buffer;
char *pEnd = buffer + bufferSize - 3;
while (len) {
*pWrite++ = digits[(*pBytes & 0xF0) >> 4];
*pWrite++ = digits[*pBytes & 0x0f];
*pWrite++ = ' ';
if (pWrite == pEnd) {
OutputDebugStringA(buffer);
pWrite = buffer;
}
--len;
++pBytes;
}
if (pWrite != buffer) {
*pWrite = '\0';
OutputDebugStringA(buffer);
OutputDebugStringA("\r\n");
}
}
inline void OutputDebugFormatA(const char *pszFormat, ...) {
constexpr size_t bufferSize = 1024;
char buffer[bufferSize];
va_list argList;
va_start(argList, pszFormat);
int count = vsnprintf_s(buffer, bufferSize, bufferSize, pszFormat, argList);
va_end(argList);
OutputDebugStringA(buffer);
if (count < 0) {
OutputDebugStringA("...\n");
}
}
#endif // _WIN32
#ifndef NDEBUG
#ifdef _WIN32
// DXASSERT is used to debug break when 'exp' evaluates to false and is only
// intended for internal developer use. It is compiled out in free
// builds. This means that code that should be in the final exe should
// NOT be inside of of an DXASSERT test.
//
// 'fmt' is a printf-like format string; positional arguments aren't
// supported.
//
// Example: DXASSERT(i > 10, "Hello %s", "World");
// This prints 'Hello World (i > 10)' and breaks in the debugger if the
// assertion doesn't hold.
//
#define DXASSERT_ARGS(exp, fmt, ...) \
do { \
if (!(exp)) { \
OutputDebugFormatA( \
"Error: \t%s\nFile:\n%s(%d)\nFunc:\t%s.\n\t" fmt "\n", \
"!(" #exp ")", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
__debugbreak(); \
} \
} while (0)
#define DXASSERT(exp, msg) DXASSERT_ARGS(exp, msg)
#define DXASSERT_LOCALVAR(local, exp, msg) DXASSERT(exp, msg)
#define DXASSERT_LOCALVAR_NOMSG(local, exp) DXASSERT_LOCALVAR(local, exp, "")
#define DXASSERT_NOMSG(exp) DXASSERT(exp, "")
#define DXVERIFY_NOMSG(exp) DXASSERT(exp, "")
#else // _WIN32
#include <cassert>
#define DXASSERT_NOMSG assert
#define DXASSERT_LOCALVAR(local, exp, msg) DXASSERT(exp, msg)
#define DXASSERT_LOCALVAR_NOMSG(local, exp) DXASSERT_LOCALVAR(local, exp, "")
#define DXVERIFY_NOMSG assert
#define DXASSERT_ARGS(expr, fmt, ...) \
do { \
if (!(expr)) { \
fprintf(stderr, fmt, __VA_ARGS__); \
assert(false); \
} \
} while (0)
#define DXASSERT(expr, msg) \
do { \
if (!(expr)) { \
fprintf(stderr, msg); \
assert(false && msg); \
} \
} while (0)
#endif // _WIN32
#else // NDEBUG
// DXASSERT_ARGS is disabled in free builds.
#define DXASSERT_ARGS(exp, s, ...) \
do { \
} while (0)
// DXASSERT is disabled in free builds.
#define DXASSERT(exp, msg) \
do { \
} while (0)
// DXASSERT_LOCALVAR is disabled in free builds, but we keep the local
// referenced to avoid a warning.
#define DXASSERT_LOCALVAR(local, exp, msg) \
do { \
(void)(local); \
} while (0)
#define DXASSERT_LOCALVAR_NOMSG(local, exp) DXASSERT_LOCALVAR(local, exp, "")
// DXASSERT_NOMSG is disabled in free builds.
#define DXASSERT_NOMSG(exp) \
do { \
} while (0)
// DXVERIFY is patterned after NT_VERIFY and will evaluate the expression
#define DXVERIFY_NOMSG(exp) \
do { \
(void)(exp); \
} while (0)
#endif // NDEBUG