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
3 //
4 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // The memory allocation library.
7 //
8 #pragma once
9 #define _INC_MALLOC
10 
11 #include <corecrt.h>
12 #include <corecrt_malloc.h>
13 
15 
16 
17 
18 // Maximum heap request the heap manager will attempt
19 #ifdef _WIN64
20  #define _HEAP_MAXREQ 0xFFFFFFFFFFFFFFE0
21 #else
22  #define _HEAP_MAXREQ 0xFFFFFFE0
23 #endif
24 
25 
26 
27 // Constants for _heapchk and _heapwalk routines
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 #define _FREEENTRY 0
35 #define _USEDENTRY 1
36 
37 
38 
39 typedef struct _heapinfo
40 {
41  int* _pentry;
42  size_t _size;
43  int _useflag;
44 } _HEAPINFO;
45 
46 
47 
48 #define _mm_free(a) _aligned_free(a)
49 #define _mm_malloc(a, b) _aligned_malloc(a, b)
50 
51 
52 
54 void* __cdecl _alloca(_In_ size_t _Size);
55 
56 
57 
58 #if !defined __midl && !defined RC_INVOKED
59 
60  _ACRTIMP intptr_t __cdecl _get_heap_handle(void);
61 
63  _DCRTIMP int __cdecl _heapmin(void);
64 
65  #if defined _DEBUG || defined _CRT_USE_WINAPI_FAMILY_DESKTOP_APP || defined _CORECRT_BUILD
66  _ACRTIMP int __cdecl _heapwalk(_Inout_ _HEAPINFO* _EntryInfo);
67  #endif
68 
69  #ifdef _CRT_USE_WINAPI_FAMILY_DESKTOP_APP
70  _Check_return_ _DCRTIMP int __cdecl _heapchk(void);
71  #endif
72 
73  _DCRTIMP int __cdecl _resetstkoflw(void);
74 
75  #define _ALLOCA_S_THRESHOLD 1024
76  #define _ALLOCA_S_STACK_MARKER 0xCCCC
77  #define _ALLOCA_S_HEAP_MARKER 0xDDDD
78 
79  #ifdef _WIN64
80  #define _ALLOCA_S_MARKER_SIZE 16
81  #else
82  #define _ALLOCA_S_MARKER_SIZE 8
83  #endif
84 
85  _STATIC_ASSERT(sizeof(unsigned int) <= _ALLOCA_S_MARKER_SIZE);
86 
87 
88  #pragma warning(push)
89  #pragma warning(disable:6540)
90 
91  __inline void* _MarkAllocaS(_Out_opt_ __crt_typefix(unsigned int*) void* _Ptr, unsigned int _Marker)
92  {
93  if (_Ptr)
94  {
95  *((unsigned int*)_Ptr) = _Marker;
96  _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE;
97  }
98  return _Ptr;
99  }
100 
101  __inline size_t _MallocaComputeSize(size_t _Size)
102  {
103  size_t _MarkedSize = _Size + _ALLOCA_S_MARKER_SIZE;
104  return _MarkedSize > _Size ? _MarkedSize : 0;
105  }
106 
107  #pragma warning(pop)
108 
109 #endif
110 
111 
112 
113 #ifdef _DEBUG
114 
115  #ifndef _CRTDBG_MAP_ALLOC
116  #undef _malloca
117  #define _malloca(size) \
118  __pragma(warning(suppress: 6255 6386)) \
119  (_MallocaComputeSize(size) != 0 \
120  ? _MarkAllocaS(malloc(_MallocaComputeSize(size)), _ALLOCA_S_HEAP_MARKER) \
121  : NULL)
122  #endif
123 
124 #else
125 
126  #undef _malloca
127  #define _malloca(size) \
128  __pragma(warning(suppress: 6255 6386)) \
129  (_MallocaComputeSize(size) != 0 \
130  ? (((_MallocaComputeSize(size) <= _ALLOCA_S_THRESHOLD) \
131  ? _MarkAllocaS(_alloca(_MallocaComputeSize(size)), _ALLOCA_S_STACK_MARKER) \
132  : _MarkAllocaS(malloc(_MallocaComputeSize(size)), _ALLOCA_S_HEAP_MARKER))) \
133  : NULL)
134 
135 #endif
136 
137 
138 
139 #if defined __midl && !defined RC_INVOKED
140 #elif defined _DEBUG && defined _CRTDBG_MAP_ALLOC
141 #else
142 
143  #undef _freea
144 
145  #pragma warning(push)
146  #pragma warning(disable: 6014)
147  __inline void __CRTDECL _freea(_Pre_maybenull_ _Post_invalid_ void* _Memory)
148  {
149  unsigned int _Marker;
150  if (_Memory)
151  {
152  _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE;
153  _Marker = *(unsigned int*)_Memory;
154  if (_Marker == _ALLOCA_S_HEAP_MARKER)
155  {
156  free(_Memory);
157  }
158  #ifdef _ASSERTE
159  else if (_Marker != _ALLOCA_S_STACK_MARKER)
160  {
161  _ASSERTE(("Corrupted pointer passed to _freea" && 0));
162  }
163  #endif
164  }
165  }
166  #pragma warning(pop)
167 
168 #endif
169 
170 
171 
172 #if _CRT_INTERNAL_NONSTDC_NAMES
173  #define alloca _alloca
174 #endif
175 
176 
177 
#define _ACRTIMP
Definition: corecrt.h:27
struct _heapinfo _HEAPINFO
#define _ALLOCA_S_MARKER_SIZE
Definition: malloc.h:82
_ACRTIMP int __cdecl _heapwalk(_Inout_ _HEAPINFO *_EntryInfo)
#define _Out_opt_
Definition: sal.h:343
_ACRTIMP void __cdecl free(_Pre_maybenull_ _Post_invalid_ void *_Block)
#define _ALLOCA_S_STACK_MARKER
Definition: malloc.h:76
__inline void * _MarkAllocaS(_Out_opt_ __crt_typefix(unsigned int *) void *_Ptr, unsigned int _Marker)
Definition: malloc.h:91
Definition: malloc.h:39
_Ret_notnull_ _Post_writable_byte_size_(_Size) void *__cdecl _alloca(_In_ size_t _Size)
#define __crt_typefix(ctype)
Definition: corecrt.h:108
_Check_return_ _DCRTIMP int __cdecl _heapmin(void)
#define _Post_invalid_
Definition: sal.h:692
_DCRTIMP int __cdecl _resetstkoflw(void)
__inline void __CRTDECL _freea(_Pre_maybenull_ _Post_invalid_ void *_Memory)
Definition: malloc.h:147
_CRT_BEGIN_C_HEADER _Check_return_ _Ret_maybenull_ _In_ size_t _Size
Definition: corecrt_malloc.h:58
#define _CRT_BEGIN_C_HEADER
Definition: vcruntime.h:73
_Check_return_ _DCRTIMP int __cdecl _heapchk(void)
__inline size_t _MallocaComputeSize(size_t _Size)
Definition: malloc.h:101
#define _ALLOCA_S_HEAP_MARKER
Definition: malloc.h:77
#define __CRTDECL
Definition: vcruntime.h:156
size_t _size
Definition: malloc.h:42
#define _Pre_maybenull_
Definition: sal.h:678
#define _Check_return_
Definition: sal.h:554
#define _ASSERTE(expr)
Definition: crtdbg.h:707
#define _In_
Definition: sal.h:305
#define _DCRTIMP
Definition: corecrt.h:43
_CRT_BEGIN_C_HEADER typedef void(__CRTDECL *terminate_handler)()
int _useflag
Definition: malloc.h:43
int intptr_t
Definition: vcruntime.h:200
#define _CRT_END_C_HEADER
Definition: vcruntime.h:76
#define _Ret_notnull_
Definition: sal.h:525
_STATIC_ASSERT(sizeof(unsigned int)<=_ALLOCA_S_MARKER_SIZE)
#define _Inout_
Definition: sal.h:375
int * _pentry
Definition: malloc.h:41
_ACRTIMP intptr_t __cdecl _get_heap_handle(void)