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 #ifndef _CRT_ALLOCATION_DEFINED
56 #define _CRT_ALLOCATION_DEFINED
57   void *__cdecl calloc(size_t _NumOfElements,size_t _SizeOfElements);
58   void __cdecl free(void *_Memory);
59   void *__cdecl malloc(size_t _Size);
60   void *__cdecl realloc(void *_Memory,size_t _NewSize);
61   _CRTIMP void *__cdecl _recalloc(void *_Memory,size_t _Count,size_t _Size);
62 
63   _CRTIMP void __cdecl _aligned_free(void *_Memory);
64   _CRTIMP void *__cdecl _aligned_malloc(size_t _Size,size_t _Alignment);
65 
66   _CRTIMP void *__cdecl _aligned_offset_malloc(size_t _Size,size_t _Alignment,size_t _Offset);
67   _CRTIMP void *__cdecl _aligned_realloc(void *_Memory,size_t _Size,size_t _Alignment);
68   _CRTIMP void *__cdecl _aligned_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment);
69   _CRTIMP void *__cdecl _aligned_offset_realloc(void *_Memory,size_t _Size,size_t _Alignment,size_t _Offset);
70   _CRTIMP void *__cdecl _aligned_offset_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment,size_t _Offset);
71 #endif
72 
73 /* Users should really use MS provided versions */
74 void * __mingw_aligned_malloc (size_t _Size, size_t _Alignment);
75 void __mingw_aligned_free (void *_Memory);
76 void * __mingw_aligned_offset_realloc (void *_Memory, size_t _Size, size_t _Alignment, size_t _Offset);
77 void * __mingw_aligned_realloc (void *_Memory, size_t _Size, size_t _Offset);
78 
79 /* Get the compiler's definition of _mm_malloc and _mm_free. */
80 #include <mm_malloc.h>
81 
82 #define _MAX_WAIT_MALLOC_CRT 60000
83 
84 #ifdef _CRT_USE_WINAPI_FAMILY_DESKTOP_APP
85   _CRTIMP int __cdecl _resetstkoflw (void);
86 #endif /* _CRT_USE_WINAPI_FAMILY_DESKTOP_APP */
87   _CRTIMP unsigned long __cdecl _set_malloc_crt_max_wait(unsigned long _NewValue);
88 
89   _CRTIMP void *__cdecl _expand(void *_Memory,size_t _NewSize);
90   _CRTIMP size_t __cdecl _msize(void *_Memory);
91 #ifdef __GNUC__
92 #undef _alloca
93 #define _alloca(x) __builtin_alloca((x))
94 #else
95   void *__cdecl _alloca(size_t _Size) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
96 #endif
97   _CRTIMP size_t __cdecl _get_sbh_threshold(void);
98   _CRTIMP int __cdecl _set_sbh_threshold(size_t _NewValue);
99   _CRTIMP errno_t __cdecl _set_amblksiz(size_t _Value);
100   _CRTIMP errno_t __cdecl _get_amblksiz(size_t *_Value);
101   _CRTIMP int __cdecl _heapadd(void *_Memory,size_t _Size);
102   _CRTIMP int __cdecl _heapchk(void);
103   _CRTIMP int __cdecl _heapmin(void);
104   _CRTIMP int __cdecl _heapset(unsigned int _Fill);
105   _CRTIMP int __cdecl _heapwalk(_HEAPINFO *_EntryInfo);
106   _CRTIMP size_t __cdecl _heapused(size_t *_Used,size_t *_Commit);
107   _CRTIMP intptr_t __cdecl _get_heap_handle(void);
108 
109 #define _ALLOCA_S_THRESHOLD 1024
110 #define _ALLOCA_S_STACK_MARKER 0xCCCC
111 #define _ALLOCA_S_HEAP_MARKER 0xDDDD
112 
113 #if defined(_ARM_) || (defined(_X86_) && !defined(__x86_64))
114 #define _ALLOCA_S_MARKER_SIZE 8
115 #elif defined(__ia64__) || defined(__x86_64) || defined(__aarch64__)
116 #define _ALLOCA_S_MARKER_SIZE 16
117 #endif
118 
119 #if !defined(RC_INVOKED)
_MarkAllocaS(void * _Ptr,unsigned int _Marker)120   static __inline void *_MarkAllocaS(void *_Ptr,unsigned int _Marker) {
121     if(_Ptr) {
122       *((unsigned int*)_Ptr) = _Marker;
123       _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE;
124     }
125     return _Ptr;
126   }
127 #endif
128 
129 #undef _malloca
130 #define _malloca(size) \
131   ((((size) + _ALLOCA_S_MARKER_SIZE) <= _ALLOCA_S_THRESHOLD) ? \
132     _MarkAllocaS(_alloca((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_STACK_MARKER) : \
133     _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_HEAP_MARKER))
134 #undef _FREEA_INLINE
135 #define _FREEA_INLINE
136 
137 #ifndef RC_INVOKED
138 #undef _freea
_freea(void * _Memory)139   static __inline void __cdecl _freea(void *_Memory) {
140     unsigned int _Marker;
141     if(_Memory) {
142       _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE;
143       _Marker = *(unsigned int *)_Memory;
144       if(_Marker==_ALLOCA_S_HEAP_MARKER) {
145 	free(_Memory);
146       }
147 #ifdef _ASSERTE
148       else if(_Marker!=_ALLOCA_S_STACK_MARKER) {
149 	_ASSERTE(("Corrupted pointer passed to _freea",0));
150       }
151 #endif
152     }
153   }
154 #endif /* RC_INVOKED */
155 
156 #ifndef	NO_OLDNAMES
157 #undef alloca
158 #ifdef __GNUC__
159 #define alloca(x) __builtin_alloca((x))
160 #else
161 #define alloca _alloca
162 #endif
163 #endif
164 
165 #ifdef HEAPHOOK
166 #ifndef _HEAPHOOK_DEFINED
167 #define _HEAPHOOK_DEFINED
168   typedef int (__cdecl *_HEAPHOOK)(int,size_t,void *,void **);
169 #endif
170 
171   _CRTIMP _HEAPHOOK __cdecl _setheaphook(_HEAPHOOK _NewHook);
172 
173 #define _HEAP_MALLOC 1
174 #define _HEAP_CALLOC 2
175 #define _HEAP_FREE 3
176 #define _HEAP_REALLOC 4
177 #define _HEAP_MSIZE 5
178 #define _HEAP_EXPAND 6
179 #endif
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #pragma pack(pop)
186 
187 #endif /* _MALLOC_H_ */
188