ReactOS 0.4.16-dev-980-g00983aa
internal_shared.h
Go to the documentation of this file.
1//
2// internal_shared.h
3//
4// Copyright (c) 2024 Timo Kreuzer
5//
6// Header for definitions shared between vcruntime and UCRT.
7//
8// SPDX-License-Identifier: MIT
9//
10
11#pragma once
12
13#include <corecrt_startup.h>
14#include <crtdbg.h>
15#include <windef.h>
16#include <winbase.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif // __cplusplus
21
22#if defined(_MSC_VER)
23#define _CRTALLOC(x) __declspec(allocate(x))
24#else
25#define _CRTALLOC(x) __attribute__((section(x)))
26#endif
27
28#ifdef _MSC_VER
29#pragma section(".CRT$XIC", long, read)
30#pragma section(".CRT$XPX", long, read)
31#pragma section(".CRT$XPXA", long, read)
32
33#pragma section(".CRT$XIA", long, read)
34#pragma section(".CRT$XIAA", long, read) // CRT startup
35#pragma section(".CRT$XIZ", long, read)
36#pragma section(".CRT$XCA", long, read)
37#pragma section(".CRT$XCZ", long, read)
38#pragma section(".CRT$XPA", long, read)
39#pragma section(".CRT$XPZ", long, read)
40#pragma section(".CRT$XTA", long, read)
41#pragma section(".CRT$XTZ", long, read)
42
43
44#pragma section(".CRT$XDA",long,read)
45#pragma section(".CRT$XDC",long,read)
46#pragma section(".CRT$XDZ",long,read)
47
48#pragma section(".CRT$XLA",long,read) // TLS callback start
49#pragma section(".CRT$XLC",long,read) // TLS constructors
50#pragma section(".CRT$XLD",long,read) // TLS destructors
51#pragma section(".CRT$XLZ",long,read) // TLS callback end
52
53#pragma section(".tls",long,read,write)
54#pragma section(".tls$AAA",long,read,write)
55#pragma section(".tls$ZZZ",long,read,write)
56#endif
57
58extern _PIFV __xi_a[];
59extern _PIFV __xi_z[];
60extern _PVFV __xc_a[];
61extern _PVFV __xc_z[];
62extern _PVFV __xp_a[];
63extern _PVFV __xp_z[];
64extern _PVFV __xt_a[];
65extern _PVFV __xt_z[];
66
67extern char __ImageBase;
68
70
71#define CRT_WARNING_DISABLE_PUSH(wn, message) \
72 __pragma(warning(push)) \
73 __pragma(warning(disable: wn))
74
75#define CRT_WARNING_POP \
76 __pragma(warning(pop))
77
78#define _BEGIN_SECURE_CRT_DEPRECATION_DISABLE \
79 __pragma(warning(push)) \
80 __pragma(warning(disable: 4996))
81
82#define _END_SECURE_CRT_DEPRECATION_DISABLE \
83 __pragma(warning(pop))
84
85#define _CRT_DEBUGGER_IGNORE -1
86#define _CRT_DEBUGGER_GSFAILURE 1
87#define _CRT_DEBUGGER_INVALIDPARAMETER 2
88#define _CRT_DEBUGGER_ABORT 3
89
90#define _CRT_DEBUGGER_HOOK(x)
91
92#define _CRT_SECURITYCRITICAL_ATTRIBUTE
93#define _CRT_SECURITYSAFECRITICAL_ATTRIBUTE
94
95//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96//
97// Parameter validation macros
98// Partly duplicated in corecrt_internal_strtox.h
99//
100//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101
102#ifdef _DEBUG
103 #define _INVALID_PARAMETER(expr) _invalid_parameter(expr, __FUNCTIONW__, __FILEW__, __LINE__, 0)
104#else
105 #define _INVALID_PARAMETER(expr) _invalid_parameter_noinfo()
106#endif
107
108#define _VALIDATE_RETURN(expr, errorcode, retexpr) \
109 { \
110 int _Expr_val = !!(expr); \
111 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
112 if (!(_Expr_val)) \
113 { \
114 *_errno() = (errorcode); \
115 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \
116 return (retexpr); \
117 } \
118 }
119
120#define _VALIDATE_RETURN_VOID(expr, errorcode) \
121 { \
122 int _Expr_val = !!(expr); \
123 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
124 if (!(_Expr_val)) \
125 { \
126 *_errno() = (errorcode); \
127 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \
128 return; \
129 } \
130 }
131
132#define _VALIDATE_RETURN_NOERRNO(expr, retexpr) \
133 { \
134 int _Expr_val = !!(expr); \
135 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
136 if (!(_Expr_val)) \
137 { \
138 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \
139 return (retexpr); \
140 } \
141 }
142
143#define _VALIDATE_RETURN_ERRCODE(expr, errorcode) \
144 _VALIDATE_RETURN(expr, errorcode, errorcode)
145
146#define _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, retexpr) \
147 { \
148 int _Expr_val = !!(expr); \
149 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
150 if (!(_Expr_val)) \
151 { \
152 *__doserrno() = 0L; \
153 *_errno() = errorcode; \
154 _INVALID_PARAMETER(_CRT_WIDE(#expr) ); \
155 return (retexpr); \
156 } \
157 }
158
159#define _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(expr, errorcode) \
160 _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, errorcode)
161
162#define _VALIDATE_RETURN_NOEXC(expr, errorcode, retexpr) \
163 { \
164 if (!(expr)) \
165 { \
166 *_errno() = errorcode; \
167 return (retexpr); \
168 } \
169 }
170
171#define _VALIDATE_RETURN_ERRCODE_NOEXC(expr, errorcode) \
172 _VALIDATE_RETURN_NOEXC(expr, errorcode, errorcode)
173
174#define _malloc_crt malloc
175#define _free_crt free
176#define _calloc_crt calloc
177#define _recalloc_crt _recalloc
178#define _malloca_crt _malloca
179#define _freea_crt _freea
180
181#ifdef __cplusplus
182} // extern "C"
183
184template<typename T>
185__forceinline
186T __crt_fast_encode_pointer(T ptr)
187{
188 union { T Ptr; uintptr_t Uint; } u = { ptr };
189 u.Uint ^= __security_cookie;
190 return u.Ptr;
191}
192
193template<typename T>
194__forceinline
195T __crt_fast_decode_pointer(T ptr)
196{
197 union { T Ptr; uintptr_t Uint; } u = { ptr };
198 u.Uint ^= __security_cookie;
199 return u.Ptr;
200}
201
202template<typename T>
203T __crt_interlocked_read(const T volatile* ptr)
204{
205 return *ptr;
206}
207
208template<typename T>
209T __crt_interlocked_read_pointer(const T volatile* ptr)
210{
211 return *ptr;
212}
213
214template<typename T>
215T __crt_interlocked_exchange_pointer(T *ptr, void* value)
216{
217 return (T)_InterlockedExchangePointer((void* volatile*)ptr, value);
218}
219
220template<typename T>
221struct __crt_seh_guarded_call
222{
223 template<typename Init, typename Action, typename Cleanup>
224 T operator()(Init init, Action action, Cleanup cleanup)
225 {
226 T result;
227 init();
228 __try
229 {
230 result = action();
231 }
233 {
234 cleanup();
235 }
237 return result;
238 }
239};
240
241template<>
242struct __crt_seh_guarded_call<void>
243{
244 template<typename Init, typename Action, typename Cleanup>
245 void operator()(Init init, Action action, Cleanup cleanup)
246 {
247 init();
248 __try
249 {
250 action();
251 }
253 {
254 cleanup();
255 }
257 }
258};
259
260template<typename FuncPtr>
261FuncPtr __crt_get_proc_address(HMODULE module, const char* name)
262{
263 return (FuncPtr)GetProcAddress(module, name);
264}
265
266template<typename Action, typename Cleanup>
267void __crt_call_and_cleanup(Action action, Cleanup cleanup)
268{
269 action();
270 cleanup();
271}
272
273struct __crt_malloc_free_policy
274{
275 template<typename T>
276 void operator()(T* const ptr) throw()
277 {
278 _free_crt((void*)ptr);
279 }
280};
281
282struct __crt_public_free_policy
283{
284 template<typename T>
285 void operator()(T* const ptr) throw()
286 {
287 _free_crt((void*)ptr);
288 }
289};
290
291struct __crt_malloca_free_policy
292{
293 void operator()(void* const ptr) throw()
294 {
296 }
297};
298
299template <typename T, typename FreePolicy = __crt_malloc_free_policy>
300class __crt_unique_heap_ptr
301{
302 T* _ptr;
303public:
304 __crt_unique_heap_ptr() : _ptr(nullptr) {}
305 __crt_unique_heap_ptr(T* ptr) : _ptr(ptr) {}
306 __crt_unique_heap_ptr(__crt_unique_heap_ptr&& from) : _ptr(from._ptr) { from._ptr = nullptr; }
307 ~__crt_unique_heap_ptr() { FreePolicy()(_ptr); }
308 void attach(T* ptr) { FreePolicy()(_ptr); _ptr = ptr; }
309 T* detach() { T* ptr = _ptr; _ptr = nullptr; return ptr; }
310 operator bool() const { return _ptr != nullptr; }
311 T* get() const { return _ptr; }
312 T** get_address_of() { return &_ptr; }
313
314 // Move assignment
315 __crt_unique_heap_ptr& operator=(__crt_unique_heap_ptr&& from)
316 {
317 FreePolicy()(_ptr);
318 _ptr = from._ptr;
319 from._ptr = nullptr;
320 return *this;
321 }
322};
323
324template <typename T>
325using __crt_scoped_stack_ptr = __crt_unique_heap_ptr<T, __crt_malloca_free_policy>;
326
327#define _malloc_crt_t(t, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_malloc_crt((n) * sizeof(t)))))
328#define _calloc_crt_t(t, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_calloc_crt((n), sizeof(t)))))
329#define _recalloc_crt_t(t, p, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_recalloc_crt(p, (n), sizeof(t)))))
330#define _malloca_crt_t(t, n) (__crt_scoped_stack_ptr<t>(static_cast<t*>(_malloca_crt((n) * sizeof(t)))))
331
332#endif // __cplusplus
void get(int argc, const char *argv[])
Definition: cmds.c:480
int(__cdecl * _PIFV)(void)
void(__cdecl * _PVFV)(void)
#define GetProcAddress(x, y)
Definition: compat.h:753
static void cleanup(void)
Definition: main.c:1335
const WCHAR * action
Definition: action.c:7509
static const WCHAR Cleanup[]
Definition: register.c:80
return nullptr
Definition: expand.cpp:78
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
GLuint64EXT * result
Definition: glext.h:11304
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
char __ImageBase
_PVFV __xp_z[]
#define _free_crt
_PVFV __xp_a[]
#define _freea_crt
_PVFV __xc_a[]
_PVFV __xc_z[]
_PVFV __xt_a[]
const IMAGE_TLS_DIRECTORY _tls_used
_PVFV __xt_z[]
_PIFV __xi_z[]
_PIFV __xi_a[]
void * _InterlockedExchangePointer(_Interlocked_operand_ void *volatile *_Target, void *_Value)
#define T
Definition: mbstring.h:31
unsigned int uintptr_t
Definition: intrin.h:47
static PVOID ptr
Definition: dispmode.c:27
#define bool
Definition: nsiface.idl:72
#define __try
Definition: pseh2_64.h:188
#define __endtry
Definition: pseh2_64.h:191
#define __finally
Definition: pseh2_64.h:190
uintptr_t __security_cookie
Definition: gs_support.c:49
CardRegion * from
Definition: spigame.cpp:19
Definition: name.c:39
Definition: pdh_main.c:96
unsigned int Uint
Definition: utypes.h:43
_In_ WDFIOTARGET _In_ _Strict_type_match_ WDF_IO_TARGET_SENT_IO_ACTION Action
Definition: wdfiotarget.h:510
static int init
Definition: wintirpc.c:33