1 /***
2 *malloc.h - declarations and definitions for memory allocation functions
3 *
4 *           Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 *           Contains the function declarations for memory allocation functions;
8 *           also defines manifest constants and types used by the heap routines.
9 *           [System V]
10 *
11 *           [Public]
12 *
13 ****/
14
15 #if        _MSC_VER > 1000
16 #pragma once
17 #endif
18
19 #ifndef _INC_MALLOC
20 #define _INC_MALLOC
21
22 #include <crtdefs.h>
23
24 #ifdef  _MSC_VER
25 /*
26  * Currently, all MS C compilers for Win32 platforms default to 8 byte
27  * alignment.
28  */
29 #pragma pack(push,_CRT_PACKING)
30 #endif  /* _MSC_VER */
31
32 #ifdef  __cplusplus
33 extern "C" {
34 #endif
35
36 /* Maximum heap request the heap manager will attempt */
37
38 #ifdef  _WIN64
39 #define _HEAP_MAXREQ      0xFFFFFFFFFFFFFFE0
40 #else
41 #define _HEAP_MAXREQ      0xFFFFFFE0
42 #endif
43
44 /* _STATIC_ASSERT is for enforcing boolean/integral conditions at compile time. */
45
46 #ifndef _STATIC_ASSERT
47 #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ]
48 #endif
49
50 /* Constants for _heapchk/_heapset/_heapwalk routines */
51
52 #define _HEAPEMPTY          (-1)
53 #define _HEAPOK               (-2)
54 #define _HEAPBADBEGIN     (-3)
55 #define _HEAPBADNODE      (-4)
56 #define _HEAPEND             (-5)
57 #define _HEAPBADPTR        (-6)
58 #define _FREEENTRY          0
59 #define _USEDENTRY          1
60
61 #ifndef _HEAPINFO_DEFINED
62 typedef struct _heapinfo {
63              int * _pentry;
64              size_t _size;
65              int _useflag;
66              } _HEAPINFO;
67 #define _HEAPINFO_DEFINED
68 #endif
69
70 /* External variable declarations */
71
72 #if !defined(_M_CEE_PURE)
73 extern _CRT_INSECURE_DEPRECATE_GLOBALS(_get_amblksiz) unsigned int _amblksiz;
74 #else
75 _CRT_INSECURE_DEPRECATE_GLOBALS(_get_amblksiz) _CRTIMP unsigned int * __cdecl __p__amblksiz(void);
76 #define _amblksiz (*__p__amblksiz())
77 #endif /* !defined(_M_CEE_PURE) */
78
79 #define _mm_free(a)          _aligned_free(a)
80 #define _mm_malloc(a, b)      _aligned_malloc(a, b)
81
82 /* Function prototypes */
83
84 #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
85 #pragma push_macro("calloc")
86 #pragma push_macro("free")
87 #pragma push_macro("malloc")
88 #pragma push_macro("realloc")
89 #pragma push_macro("_recalloc")
90 #pragma push_macro("_aligned_free")
91 #pragma push_macro("_aligned_malloc")
92 #pragma push_macro("_aligned_offset_malloc")
93 #pragma push_macro("_aligned_realloc")
94 #pragma push_macro("_aligned_recalloc")
Lines 95 ... 104 are skipped.
105 #undef _aligned_malloc
106 #undef _aligned_offset_malloc
107 #undef _aligned_realloc
108 #undef _aligned_recalloc
109 #undef _aligned_offset_realloc
110 #undef _aligned_offset_recalloc
111 #undef _aligned_msize
112 #undef _freea
113 #endif
114
115 #ifndef _CRT_ALLOCATION_DEFINED
116 #define _CRT_ALLOCATION_DEFINED
117 _Check_return_ _Ret_opt_bytecap_x_(_Count*_Size) _CRTIMP _CRT_JIT_INTRINSIC _CRTNOALIAS _CRTRESTRICT      void * __cdecl calloc(_In_ size_t _Count, _In_ size_t _Size);
118 _CRTIMP                                   _CRTNOALIAS                                                                                                                                void     __cdecl free(_Inout_opt_ void * _Memory);
119 _Check_return_ _Ret_opt_bytecap_(_Size) _CRTIMP _CRT_JIT_INTRINSIC _CRTNOALIAS _CRTRESTRICT                                                  void * __cdecl malloc(_In_ size_t _Size);
120 _Check_return_ _Ret_opt_bytecap_(_NewSize) _CRTIMP _CRTNOALIAS _CRTRESTRICT                                             void * __cdecl realloc(_In_opt_ void * _Memory, _In_ size_t _NewSize);
121 _Check_return_ _Ret_opt_bytecap_x_(_Count*_Size) _CRTIMP _CRTNOALIAS _CRTRESTRICT                                      void * __cdecl _recalloc(_In_opt_ void * _Memory, _In_ size_t _Count, _In_ size_t _Size);
122 _CRTIMP                                   _CRTNOALIAS                                                                                                                                void     __cdecl _aligned_free(_Inout_opt_ void * _Memory);
123 _Check_return_ _Ret_opt_bytecap_(_Size) _CRTIMP _CRTNOALIAS _CRTRESTRICT                                                  void * __cdecl _aligned_malloc(_In_ size_t _Size, _In_ size_t _Alignment);
124 _Check_return_ _Ret_opt_bytecap_(_Size) _CRTIMP _CRTNOALIAS _CRTRESTRICT                                                  void * __cdecl _aligned_offset_malloc(_In_ size_t _Size, _In_ size_t _Alignment, _In_ size_t _Offset);
125 _Check_return_ _Ret_opt_bytecap_(_NewSize) _CRTIMP _CRTNOALIAS _CRTRESTRICT                                                  void * __cdecl _aligned_realloc(_In_opt_ void * _Memory, _In_ size_t _NewSize, _In_ size_t _Alignment);
126 _Check_return_ _Ret_opt_bytecap_x_(_Count*_Size) _CRTIMP _CRTNOALIAS _CRTRESTRICT                                      void * __cdecl _aligned_recalloc(_In_opt_ void * _Memory, _In_ size_t _Count, _In_ size_t _Size, _In_ size_t _Alignment);
127 _Check_return_ _Ret_opt_bytecap_(_NewSize) _CRTIMP _CRTNOALIAS _CRTRESTRICT                                                  void * __cdecl _aligned_offset_realloc(_In_opt_ void * _Memory, _In_ size_t _NewSize, _In_ size_t _Alignment, _In_ size_t _Offset);
128 _Check_return_ _Ret_opt_bytecap_x_(_Count*_Size) _CRTIMP _CRTNOALIAS _CRTRESTRICT                                      void * __cdecl _aligned_offset_recalloc(_In_opt_ void * _Memory, _In_ size_t _Count, _In_ size_t _Size, _In_ size_t _Alignment, _In_ size_t _Offset);
129 _Check_return_ _CRTIMP                                                                                   size_t __cdecl _aligned_msize(_In_ void * _Memory, _In_ size_t _Alignment, _In_ size_t _Offset);
130 #endif
131
132 #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
133 #pragma pop_macro("calloc")
134 #pragma pop_macro("free")
135 #pragma pop_macro("malloc")
136 #pragma pop_macro("realloc")
137 #pragma pop_macro("_recalloc")
138 #pragma pop_macro("_aligned_free")
139 #pragma pop_macro("_aligned_malloc")
140 #pragma pop_macro("_aligned_offset_malloc")
141 #pragma pop_macro("_aligned_realloc")
142 #pragma pop_macro("_aligned_recalloc")
143 #pragma pop_macro("_aligned_offset_realloc")
144 #pragma pop_macro("_aligned_offset_recalloc")
145 #pragma pop_macro("_aligned_msize")
146 #pragma pop_macro("_freea")
147 #endif
148
149 _CRTIMP int        __cdecl _resetstkoflw (void);
150
151 #define _MAX_WAIT_MALLOC_CRT 60000
152
153 _CRTIMP unsigned long __cdecl _set_malloc_crt_max_wait(_In_ unsigned long _NewValue);
154
155 #ifndef _POSIX_
156
157 #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
158 #pragma push_macro("_expand")
159 #pragma push_macro("_msize")
160 #undef _expand
161 #undef _msize
162 #endif
163
164 _Check_return_ _Ret_opt_bytecap_(_NewSize) _CRTIMP void *  __cdecl _expand(_In_opt_ void * _Memory, _In_ size_t _NewSize);
165 _Check_return_ _CRTIMP size_t  __cdecl _msize(_In_ void * _Memory);
166
167 #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
168 #pragma pop_macro("_expand")
169 #pragma pop_macro("_msize")
170 #endif
171
172 _Check_return_ _Ret_bytecap_(_Size) void *                __cdecl _alloca(_In_ size_t _Size);
173 _Check_return_ _CRTIMP size_t  __cdecl _get_sbh_threshold(void);
174 _CRTIMP int        __cdecl _set_sbh_threshold(_In_ size_t _NewValue);
175 _CRTIMP errno_t __cdecl _set_amblksiz(_In_ size_t _Value);
176 _CRTIMP errno_t __cdecl _get_amblksiz(_Out_ size_t * _Value);
177 _Check_return_ _CRTIMP int        __cdecl _heapadd(_In_ void * _Memory, _In_ size_t _Size);
178 _Check_return_ _CRTIMP int        __cdecl _heapchk(void);
179 _Check_return_ _CRTIMP int        __cdecl _heapmin(void);
180 _CRTIMP int        __cdecl _heapset(_In_ unsigned int _Fill);
181 _CRTIMP _CRT_MANAGED_HEAP_DEPRECATE int        __cdecl _heapwalk(_Inout_ _HEAPINFO * _EntryInfo);
182 _CRTIMP size_t  __cdecl _heapused(size_t * _Used, size_t * _Commit);
183
184 _CRTIMP intptr_t __cdecl _get_heap_handle(void);
185
186 #define _ALLOCA_S_THRESHOLD        1024
187 #define _ALLOCA_S_STACK_MARKER  0xCCCC
188 #define _ALLOCA_S_HEAP_MARKER     0xDDDD
189
190 #if defined(_M_IX86)
191 #define _ALLOCA_S_MARKER_SIZE     8
192 #elif defined(_M_IA64)
193 #define _ALLOCA_S_MARKER_SIZE     16
194 #elif defined(_M_AMD64)
195 #define _ALLOCA_S_MARKER_SIZE     16
196 #endif
197
198 _STATIC_ASSERT(sizeof(unsigned int) <= _ALLOCA_S_MARKER_SIZE);
199
200 #if !defined(__midl) && !defined(RC_INVOKED)
201 #pragma warning(push)
202 #pragma warning(disable:6540)
203 __inline void *_MarkAllocaS(_Out_opt_ __crt_typefix(unsigned int*) void *_Ptr, unsigned int _Marker)
204 {
205       if (_Ptr)
206       {
207              *((unsigned int*)_Ptr) = _Marker;
208              _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE;
209       }
210       return _Ptr;
211 }
212 #pragma warning(pop)
213 #endif
214
215 #if defined(_DEBUG)
216 #if !defined(_CRTDBG_MAP_ALLOC)
217 #undef _malloca
218 #define _malloca(size) \
219 __pragma(warning(suppress: 6255)) \
220              _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE), _ALLOCA_S_HEAP_MARKER)
221 #endif
222 #else
223 #undef _malloca
224 #define _malloca(size) \
225 __pragma(warning(suppress: 6255)) \
226       ((((size) + _ALLOCA_S_MARKER_SIZE) <= _ALLOCA_S_THRESHOLD) ? \
227              _MarkAllocaS(_alloca((size) + _ALLOCA_S_MARKER_SIZE), _ALLOCA_S_STACK_MARKER) : \
228              _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE), _ALLOCA_S_HEAP_MARKER))
229 #endif
230
231 #undef _FREEA_INLINE
232 #define _FREEA_INLINE
233
234 #ifdef _FREEA_INLINE
235 /* _freea must be in the header so that its allocator matches _malloca */
236 #if !defined(__midl) && !defined(RC_INVOKED)
237 #undef _freea
238 _CRTNOALIAS __inline void __CRTDECL _freea(_Inout_opt_ void * _Memory)
239 {
240       unsigned int _Marker;
241       if (_Memory)
242       {
243              _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE;
244              _Marker = *(unsigned int *)_Memory;
245              if (_Marker == _ALLOCA_S_HEAP_MARKER)
246              {
247                     free(_Memory);
248              }
249 #if defined(_ASSERTE)
250              else if (_Marker != _ALLOCA_S_STACK_MARKER)
251              {
252                     _ASSERTE(("Corrupted pointer passed to _freea", 0));
253              }
254 #endif
255       }
256 }
257 #endif
258 #endif
259
260 #if        !__STDC__
261 /* Non-ANSI names for compatibility */
262 #define alloca  _alloca
263 #endif  /* __STDC__*/
264
265 #endif  /* _POSIX_ */
266
267 #ifdef  HEAPHOOK
268 #ifndef _HEAPHOOK_DEFINED
269 /* hook function type */
270 typedef int (__cdecl * _HEAPHOOK)(int, size_t, void *, void **);
271 #define _HEAPHOOK_DEFINED
272 #endif  /* _HEAPHOOK_DEFINED */
273
274 /* set hook function */
275 _CRTIMP _HEAPHOOK __cdecl _setheaphook(_In_opt_ _HEAPHOOK _NewHook);
276
277 /* hook function must handle these types */
278 #define _HEAP_MALLOC      1
279 #define _HEAP_CALLOC      2
280 #define _HEAP_FREE          3
281 #define _HEAP_REALLOC     4
282 #define _HEAP_MSIZE        5
283 #define _HEAP_EXPAND      6
284 #endif  /* HEAPHOOK */
285
286
287 #ifdef  __cplusplus
288 }
289 #endif
290
291 #ifdef  _MSC_VER
292 #pragma pack(pop)
293 #endif  /* _MSC_VER */
294
295 #endif  /* _INC_MALLOC */
296