[BASESRV-CONSRV-WINSRV]
[reactos.git] / include / crt / malloc.h
1 /**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the w64 mingw-runtime package.
4 * No warranty is given; refer to the file DISCLAIMER within this package.
5 */
6 #ifndef _MALLOC_H_
7 #define _MALLOC_H_
8
9 #include <crtdefs.h>
10
11 #pragma pack(push,_CRT_PACKING)
12
13 #ifndef _MM_MALLOC_H_INCLUDED
14 #define _MM_MALLOC_H_INCLUDED
15 #endif
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 #ifdef _WIN64
22 #define _HEAP_MAXREQ 0xFFFFFFFFFFFFFFE0
23 #else
24 #define _HEAP_MAXREQ 0xFFFFFFE0
25 #endif
26
27 #ifndef _STATIC_ASSERT
28 #define _STATIC_ASSERT(expr) extern char (*static_assert(void)) [(expr) ? 1 : -1]
29 #endif
30
31 /* Return codes for _heapwalk() */
32 #define _HEAPEMPTY (-1)
33 #define _HEAPOK (-2)
34 #define _HEAPBADBEGIN (-3)
35 #define _HEAPBADNODE (-4)
36 #define _HEAPEND (-5)
37 #define _HEAPBADPTR (-6)
38
39 /* Values for _heapinfo.useflag */
40 #define _FREEENTRY 0
41 #define _USEDENTRY 1
42
43 #ifndef _HEAPINFO_DEFINED
44 #define _HEAPINFO_DEFINED
45 /* The structure used to walk through the heap with _heapwalk. */
46 typedef struct _heapinfo {
47 int *_pentry;
48 size_t _size;
49 int _useflag;
50 } _HEAPINFO;
51 #endif
52
53 extern unsigned int _amblksiz;
54
55 /* Make sure that X86intrin.h doesn't produce here collisions. */
56 #if (!defined (_XMMINTRIN_H_INCLUDED) && !defined (_MM_MALLOC_H_INCLUDED)) || defined(_aligned_malloc)
57 #define __DO_ALIGN_DEFINES
58 #endif
59
60 #ifdef __DO_ALIGN_DEFINES
61 #pragma push_macro("_aligned_free")
62 #pragma push_macro("_aligned_malloc")
63 #undef _aligned_free
64 #undef _aligned_malloc
65 #endif
66
67 #define _mm_free(a) _aligned_free(a)
68 #define _mm_malloc(a,b) _aligned_malloc(a,b)
69
70 #ifndef _CRT_ALLOCATION_DEFINED
71 #define _CRT_ALLOCATION_DEFINED
72
73 _Check_return_
74 _Ret_maybenull_
75 _Post_writable_byte_size_(_NumOfElements * _SizeOfElements)
76 void*
77 __cdecl
78 calloc(
79 _In_ size_t _NumOfElements,
80 _In_ size_t _SizeOfElements);
81
82 void
83 __cdecl
84 free(
85 _Pre_maybenull_ _Post_invalid_ void *_Memory);
86
87 _Check_return_
88 _Ret_maybenull_
89 _Post_writable_byte_size_(_Size)
90 void*
91 __cdecl
92 malloc(
93 _In_ size_t _Size);
94
95 _Check_return_
96 _Ret_maybenull_
97 _Post_writable_byte_size_(_NewSize)
98 void*
99 __cdecl
100 realloc(
101 _Pre_maybenull_ _Post_invalid_ void *_Memory,
102 _In_ size_t _NewSize);
103
104 _Check_return_
105 _Ret_maybenull_
106 _Post_writable_byte_size_(_Count * _Size)
107 _CRTIMP
108 void*
109 __cdecl
110 _recalloc(
111 _Pre_maybenull_ _Post_invalid_ void *_Memory,
112 _In_ size_t _Count,
113 _In_ size_t _Size);
114
115 #ifdef __DO_ALIGN_DEFINES
116
117 _CRTIMP
118 void
119 __cdecl
120 _aligned_free(
121 _Pre_maybenull_ _Post_invalid_ void *_Memory);
122
123 _Check_return_
124 _Ret_maybenull_
125 _Post_writable_byte_size_(_Size)
126 _CRTIMP
127 void*
128 __cdecl
129 _aligned_malloc(
130 _In_ size_t _Size,
131 _In_ size_t _Alignment);
132
133 #endif /* __DO_ALIGN_DEFINES */
134
135 _Check_return_
136 _Ret_maybenull_
137 _Post_writable_byte_size_(_Size)
138 _CRTIMP
139 void*
140 __cdecl
141 _aligned_offset_malloc(
142 _In_ size_t _Size,
143 _In_ size_t _Alignment,
144 _In_ size_t _Offset);
145
146 _Check_return_
147 _Ret_maybenull_
148 _Post_writable_byte_size_(_Size)
149 _CRTIMP
150 void*
151 __cdecl
152 _aligned_realloc(
153 _Pre_maybenull_ _Post_invalid_ void *_Memory,
154 _In_ size_t _Size,
155 _In_ size_t _Alignment);
156
157 _Check_return_
158 _Ret_maybenull_
159 _Post_writable_byte_size_(_Count * _Size)
160 _CRTIMP
161 void*
162 __cdecl
163 _aligned_recalloc(
164 _Pre_maybenull_ _Post_invalid_ void *_Memory,
165 _In_ size_t _Count,
166 _In_ size_t _Size,
167 _In_ size_t _Alignment);
168
169 _Check_return_
170 _Ret_maybenull_
171 _Post_writable_byte_size_(_Size)
172 _CRTIMP
173 void*
174 __cdecl
175 _aligned_offset_realloc(
176 _Pre_maybenull_ _Post_invalid_ void *_Memory,
177 _In_ size_t _Size,
178 _In_ size_t _Alignment,
179 _In_ size_t _Offset);
180
181 _Check_return_
182 _Ret_maybenull_
183 _Post_writable_byte_size_(_Count * _Size)
184 _CRTIMP
185 void*
186 __cdecl
187 _aligned_offset_recalloc(
188 _Pre_maybenull_ _Post_invalid_ void *_Memory,
189 _In_ size_t _Count,
190 _In_ size_t _Size,
191 _In_ size_t _Alignment,
192 _In_ size_t _Offset);
193
194 #endif /* _CRT_ALLOCATION_DEFINED */
195
196 #ifdef __DO_ALIGN_DEFINES
197 #undef __DO_ALIGN_DEFINES
198
199 #pragma pop_macro("_aligned_malloc")
200 #pragma pop_macro("_aligned_free")
201
202 #endif
203
204 #define _MAX_WAIT_MALLOC_CRT 60000
205
206 _CRTIMP int __cdecl _resetstkoflw (void);
207
208 _CRTIMP
209 unsigned long
210 __cdecl
211 _set_malloc_crt_max_wait(
212 _In_ unsigned long _NewValue);
213
214 _Check_return_
215 _Ret_maybenull_
216 _Post_writable_byte_size_(_NewSize)
217 _CRTIMP
218 void*
219 __cdecl
220 _expand(
221 _In_opt_ void *_Memory,
222 _In_ size_t _NewSize);
223
224 _Check_return_
225 _CRTIMP
226 size_t
227 __cdecl
228 _msize(
229 _In_ void *_Memory);
230
231 #ifdef __GNUC__
232 #undef _alloca
233 #define _alloca(x) __builtin_alloca((x))
234 #else
235 _Ret_notnull_
236 _Post_writable_byte_size_(_Size)
237 void*
238 __cdecl
239 _alloca(
240 _In_ size_t _Size);
241 #endif
242
243 _Check_return_
244 _CRTIMP
245 size_t
246 __cdecl
247 _get_sbh_threshold(void);
248
249 _CRTIMP
250 int
251 __cdecl
252 _set_sbh_threshold(
253 _In_ size_t _NewValue);
254
255 _CRTIMP
256 errno_t
257 __cdecl
258 _set_amblksiz(
259 _In_ size_t _Value);
260
261 _CRTIMP
262 errno_t
263 __cdecl
264 _get_amblksiz(
265 _Out_ size_t *_Value);
266
267 _Check_return_
268 _CRTIMP
269 int
270 __cdecl
271 _heapadd(
272 _In_ void *_Memory,
273 _In_ size_t _Size);
274
275 _Check_return_
276 _CRTIMP
277 int
278 __cdecl
279 _heapchk(void);
280
281 _Check_return_
282 _CRTIMP
283 int
284 __cdecl
285 _heapmin(void);
286
287 _CRTIMP
288 int
289 __cdecl
290 _heapset(
291 _In_ unsigned int _Fill);
292
293 _CRTIMP
294 int
295 __cdecl
296 _heapwalk(
297 _Inout_ _HEAPINFO *_EntryInfo);
298
299 _CRTIMP
300 size_t
301 __cdecl
302 _heapused(
303 size_t *_Used,
304 size_t *_Commit);
305
306 _CRTIMP
307 intptr_t
308 __cdecl
309 _get_heap_handle(void);
310
311 #define _ALLOCA_S_THRESHOLD 1024
312 #define _ALLOCA_S_STACK_MARKER 0xCCCC
313 #define _ALLOCA_S_HEAP_MARKER 0xDDDD
314
315 #if(defined(_X86_) && !defined(__x86_64))
316 #define _ALLOCA_S_MARKER_SIZE 8
317 #elif defined(__ia64__) || defined(__x86_64)
318 #define _ALLOCA_S_MARKER_SIZE 16
319 #elif defined(__arm__)
320 #define _ALLOCA_S_MARKER_SIZE 8
321 #endif
322
323 #if !defined(RC_INVOKED)
324 static __inline void *_MarkAllocaS(void *_Ptr,unsigned int _Marker) {
325 if(_Ptr) {
326 *((unsigned int*)_Ptr) = _Marker;
327 _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE;
328 }
329 return _Ptr;
330 }
331 #endif
332
333 #undef _malloca
334 #define _malloca(size) \
335 ((((size) + _ALLOCA_S_MARKER_SIZE) <= _ALLOCA_S_THRESHOLD) ? \
336 _MarkAllocaS(_alloca((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_STACK_MARKER) : \
337 _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_HEAP_MARKER))
338 #undef _FREEA_INLINE
339 #define _FREEA_INLINE
340
341 #ifndef RC_INVOKED
342 #undef _freea
343 static __inline void __cdecl _freea(void *_Memory) {
344 unsigned int _Marker;
345 if(_Memory) {
346 _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE;
347 _Marker = *(unsigned int *)_Memory;
348 if(_Marker==_ALLOCA_S_HEAP_MARKER) {
349 free(_Memory);
350 }
351 #ifdef _ASSERTE
352 else if(_Marker!=_ALLOCA_S_STACK_MARKER) {
353 _ASSERTE(("Corrupted pointer passed to _freea",0));
354 }
355 #endif
356 }
357 }
358 #endif /* RC_INVOKED */
359
360 #ifndef NO_OLDNAMES
361 #define alloca _alloca
362 #endif
363
364 #ifdef HEAPHOOK
365 #ifndef _HEAPHOOK_DEFINED
366 #define _HEAPHOOK_DEFINED
367 typedef int (__cdecl *_HEAPHOOK)(int,size_t,void *,void **);
368 #endif
369
370 _CRTIMP
371 _HEAPHOOK
372 __cdecl
373 _setheaphook(
374 _In_opt_ _HEAPHOOK _NewHook);
375
376 #define _HEAP_MALLOC 1
377 #define _HEAP_CALLOC 2
378 #define _HEAP_FREE 3
379 #define _HEAP_REALLOC 4
380 #define _HEAP_MSIZE 5
381 #define _HEAP_EXPAND 6
382 #endif
383
384 #ifdef __cplusplus
385 }
386 #endif
387
388 #pragma pack(pop)
389
390 #endif /* _MALLOC_H_ */