STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
malloc.h
Go to the documentation of this file.
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 #pragma once
16 
17 #ifndef _INC_MALLOC
18 #define _INC_MALLOC
19 
20 #include <crtdefs.h>
21 
22 /*
23  * Currently, all MS C compilers for Win32 platforms default to 8 byte
24  * alignment.
25  */
26 #pragma pack(push,_CRT_PACKING)
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31 
32 /* Maximum heap request the heap manager will attempt */
33 
34 #ifdef _WIN64
35 #define _HEAP_MAXREQ 0xFFFFFFFFFFFFFFE0
36 #else /* _WIN64 */
37 #define _HEAP_MAXREQ 0xFFFFFFE0
38 #endif /* _WIN64 */
39 
40 /* _STATIC_ASSERT is for enforcing boolean/integral conditions at compile time. */
41 
42 #ifndef _STATIC_ASSERT
43 #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ]
44 #endif /* _STATIC_ASSERT */
45 
46 /* Constants for _heapchk/_heapset/_heapwalk routines */
47 
48 #define _HEAPEMPTY (-1)
49 #define _HEAPOK (-2)
50 #define _HEAPBADBEGIN (-3)
51 #define _HEAPBADNODE (-4)
52 #define _HEAPEND (-5)
53 #define _HEAPBADPTR (-6)
54 #define _FREEENTRY 0
55 #define _USEDENTRY 1
56 
57 #ifndef _HEAPINFO_DEFINED
58 typedef struct _heapinfo {
59  int * _pentry;
60  size_t _size;
61  int _useflag;
62  } _HEAPINFO;
63 #define _HEAPINFO_DEFINED
64 #endif /* _HEAPINFO_DEFINED */
65 
66 #define _mm_free(a) _aligned_free(a)
67 #define _mm_malloc(a, b) _aligned_malloc(a, b)
68 
69 /* Function prototypes */
70 
71 #if defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC)
72 #pragma push_macro("calloc")
73 #pragma push_macro("free")
74 #pragma push_macro("malloc")
75 #pragma push_macro("realloc")
76 #pragma push_macro("_recalloc")
77 #pragma push_macro("_aligned_free")
78 #pragma push_macro("_aligned_malloc")
79 #pragma push_macro("_aligned_offset_malloc")
80 #pragma push_macro("_aligned_realloc")
81 #pragma push_macro("_aligned_recalloc")
82 #pragma push_macro("_aligned_offset_realloc")
83 #pragma push_macro("_aligned_offset_recalloc")
84 #pragma push_macro("_aligned_msize")
85 #pragma push_macro("_freea")
86 #undef calloc
87 #undef free
88 #undef malloc
89 #undef realloc
90 #undef _recalloc
91 #undef _aligned_free
92 #undef _aligned_malloc
93 #undef _aligned_offset_malloc
94 #undef _aligned_realloc
95 #undef _aligned_recalloc
96 #undef _aligned_offset_realloc
97 #undef _aligned_offset_recalloc
98 #undef _aligned_msize
99 #undef _freea
100 #endif /* defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC) */
101 
102 #ifndef _CRT_ALLOCATION_DEFINED
103 #define _CRT_ALLOCATION_DEFINED
105 _CRTIMP _CRTNOALIAS void __cdecl free(_Pre_maybenull_ _Post_invalid_ void * _Memory);
107 _Success_(return!=0)
109 _Success_(return!=0)
111 _CRTIMP _CRTNOALIAS void __cdecl _aligned_free(_Pre_maybenull_ _Post_invalid_ void * _Memory);
113 _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Size) _CRTIMP _CRTNOALIAS _CRTRESTRICT void * __cdecl _aligned_offset_malloc(_In_ _CRT_GUARDOVERFLOW size_t _Size, _In_ size_t _Alignment, _In_ size_t _Offset);
114 _Success_(return!=0)
115 _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_NewSize) _CRTIMP _CRTNOALIAS _CRTRESTRICT void * __cdecl _aligned_realloc(_Pre_maybenull_ _Post_invalid_ void * _Memory, _In_ _CRT_GUARDOVERFLOW size_t _NewSize, _In_ size_t _Alignment);
116 _Success_(return!=0)
117 _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count*_Size) _CRTIMP _CRTNOALIAS _CRTRESTRICT void * __cdecl _aligned_recalloc(_Pre_maybenull_ _Post_invalid_ void * _Memory, _In_ _CRT_GUARDOVERFLOW size_t _Count, _In_ _CRT_GUARDOVERFLOW size_t _Size, _In_ size_t _Alignment);
118 _Success_(return!=0)
119 _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_NewSize) _CRTIMP _CRTNOALIAS _CRTRESTRICT void * __cdecl _aligned_offset_realloc(_Pre_maybenull_ _Post_invalid_ void * _Memory, _In_ _CRT_GUARDOVERFLOW size_t _NewSize, _In_ size_t _Alignment, _In_ size_t _Offset);
120 _Success_(return!=0)
121 _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count*_Size) _CRTIMP _CRTNOALIAS _CRTRESTRICT void * __cdecl _aligned_offset_recalloc(_Pre_maybenull_ _Post_invalid_ void * _Memory, _In_ _CRT_GUARDOVERFLOW size_t _Count, _In_ _CRT_GUARDOVERFLOW size_t _Size, _In_ size_t _Alignment, _In_ size_t _Offset);
122 _Check_return_ _CRTIMP size_t __cdecl _aligned_msize(_Pre_notnull_ void * _Memory, _In_ size_t _Alignment, _In_ size_t _Offset);
123 #endif /* _CRT_ALLOCATION_DEFINED */
124 
125 #if defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC)
126 #pragma pop_macro("calloc")
127 #pragma pop_macro("free")
128 #pragma pop_macro("malloc")
129 #pragma pop_macro("realloc")
130 #pragma pop_macro("_recalloc")
131 #pragma pop_macro("_aligned_free")
132 #pragma pop_macro("_aligned_malloc")
133 #pragma pop_macro("_aligned_offset_malloc")
134 #pragma pop_macro("_aligned_realloc")
135 #pragma pop_macro("_aligned_recalloc")
136 #pragma pop_macro("_aligned_offset_realloc")
137 #pragma pop_macro("_aligned_offset_recalloc")
138 #pragma pop_macro("_aligned_msize")
139 #pragma pop_macro("_freea")
140 #endif /* defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC) */
141 
142 #ifdef _CRT_USE_WINAPI_FAMILY_DESKTOP_APP
143 _CRTIMP int __cdecl _resetstkoflw (void);
144 #endif /* _CRT_USE_WINAPI_FAMILY_DESKTOP_APP */
145 
146 #define _MAX_WAIT_MALLOC_CRT 60000
147 
148 _CRTIMP unsigned long __cdecl _set_malloc_crt_max_wait(_In_ unsigned long _NewValue);
149 
150 
151 #if defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC)
152 #pragma push_macro("_expand")
153 #pragma push_macro("_msize")
154 #undef _expand
155 #undef _msize
156 #endif /* defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC) */
157 
158 _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_NewSize) _CRTIMP void * __cdecl _expand(_Pre_notnull_ void * _Memory, _In_ size_t _NewSize);
159 _Check_return_ _CRTIMP size_t __cdecl _msize(_Pre_notnull_ void * _Memory);
160 
161 #if defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC)
162 #pragma pop_macro("_expand")
163 #pragma pop_macro("_msize")
164 #endif /* defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC) */
165 
166 _Ret_notnull_ _Post_writable_byte_size_(_Size) void * __cdecl _alloca(_In_ size_t _Size);
167 
168 #if defined (_DEBUG) || defined (_CRT_USE_WINAPI_FAMILY_DESKTOP_APP)
170 _CRTIMP intptr_t __cdecl _get_heap_handle(void);
171 #endif /* defined (_DEBUG) || defined (_CRT_USE_WINAPI_FAMILY_DESKTOP_APP) */
172 
173 #ifdef _CRT_USE_WINAPI_FAMILY_DESKTOP_APP
174 _Check_return_ _CRTIMP int __cdecl _heapadd(_In_ void * _Memory, _In_ size_t _Size);
175 _Check_return_ _CRTIMP int __cdecl _heapchk(void);
176 _Check_return_ _CRTIMP int __cdecl _heapmin(void);
177 _CRTIMP int __cdecl _heapset(_In_ unsigned int _Fill);
178 _CRTIMP size_t __cdecl _heapused(size_t * _Used, size_t * _Commit);
179 #endif /* _CRT_USE_WINAPI_FAMILY_DESKTOP_APP */
180 
181 #define _ALLOCA_S_THRESHOLD 1024
182 #define _ALLOCA_S_STACK_MARKER 0xCCCC
183 #define _ALLOCA_S_HEAP_MARKER 0xDDDD
184 
185 #if defined (_M_IX86)
186 #define _ALLOCA_S_MARKER_SIZE 8
187 #elif defined (_M_X64)
188 #define _ALLOCA_S_MARKER_SIZE 16
189 #elif defined (_M_ARM)
190 #define _ALLOCA_S_MARKER_SIZE 8
191 #elif !defined (RC_INVOKED)
192 #error Unsupported target platform.
193 #endif /* !defined (RC_INVOKED) */
194 
195 _STATIC_ASSERT(sizeof(unsigned int) <= _ALLOCA_S_MARKER_SIZE);
196 
197 #if !defined (__midl) && !defined (RC_INVOKED)
198 #pragma warning(push)
199 #pragma warning(disable:6540)
200 __inline void *_MarkAllocaS(_Out_opt_ __crt_typefix(unsigned int*) void *_Ptr, unsigned int _Marker)
201 {
202  if (_Ptr)
203  {
204  *((unsigned int*)_Ptr) = _Marker;
205  _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE;
206  }
207  return _Ptr;
208 }
209 
210 __inline int _MallocaIsSizeInRange(size_t size)
211 {
212  return size + _ALLOCA_S_MARKER_SIZE > size;
213 }
214 #pragma warning(pop)
215 #endif /* !defined (__midl) && !defined (RC_INVOKED) */
216 
217 #if defined(_DEBUG)
218 #if !defined(_CRTDBG_MAP_ALLOC)
219 #undef _malloca
220 #define _malloca(size) \
221  __pragma(warning(suppress: 6255)) \
222  (_MallocaIsSizeInRange(size) \
223  ? _MarkAllocaS(malloc((size_t)(size) + _ALLOCA_S_MARKER_SIZE), _ALLOCA_S_HEAP_MARKER) \
224  : NULL)
225 #endif
226 #else
227 #undef _malloca
228 #define _malloca(size) \
229  __pragma(warning(suppress: 6255)) \
230  (_MallocaIsSizeInRange(size) \
231  ? (((((size_t)(size) + _ALLOCA_S_MARKER_SIZE) <= _ALLOCA_S_THRESHOLD) \
232  ? _MarkAllocaS(_alloca((size_t)(size) + _ALLOCA_S_MARKER_SIZE), _ALLOCA_S_STACK_MARKER) \
233  : _MarkAllocaS(malloc((size_t)(size) + _ALLOCA_S_MARKER_SIZE), _ALLOCA_S_HEAP_MARKER))) \
234  : NULL)
235 #endif
236 
237 #undef _FREEA_INLINE
238 #ifdef _CRTBLD
239 #ifndef _CRT_NOFREEA
240 #define _FREEA_INLINE
241 #else /* _CRT_NOFREEA */
242 #undef _FREEA_INLINE
243 #endif /* _CRT_NOFREEA */
244 #else /* _CRTBLD */
245 #define _FREEA_INLINE
246 #endif /* _CRTBLD */
247 
248 #ifdef _FREEA_INLINE
249 /* _freea must be in the header so that its allocator matches _malloca */
250 #if !defined (__midl) && !defined (RC_INVOKED)
251 #if !(defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC))
252 #undef _freea
253 __pragma(warning(push))
254 __pragma(warning(disable: 6014))
255 _CRTNOALIAS __inline void __CRTDECL _freea(_Pre_maybenull_ _Post_invalid_ void * _Memory)
256 {
257  unsigned int _Marker;
258  if (_Memory)
259  {
260  _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE;
261  _Marker = *(unsigned int *)_Memory;
262  if (_Marker == _ALLOCA_S_HEAP_MARKER)
263  {
264  free(_Memory);
265  }
266 #if defined (_ASSERTE)
267  else if (_Marker != _ALLOCA_S_STACK_MARKER)
268  {
269  #pragma warning(suppress: 4548) /* expression before comma has no effect */
270  _ASSERTE(("Corrupted pointer passed to _freea", 0));
271  }
272 #endif /* defined (_ASSERTE) */
273  }
274 }
275 __pragma(warning(pop))
276 #endif /* !(defined (_DEBUG) && defined (_CRTDBG_MAP_ALLOC)) */
277 #endif /* !defined (__midl) && !defined (RC_INVOKED) */
278 #endif /* _FREEA_INLINE */
279 
280 #if !__STDC__
281 /* Non-ANSI names for compatibility */
282 #define alloca _alloca
283 #endif /* !__STDC__ */
284 
285 
286 
287 
288 #ifdef __cplusplus
289 }
290 #endif /* __cplusplus */
291 
292 #pragma pack(pop)
293 
294 #endif /* _INC_MALLOC */
_Check_return_ _Ret_maybenull_ _In_ size_t _In_ size_t _Offset
Definition: malloc.h:113
struct _heapinfo _HEAPINFO
#define _Out_opt_
Definition: sal.h:352
#define _ALLOCA_S_STACK_MARKER
Definition: malloc.h:182
__inline void * _MarkAllocaS(_Out_opt_ __crt_typefix(unsigned int *) void *_Ptr, unsigned int _Marker)
Definition: malloc.h:200
Definition: malloc.h:58
#define _CRTIMP
Definition: crtdefs.h:23
#define _Ret_maybenull_
Definition: sal.h:535
#define _Post_invalid_
Definition: sal.h:701
typedef void(__cdecl *_se_translator_function)(unsigned int
_CRTIMP _CRTNOALIAS void __cdecl free(_Pre_maybenull_ _Post_invalid_ void *_Memory)
_Check_return_ _CRTIMP size_t __cdecl _aligned_msize(_Pre_notnull_ void *_Memory, _In_ size_t _Alignment, _In_ size_t _Offset)
#define _ALLOCA_S_HEAP_MARKER
Definition: malloc.h:183
_Check_return_ _CRTIMP size_t __cdecl _msize(_Pre_notnull_ void *_Memory)
#define _CRT_JIT_INTRINSIC
Definition: crtdefs.h:414
_CRTIMP int __cdecl _resetstkoflw(void)
size_t _size
Definition: malloc.h:60
#define _Pre_maybenull_
Definition: sal.h:687
_In_ _CRT_GUARDOVERFLOW size_t _NewSize
Definition: malloc.h:108
#define _Check_return_
Definition: sal.h:563
__inline int _MallocaIsSizeInRange(size_t size)
Definition: malloc.h:210
#define _ASSERTE(expr)
Definition: crtdbg.h:216
#define _In_
Definition: sal.h:314
_Check_return_ _CRTIMP int __cdecl _heapadd(_In_ void *_Memory, _In_ size_t _Size)
#define _CRTRESTRICT
Definition: crtdefs.h:615
#define __crt_typefix(ctype)
Definition: crtdefs.h:2134
_CRTIMP intptr_t __cdecl _get_heap_handle(void)
_Check_return_ _CRTIMP int __cdecl _heapmin(void)
#define _CRT_GUARDOVERFLOW
Definition: crtdefs.h:248
#define _STATIC_ASSERT(expr)
Definition: malloc.h:43
__pragma(warning(push)) __pragma(warning(disable
Definition: malloc.h:253
int _useflag
Definition: malloc.h:61
#define __CRTDECL
Definition: crtdefs.h:622
_CRTIMP size_t __cdecl _heapused(size_t *_Used, size_t *_Commit)
#define _Ret_notnull_
Definition: sal.h:534
_CRTIMP _CRT_MANAGED_HEAP_DEPRECATE int __cdecl _heapwalk(_Inout_ _HEAPINFO *_EntryInfo)
_In_ _CRT_GUARDOVERFLOW size_t _Count
Definition: malloc.h:110
return(_PAIR_TYPE(_FwdIt)(_First, _First))
#define _CRT_MANAGED_HEAP_DEPRECATE
Definition: crtdefs.h:341
_Check_return_ _CRTIMP int __cdecl _heapchk(void)
#define _Inout_
Definition: sal.h:384
_W64 int intptr_t
Definition: crtdefs.h:512
#define _CRTNOALIAS
Definition: crtdefs.h:611
int * _pentry
Definition: malloc.h:59
_Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_Count *_Size) _CRTIMP _CRT_JIT_INTRINSIC _CRTNOALIAS _CRTRESTRICT void *__cdecl calloc(_In_ _CRT_GUARDOVERFLOW size_t _Count
_CRTIMP _CRTNOALIAS void __cdecl _aligned_free(_Pre_maybenull_ _Post_invalid_ void *_Memory)
_CRTIMP int __cdecl _heapset(_In_ unsigned int _Fill)
#define _Pre_notnull_
Definition: sal.h:686
_Check_return_ _Ret_maybenull_ _In_ _CRT_GUARDOVERFLOW size_t _Size
Definition: malloc.h:104
_CRTIMP unsigned long __cdecl _set_malloc_crt_max_wait(_In_ unsigned long _NewValue)
_Check_return_ _Ret_maybenull_ _In_ size_t _Alignment
Definition: malloc.h:112
_Success_(return!=0) _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(_NewSize) _CRTIMP _CRTNOALIAS _CRTRESTRICT void *__cdecl realloc(_Pre_maybenull_ _Post_invalid_ void *_Memory
void _Fill(_FwdIt _First, _FwdIt _Last, const _Ty &_Val)
Definition: xutility:2597