ReactOS 0.4.16-dev-822-gbcedb53
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$XIZ", long, read)
35#pragma section(".CRT$XCA", long, read)
36#pragma section(".CRT$XCZ", long, read)
37#pragma section(".CRT$XPA", long, read)
38#pragma section(".CRT$XPZ", long, read)
39#pragma section(".CRT$XTA", long, read)
40#pragma section(".CRT$XTZ", long, read)
41
42
43#pragma section(".CRT$XDA",long,read)
44#pragma section(".CRT$XDC",long,read)
45#pragma section(".CRT$XDZ",long,read)
46
47#pragma section(".CRT$XLA",long,read) // TLS callback start
48#pragma section(".CRT$XLC",long,read) // TLS constructors
49#pragma section(".CRT$XLD",long,read) // TLS destructors
50#pragma section(".CRT$XLZ",long,read) // TLS callback end
51
52#pragma section(".tls",long,read,write)
53#pragma section(".tls$AAA",long,read,write)
54#pragma section(".tls$ZZZ",long,read,write)
55#endif
56
57extern _PIFV __xi_a[];
58extern _PIFV __xi_z[];
59extern _PVFV __xc_a[];
60extern _PVFV __xc_z[];
61extern _PVFV __xp_a[];
62extern _PVFV __xp_z[];
63extern _PVFV __xt_a[];
64extern _PVFV __xt_z[];
65
66extern char __ImageBase;
67
69
70#define CRT_WARNING_DISABLE_PUSH(wn, message) \
71 __pragma(warning(push)) \
72 __pragma(warning(disable: wn))
73
74#define CRT_WARNING_POP \
75 __pragma(warning(pop))
76
77#define _BEGIN_SECURE_CRT_DEPRECATION_DISABLE \
78 __pragma(warning(push)) \
79 __pragma(warning(disable: 4996))
80
81#define _END_SECURE_CRT_DEPRECATION_DISABLE \
82 __pragma(warning(pop))
83
84#define _CRT_DEBUGGER_IGNORE -1
85#define _CRT_DEBUGGER_GSFAILURE 1
86#define _CRT_DEBUGGER_INVALIDPARAMETER 2
87#define _CRT_DEBUGGER_ABORT 3
88
89#define _CRT_DEBUGGER_HOOK(x)
90
91#define _CRT_SECURITYCRITICAL_ATTRIBUTE
92#define _CRT_SECURITYSAFECRITICAL_ATTRIBUTE
93
94//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95//
96// Parameter validation macros
97// Partly duplicated in corecrt_internal_strtox.h
98//
99//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100
101#ifdef _DEBUG
102 #define _INVALID_PARAMETER(expr) _invalid_parameter(expr, __FUNCTIONW__, __FILEW__, __LINE__, 0)
103#else
104 #define _INVALID_PARAMETER(expr) _invalid_parameter_noinfo()
105#endif
106
107#define _VALIDATE_RETURN(expr, errorcode, retexpr) \
108 { \
109 int _Expr_val = !!(expr); \
110 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
111 if (!(_Expr_val)) \
112 { \
113 *_errno() = (errorcode); \
114 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \
115 return (retexpr); \
116 } \
117 }
118
119#define _VALIDATE_RETURN_VOID(expr, errorcode) \
120 { \
121 int _Expr_val = !!(expr); \
122 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
123 if (!(_Expr_val)) \
124 { \
125 *_errno() = (errorcode); \
126 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \
127 return; \
128 } \
129 }
130
131#define _VALIDATE_RETURN_NOERRNO(expr, retexpr) \
132 { \
133 int _Expr_val = !!(expr); \
134 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
135 if (!(_Expr_val)) \
136 { \
137 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \
138 return (retexpr); \
139 } \
140 }
141
142#define _VALIDATE_RETURN_ERRCODE(expr, errorcode) \
143 _VALIDATE_RETURN(expr, errorcode, errorcode)
144
145#define _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, retexpr) \
146 { \
147 int _Expr_val = !!(expr); \
148 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
149 if (!(_Expr_val)) \
150 { \
151 *__doserrno() = 0L; \
152 *_errno() = errorcode; \
153 _INVALID_PARAMETER(_CRT_WIDE(#expr) ); \
154 return (retexpr); \
155 } \
156 }
157
158#define _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(expr, errorcode) \
159 _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, errorcode)
160
161#define _VALIDATE_RETURN_NOEXC(expr, errorcode, retexpr) \
162 { \
163 if (!(expr)) \
164 { \
165 *_errno() = errorcode; \
166 return (retexpr); \
167 } \
168 }
169
170#define _VALIDATE_RETURN_ERRCODE_NOEXC(expr, errorcode) \
171 _VALIDATE_RETURN_NOEXC(expr, errorcode, errorcode)
172
173#define _malloc_crt malloc
174#define _free_crt free
175#define _calloc_crt calloc
176#define _recalloc_crt _recalloc
177#define _malloca_crt _malloca
178#define _freea_crt _freea
179
180#ifdef __cplusplus
181} // extern "C"
182
183template<typename T>
184__forceinline
185T __crt_fast_encode_pointer(T ptr)
186{
187 union { T Ptr; uintptr_t Uint; } u = { ptr };
188 u.Uint ^= __security_cookie;
189 return u.Ptr;
190}
191
192template<typename T>
193__forceinline
194T __crt_fast_decode_pointer(T ptr)
195{
196 union { T Ptr; uintptr_t Uint; } u = { ptr };
197 u.Uint ^= __security_cookie;
198 return u.Ptr;
199}
200
201template<typename T>
202T __crt_interlocked_read(const T volatile* ptr)
203{
204 return *ptr;
205}
206
207template<typename T>
208T __crt_interlocked_read_pointer(const T volatile* ptr)
209{
210 return *ptr;
211}
212
213template<typename T>
214T __crt_interlocked_exchange_pointer(T *ptr, void* value)
215{
216 return (T)_InterlockedExchangePointer((void* volatile*)ptr, value);
217}
218
219template<typename T>
220struct __crt_seh_guarded_call
221{
222 template<typename Init, typename Action, typename Cleanup>
223 T operator()(Init init, Action action, Cleanup cleanup)
224 {
225 T result;
226 init();
227 __try
228 {
229 result = action();
230 }
232 {
233 cleanup();
234 }
236 return result;
237 }
238};
239
240template<>
241struct __crt_seh_guarded_call<void>
242{
243 template<typename Init, typename Action, typename Cleanup>
244 void operator()(Init init, Action action, Cleanup cleanup)
245 {
246 init();
247 __try
248 {
249 action();
250 }
252 {
253 cleanup();
254 }
256 }
257};
258
259template<typename FuncPtr>
260FuncPtr __crt_get_proc_address(HMODULE module, const char* name)
261{
262 return (FuncPtr)GetProcAddress(module, name);
263}
264
265template<typename Action, typename Cleanup>
266void __crt_call_and_cleanup(Action action, Cleanup cleanup)
267{
268 action();
269 cleanup();
270}
271
272struct __crt_malloc_free_policy
273{
274 template<typename T>
275 void operator()(T* const ptr) throw()
276 {
277 _free_crt((void*)ptr);
278 }
279};
280
281struct __crt_public_free_policy
282{
283 template<typename T>
284 void operator()(T* const ptr) throw()
285 {
286 _free_crt((void*)ptr);
287 }
288};
289
290struct __crt_malloca_free_policy
291{
292 void operator()(void* const ptr) throw()
293 {
295 }
296};
297
298template <typename T, typename FreePolicy = __crt_malloc_free_policy>
299class __crt_unique_heap_ptr
300{
301 T* _ptr;
302public:
303 __crt_unique_heap_ptr() : _ptr(nullptr) {}
304 __crt_unique_heap_ptr(T* ptr) : _ptr(ptr) {}
305 __crt_unique_heap_ptr(__crt_unique_heap_ptr&& from) : _ptr(from._ptr) { from._ptr = nullptr; }
306 ~__crt_unique_heap_ptr() { FreePolicy()(_ptr); }
307 void attach(T* ptr) { FreePolicy()(_ptr); _ptr = ptr; }
308 T* detach() { T* ptr = _ptr; _ptr = nullptr; return ptr; }
309 operator bool() const { return _ptr != nullptr; }
310 T* get() const { return _ptr; }
311 T** get_address_of() { return &_ptr; }
312
313 // Move assignment
314 __crt_unique_heap_ptr& operator=(__crt_unique_heap_ptr&& from)
315 {
316 FreePolicy()(_ptr);
317 _ptr = from._ptr;
318 from._ptr = nullptr;
319 return *this;
320 }
321};
322
323template <typename T>
324using __crt_scoped_stack_ptr = __crt_unique_heap_ptr<T, __crt_malloca_free_policy>;
325
326#define _malloc_crt_t(t, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_malloc_crt((n) * sizeof(t)))))
327#define _calloc_crt_t(t, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_calloc_crt((n), sizeof(t)))))
328#define _recalloc_crt_t(t, p, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_recalloc_crt(p, (n), sizeof(t)))))
329#define _malloca_crt_t(t, n) (__crt_scoped_stack_ptr<t>(static_cast<t*>(_malloca_crt((n) * sizeof(t)))))
330
331#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:172
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174
uintptr_t __security_cookie
Definition: gs_support.c:49
CardRegion * from
Definition: spigame.cpp:19
Definition: name.c:39
Definition: pdh_main.c:94
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