1 /**
2  * This file has no copyright assigned and is placed in the Public Domain.
3  * This file is part of the mingw-w64 runtime package.
4  * No warranty is given; refer to the file DISCLAIMER.PD 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 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #ifdef _WIN64
18 #define _HEAP_MAXREQ 0xFFFFFFFFFFFFFFE0
19 #else
20 #define _HEAP_MAXREQ 0xFFFFFFE0
21 #endif
22 
23 #ifndef _STATIC_ASSERT
24 #if defined(_MSC_VER)
25 #define _STATIC_ASSERT(expr) typedef char __static_assert_t[(expr)]
26 #else
27 #define _STATIC_ASSERT(expr) extern void __static_assert_t(int [(expr)?1:-1])
28 #endif
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 #ifndef _MM_MALLOC_H_INCLUDED
61 #define _MM_MALLOC_H_INCLUDED
62 #endif
63 
64 #ifdef __DO_ALIGN_DEFINES
65 #pragma push_macro("_aligned_free")
66 #pragma push_macro("_aligned_malloc")
67 #undef _aligned_free
68 #undef _aligned_malloc
69 #endif
70 
71 #define _mm_free(a) _aligned_free(a)
72 #define _mm_malloc(a,b) _aligned_malloc(a,b)
73 
74 #ifndef _CRT_ALLOCATION_DEFINED
75 #define _CRT_ALLOCATION_DEFINED
76   void *__cdecl calloc(size_t _NumOfElements,size_t _SizeOfElements);
77   void __cdecl free(void *_Memory);
78   void *__cdecl malloc(size_t _Size);
79   void *__cdecl realloc(void *_Memory,size_t _NewSize);
80   _CRTIMP void *__cdecl _recalloc(void *_Memory,size_t _Count,size_t _Size);
81 
82 #ifdef __DO_ALIGN_DEFINES
83   _CRTIMP void __cdecl _aligned_free(void *_Memory);
84   _CRTIMP void *__cdecl _aligned_malloc(size_t _Size,size_t _Alignment);
85 #endif
86 
87   _CRTIMP void *__cdecl _aligned_offset_malloc(size_t _Size,size_t _Alignment,size_t _Offset);
88   _CRTIMP void *__cdecl _aligned_realloc(void *_Memory,size_t _Size,size_t _Alignment);
89   _CRTIMP void *__cdecl _aligned_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment);
90   _CRTIMP void *__cdecl _aligned_offset_realloc(void *_Memory,size_t _Size,size_t _Alignment,size_t _Offset);
91   _CRTIMP void *__cdecl _aligned_offset_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment,size_t _Offset);
92 #endif
93 
94 #ifdef __DO_ALIGN_DEFINES
95 #undef __DO_ALIGN_DEFINES
96 
97 #pragma pop_macro("_aligned_malloc")
98 #pragma pop_macro("_aligned_free")
99 
100 #endif
101 
102 /* Users should really use MS provided versions */
103 void * __mingw_aligned_malloc (size_t _Size, size_t _Alignment);
104 void __mingw_aligned_free (void *_Memory);
105 void * __mingw_aligned_offset_realloc (void *_Memory, size_t _Size, size_t _Alignment, size_t _Offset);
106 void * __mingw_aligned_realloc (void *_Memory, size_t _Size, size_t _Offset);
107 
108 #define _MAX_WAIT_MALLOC_CRT 60000
109 
110   _CRTIMP int __cdecl _resetstkoflw (void);
111   _CRTIMP unsigned long __cdecl _set_malloc_crt_max_wait(unsigned long _NewValue);
112 
113   _CRTIMP void *__cdecl _expand(void *_Memory,size_t _NewSize);
114   _CRTIMP size_t __cdecl _msize(void *_Memory);
115 #ifdef __GNUC__
116 #undef _alloca
117 #define _alloca(x) __builtin_alloca((x))
118 #else
119   void *__cdecl _alloca(size_t _Size) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
120 #endif
121   _CRTIMP size_t __cdecl _get_sbh_threshold(void);
122   _CRTIMP int __cdecl _set_sbh_threshold(size_t _NewValue);
123   _CRTIMP errno_t __cdecl _set_amblksiz(size_t _Value);
124   _CRTIMP errno_t __cdecl _get_amblksiz(size_t *_Value);
125   _CRTIMP int __cdecl _heapadd(void *_Memory,size_t _Size);
126   _CRTIMP int __cdecl _heapchk(void);
127   _CRTIMP int __cdecl _heapmin(void);
128   _CRTIMP int __cdecl _heapset(unsigned int _Fill);
129   _CRTIMP int __cdecl _heapwalk(_HEAPINFO *_EntryInfo);
130   _CRTIMP size_t __cdecl _heapused(size_t *_Used,size_t *_Commit);
131   _CRTIMP intptr_t __cdecl _get_heap_handle(void);
132 
133 #define _ALLOCA_S_THRESHOLD 1024
134 #define _ALLOCA_S_STACK_MARKER 0xCCCC
135 #define _ALLOCA_S_HEAP_MARKER 0xDDDD
136 
137 #if defined(_ARM_) || (defined(_X86_) && !defined(__x86_64))
138 #define _ALLOCA_S_MARKER_SIZE 8
139 #elif defined(__ia64__) || defined(__x86_64) || defined(__aarch64__)
140 #define _ALLOCA_S_MARKER_SIZE 16
141 #endif
142 
143 #if !defined(RC_INVOKED)
_MarkAllocaS(void * _Ptr,unsigned int _Marker)144   static __inline void *_MarkAllocaS(void *_Ptr,unsigned int _Marker) {
145     if(_Ptr) {
146       *((unsigned int*)_Ptr) = _Marker;
147       _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE;
148     }
149     return _Ptr;
150   }
151 #endif
152 
153 #undef _malloca
154 #define _malloca(size) \
155   ((((size) + _ALLOCA_S_MARKER_SIZE) <= _ALLOCA_S_THRESHOLD) ? \
156     _MarkAllocaS(_alloca((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_STACK_MARKER) : \
157     _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_HEAP_MARKER))
158 #undef _FREEA_INLINE
159 #define _FREEA_INLINE
160 
161 #ifndef RC_INVOKED
162 #undef _freea
_freea(void * _Memory)163   static __inline void __cdecl _freea(void *_Memory) {
164     unsigned int _Marker;
165     if(_Memory) {
166       _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE;
167       _Marker = *(unsigned int *)_Memory;
168       if(_Marker==_ALLOCA_S_HEAP_MARKER) {
169 	free(_Memory);
170       }
171 #ifdef _ASSERTE
172       else if(_Marker!=_ALLOCA_S_STACK_MARKER) {
173 	_ASSERTE(("Corrupted pointer passed to _freea",0));
174       }
175 #endif
176     }
177   }
178 #endif /* RC_INVOKED */
179 
180 #ifndef	NO_OLDNAMES
181 #undef alloca
182 #ifdef __GNUC__
183 #define alloca(x) __builtin_alloca((x))
184 #else
185 #define alloca _alloca
186 #endif
187 #endif
188 
189 #ifdef HEAPHOOK
190 #ifndef _HEAPHOOK_DEFINED
191 #define _HEAPHOOK_DEFINED
192   typedef int (__cdecl *_HEAPHOOK)(int,size_t,void *,void **);
193 #endif
194 
195   _CRTIMP _HEAPHOOK __cdecl _setheaphook(_HEAPHOOK _NewHook);
196 
197 #define _HEAP_MALLOC 1
198 #define _HEAP_CALLOC 2
199 #define _HEAP_FREE 3
200 #define _HEAP_REALLOC 4
201 #define _HEAP_MSIZE 5
202 #define _HEAP_EXPAND 6
203 #endif
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #pragma pack(pop)
210 
211 #endif /* _MALLOC_H_ */
212