ReactOS 0.4.16-dev-1040-g85afe48
malloc.h
Go to the documentation of this file.
1//
2// malloc.h
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// The memory allocation library.
7//
8#pragma once
9#ifndef _INC_MALLOC // include guard for 3rd party interop
10#define _INC_MALLOC
11
12#include <corecrt.h>
13#include <corecrt_malloc.h>
14
15#pragma warning(push)
16#pragma warning(disable: _UCRT_DISABLED_WARNINGS)
18
20
21
22
23// Maximum heap request the heap manager will attempt
24#ifdef _WIN64
25 #define _HEAP_MAXREQ 0xFFFFFFFFFFFFFFE0
26#else
27 #define _HEAP_MAXREQ 0xFFFFFFE0
28#endif
29
30
31
32// Constants for _heapchk and _heapwalk routines
33#define _HEAPEMPTY (-1)
34#define _HEAPOK (-2)
35#define _HEAPBADBEGIN (-3)
36#define _HEAPBADNODE (-4)
37#define _HEAPEND (-5)
38#define _HEAPBADPTR (-6)
39#define _FREEENTRY 0
40#define _USEDENTRY 1
41
42typedef struct _heapinfo
43{
44 int* _pentry;
45 size_t _size;
46 int _useflag;
48
49#define _mm_free(a) _aligned_free(a)
50#define _mm_malloc(a, b) _aligned_malloc(a, b)
51
52
53#if defined(__GNUC__) || defined(__clang__)
54#define _alloca(x) __builtin_alloca((x))
55#else
57void* __cdecl _alloca(_In_ size_t _Size);
58#endif
59
60
61
62#if !defined __midl && !defined RC_INVOKED
63
65
67 _DCRTIMP int __cdecl _heapmin(void);
68
69 #if defined _DEBUG || defined _CRT_USE_WINAPI_FAMILY_DESKTOP_APP || defined _CORECRT_BUILD
71 #endif
72
73 #if defined _CRT_USE_WINAPI_FAMILY_DESKTOP_APP || defined _CRT_USE_WINAPI_FAMILY_GAMES
75 #endif
76
78
79 #define _ALLOCA_S_THRESHOLD 1024
80 #define _ALLOCA_S_STACK_MARKER 0xCCCC
81 #define _ALLOCA_S_HEAP_MARKER 0xDDDD
82
83 #ifdef _WIN64
84 #define _ALLOCA_S_MARKER_SIZE 16
85 #else
86 #define _ALLOCA_S_MARKER_SIZE 8
87 #endif
88
89 _STATIC_ASSERT(sizeof(unsigned int) <= _ALLOCA_S_MARKER_SIZE);
90
91
92 #pragma warning(push)
93 #pragma warning(disable: 6540) // C6540: attribute annotations on this function will invalidate all
94 // of its existing __declspec annotations
95
96 __inline void* _MarkAllocaS(_Out_opt_ __crt_typefix(unsigned int*) void* _Ptr, unsigned int _Marker)
97 {
98 if (_Ptr)
99 {
100 *((unsigned int*)_Ptr) = _Marker;
101 _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE;
102 }
103 return _Ptr;
104 }
105
107 {
108 size_t _MarkedSize = _Size + _ALLOCA_S_MARKER_SIZE;
109 return _MarkedSize > _Size ? _MarkedSize : 0;
110 }
111
112 #pragma warning(pop)
113
114#endif
115
116
117
118#ifdef _DEBUG
119// C6255: _alloca indicates failure by raising a stack overflow exception
120// C6386: buffer overrun
121 #ifndef _CRTDBG_MAP_ALLOC
122 #undef _malloca
123 #define _malloca(size) \
124 __pragma(warning(suppress: 6255 6386)) \
125 (_MallocaComputeSize(size) != 0 \
126 ? _MarkAllocaS(malloc(_MallocaComputeSize(size)), _ALLOCA_S_HEAP_MARKER) \
127 : NULL)
128 #endif
129
130#else
131
132 #undef _malloca
133 #define _malloca(size) \
134 __pragma(warning(suppress: 6255 6386)) \
135 (_MallocaComputeSize(size) != 0 \
136 ? (((_MallocaComputeSize(size) <= _ALLOCA_S_THRESHOLD) \
137 ? _MarkAllocaS(_alloca(_MallocaComputeSize(size)), _ALLOCA_S_STACK_MARKER) \
138 : _MarkAllocaS(malloc(_MallocaComputeSize(size)), _ALLOCA_S_HEAP_MARKER))) \
139 : NULL)
140
141#endif
142
143
144
145#if defined __midl && !defined RC_INVOKED
146#elif defined _DEBUG && defined _CRTDBG_MAP_ALLOC
147#else
148
149 #undef _freea
150
151 #pragma warning(push)
152 #pragma warning(disable: 6014) // leaking memory
154 {
155 unsigned int _Marker;
156 if (_Memory)
157 {
158 _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE;
159 _Marker = *(unsigned int*)_Memory;
160 if (_Marker == _ALLOCA_S_HEAP_MARKER)
161 {
162 free(_Memory);
163 }
164 #ifdef _ASSERTE
165 else if (_Marker != _ALLOCA_S_STACK_MARKER)
166 {
167 _ASSERTE(("Corrupted pointer passed to _freea" && 0));
168 }
169 #endif
170 }
171 }
172 #pragma warning(pop)
173
174#endif
175
176
177
178#if defined(_CRT_INTERNAL_NONSTDC_NAMES) && _CRT_INTERNAL_NONSTDC_NAMES
179 #define alloca _alloca
180#endif
181
182
183
186#pragma warning(pop) // _UCRT_DISABLED_WARNINGS
187#endif // _INC_MALLOC
#define __inline
Definition: _wctype.cpp:15
#define __cdecl
Definition: accygwin.h:79
#define _ASSERTE(expr)
Definition: crtdbg.h:114
struct _heapinfo _HEAPINFO
_Check_return_ _CRTIMP int __cdecl _heapchk(void)
Definition: malloc.c:123
#define _ALLOCA_S_STACK_MARKER
Definition: malloc.h:308
_CRTIMP int __cdecl _heapwalk(_Inout_ _HEAPINFO *_EntryInfo)
static __inline void * _MarkAllocaS(void *_Ptr, unsigned int _Marker)
Definition: malloc.h:320
static __inline void __cdecl _freea(void *_Memory)
Definition: malloc.h:339
_Check_return_ _Ret_maybenull_ _In_ size_t _In_ size_t _Size
Definition: malloc.h:109
_CRTIMP intptr_t __cdecl _get_heap_handle(void)
Definition: heap.c:367
_Check_return_ _CRTIMP int __cdecl _heapmin(void)
Definition: malloc.c:133
#define _ALLOCA_S_HEAP_MARKER
Definition: malloc.h:309
_CRTIMP int __cdecl _resetstkoflw(void)
Definition: stack.c:38
#define free
Definition: debug_ros.c:5
#define _Ret_notnull_
Definition: ms_sal.h:528
#define _Out_opt_
Definition: no_sal2.h:214
#define _Inout_
Definition: no_sal2.h:162
#define _Pre_maybenull_
Definition: no_sal2.h:514
#define _Check_return_
Definition: no_sal2.h:60
#define _Post_writable_byte_size_(s)
Definition: no_sal2.h:542
#define _Post_invalid_
Definition: no_sal2.h:524
#define _In_
Definition: no_sal2.h:158
int * _pentry
Definition: malloc.h:43
int _useflag
Definition: malloc.h:45
size_t _size
Definition: malloc.h:44
#define _DCRTIMP
Definition: corecrt.h:154
#define _STATIC_ASSERT(expr)
Definition: corecrt.h:305
#define _ACRTIMP
Definition: corecrt.h:138
#define __crt_typefix(ctype)
Definition: corecrt.h:236
#define _UCRT_DISABLE_CLANG_WARNINGS
Definition: corecrt.h:109
#define _UCRT_RESTORE_CLANG_WARNINGS
Definition: corecrt.h:117
__inline size_t _MallocaComputeSize(size_t _Size)
Definition: malloc.h:106
#define _ALLOCA_S_MARKER_SIZE
Definition: malloc.h:86
int intptr_t
Definition: vcruntime.h:134
#define _CRT_END_C_HEADER
Definition: vcruntime.h:42
#define _CRT_BEGIN_C_HEADER
Definition: vcruntime.h:40
#define __CRTDECL
Definition: yvals.h:17