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