master
  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
 14extern "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(__cpp_static_assert) && __cpp_static_assert >= 201411L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L)
 25#define _STATIC_ASSERT(expr) static_assert(expr)
 26#elif defined(__cpp_static_assert)
 27#define _STATIC_ASSERT(expr) static_assert(expr, #expr)
 28#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
 29#define _STATIC_ASSERT(expr) _Static_assert(expr, #expr)
 30#elif defined(_MSC_VER)
 31#define _STATIC_ASSERT(expr) typedef char __static_assert_t[(expr)]
 32#else
 33#define _STATIC_ASSERT(expr) extern void __static_assert_t(int [(expr)?1:-1])
 34#endif
 35#endif
 36
 37/* Return codes for _heapwalk()  */
 38#define _HEAPEMPTY (-1)
 39#define _HEAPOK (-2)
 40#define _HEAPBADBEGIN (-3)
 41#define _HEAPBADNODE (-4)
 42#define _HEAPEND (-5)
 43#define _HEAPBADPTR (-6)
 44
 45/* Values for _heapinfo.useflag */
 46#define _FREEENTRY 0
 47#define _USEDENTRY 1
 48
 49#ifndef _HEAPINFO_DEFINED
 50#define _HEAPINFO_DEFINED
 51 /* The structure used to walk through the heap with _heapwalk.  */
 52  typedef struct _heapinfo {
 53    int *_pentry;
 54    size_t _size;
 55    int _useflag;
 56  } _HEAPINFO;
 57#endif
 58
 59#define _amblksiz (*__p__amblksiz())
 60  _CRTIMP unsigned int *__cdecl __p__amblksiz(void);
 61
 62#ifndef _CRT_ALLOCATION_DEFINED
 63#define _CRT_ALLOCATION_DEFINED
 64
 65#if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
 66#pragma push_macro("calloc")
 67#undef calloc
 68#pragma push_macro("free")
 69#undef free
 70#pragma push_macro("malloc")
 71#undef malloc
 72#pragma push_macro("realloc")
 73#undef realloc
 74#pragma push_macro("_aligned_free")
 75#undef _aligned_free
 76#pragma push_macro("_aligned_malloc")
 77#undef _aligned_malloc
 78#pragma push_macro("_aligned_offset_malloc")
 79#undef _aligned_offset_malloc
 80#pragma push_macro("_aligned_realloc")
 81#undef _aligned_realloc
 82#pragma push_macro("_aligned_offset_realloc")
 83#undef _aligned_offset_realloc
 84#pragma push_macro("_recalloc")
 85#undef _recalloc
 86#pragma push_macro("_aligned_recalloc")
 87#undef _aligned_recalloc
 88#pragma push_macro("_aligned_offset_recalloc")
 89#undef _aligned_offset_recalloc
 90#pragma push_macro("_aligned_msize")
 91#undef _aligned_msize
 92#endif
 93
 94  void *__cdecl calloc(size_t _NumOfElements,size_t _SizeOfElements);
 95  void __cdecl free(void *_Memory);
 96  void *__cdecl malloc(size_t _Size);
 97  void *__cdecl realloc(void *_Memory,size_t _NewSize);
 98
 99  _CRTIMP void __cdecl _aligned_free(void *_Memory);
100  _CRTIMP void *__cdecl _aligned_malloc(size_t _Size,size_t _Alignment);
101
102  _CRTIMP void *__cdecl _aligned_offset_malloc(size_t _Size,size_t _Alignment,size_t _Offset);
103  _CRTIMP void *__cdecl _aligned_realloc(void *_Memory,size_t _Size,size_t _Alignment);
104  _CRTIMP void *__cdecl _aligned_offset_realloc(void *_Memory,size_t _Size,size_t _Alignment,size_t _Offset);
105  _CRTIMP void *__cdecl _recalloc(void *_Memory,size_t _Count,size_t _Size);
106  _CRTIMP void *__cdecl _aligned_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment);
107  _CRTIMP void *__cdecl _aligned_offset_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment,size_t _Offset);
108  _CRTIMP size_t __cdecl _aligned_msize(void *_Memory,size_t _Alignment,size_t _Offset);
109
110#if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
111#pragma pop_macro("calloc")
112#pragma pop_macro("free")
113#pragma pop_macro("malloc")
114#pragma pop_macro("realloc")
115#pragma pop_macro("_aligned_free")
116#pragma pop_macro("_aligned_malloc")
117#pragma pop_macro("_aligned_offset_malloc")
118#pragma pop_macro("_aligned_realloc")
119#pragma pop_macro("_aligned_offset_realloc")
120#pragma pop_macro("_recalloc")
121#pragma pop_macro("_aligned_recalloc")
122#pragma pop_macro("_aligned_offset_recalloc")
123#pragma pop_macro("_aligned_msize")
124#endif
125
126#endif
127
128/* Users should really use MS provided versions */
129void * __mingw_aligned_malloc (size_t _Size, size_t _Alignment);
130void __mingw_aligned_free (void *_Memory);
131void * __mingw_aligned_offset_realloc (void *_Memory, size_t _Size, size_t _Alignment, size_t _Offset);
132void * __mingw_aligned_offset_malloc (size_t, size_t, size_t);
133void * __mingw_aligned_realloc (void *_Memory, size_t _Size, size_t _Offset);
134size_t __mingw_aligned_msize (void *memblock, size_t alignment, size_t offset);
135
136#if defined(__x86_64__) || defined(__i386__)
137/* Get the compiler's definition of _mm_malloc and _mm_free. */
138#include <mm_malloc.h>
139#endif
140
141#define _MAX_WAIT_MALLOC_CRT 60000
142
143#ifdef _CRT_USE_WINAPI_FAMILY_DESKTOP_APP
144  _CRTIMP int __cdecl _resetstkoflw (void);
145#endif /* _CRT_USE_WINAPI_FAMILY_DESKTOP_APP */
146  _CRTIMP unsigned long __cdecl _set_malloc_crt_max_wait(unsigned long _NewValue);
147
148#if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
149#pragma push_macro("_expand")
150#undef _expand
151#pragma push_macro("_msize")
152#undef _msize
153#endif
154  _CRTIMP void *__cdecl _expand(void *_Memory,size_t _NewSize);
155  _CRTIMP size_t __cdecl _msize(void *_Memory);
156#if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
157#pragma pop_macro("_expand")
158#pragma pop_macro("_msize")
159#endif
160
161#ifdef __GNUC__
162#undef _alloca
163#define _alloca(x) __builtin_alloca((x))
164#else
165  void *__cdecl _alloca(size_t _Size) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
166#endif
167  _CRTIMP size_t __cdecl _get_sbh_threshold(void);
168  _CRTIMP int __cdecl _set_sbh_threshold(size_t _NewValue);
169  _CRTIMP errno_t __cdecl _set_amblksiz(size_t _Value);
170  _CRTIMP errno_t __cdecl _get_amblksiz(size_t *_Value);
171  _CRTIMP int __cdecl _heapadd(void *_Memory,size_t _Size);
172  _CRTIMP int __cdecl _heapchk(void);
173  _CRTIMP int __cdecl _heapmin(void);
174  _CRTIMP int __cdecl _heapset(unsigned int _Fill);
175  _CRTIMP int __cdecl _heapwalk(_HEAPINFO *_EntryInfo);
176  _CRTIMP size_t __cdecl _heapused(size_t *_Used,size_t *_Commit);
177  _CRTIMP intptr_t __cdecl _get_heap_handle(void);
178
179#define _ALLOCA_S_THRESHOLD 1024
180#define _ALLOCA_S_STACK_MARKER 0xCCCC
181#define _ALLOCA_S_HEAP_MARKER 0xDDDD
182
183#if defined(_ARM_) || (defined(_X86_) && !defined(__x86_64))
184#define _ALLOCA_S_MARKER_SIZE 8
185#elif defined(__ia64__) || defined(__x86_64) || defined(__aarch64__)
186#define _ALLOCA_S_MARKER_SIZE 16
187#endif
188
189#if !defined(RC_INVOKED)
190  static __inline void *_MarkAllocaS(void *_Ptr,unsigned int _Marker) {
191    if(_Ptr) {
192      *((unsigned int*)_Ptr) = _Marker;
193      _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE;
194    }
195    return _Ptr;
196  }
197#endif
198
199#ifdef _DEBUG
200#ifndef _CRTDBG_MAP_ALLOC
201#undef _malloca
202#define _malloca(size) \
203    _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE), _ALLOCA_S_HEAP_MARKER)
204#endif
205#else
206#undef _malloca
207#define _malloca(size) \
208  ((((size) + _ALLOCA_S_MARKER_SIZE) <= _ALLOCA_S_THRESHOLD) ? \
209    _MarkAllocaS(_alloca((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_STACK_MARKER) : \
210    _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_HEAP_MARKER))
211#endif
212
213#undef _FREEA_INLINE
214#define _FREEA_INLINE
215
216#ifndef RC_INVOKED
217#undef _freea
218  static __inline void __cdecl _freea(void *_Memory) {
219    unsigned int _Marker;
220    if(_Memory) {
221      _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE;
222      _Marker = *(unsigned int *)_Memory;
223      if(_Marker==_ALLOCA_S_HEAP_MARKER) {
224	free(_Memory);
225      }
226#ifdef _ASSERTE
227      else if(_Marker!=_ALLOCA_S_STACK_MARKER) {
228	_ASSERTE(("Corrupted pointer passed to _freea",0));
229      }
230#endif
231    }
232  }
233#endif /* RC_INVOKED */
234
235#ifndef	NO_OLDNAMES
236#undef alloca
237#ifdef __GNUC__
238#define alloca(x) __builtin_alloca((x))
239#else
240#define alloca _alloca
241#endif
242#endif
243
244#ifdef HEAPHOOK
245#ifndef _HEAPHOOK_DEFINED
246#define _HEAPHOOK_DEFINED
247  typedef int (__cdecl *_HEAPHOOK)(int,size_t,void *,void **);
248#endif
249
250  _CRTIMP _HEAPHOOK __cdecl _setheaphook(_HEAPHOOK _NewHook);
251
252#define _HEAP_MALLOC 1
253#define _HEAP_CALLOC 2
254#define _HEAP_FREE 3
255#define _HEAP_REALLOC 4
256#define _HEAP_MSIZE 5
257#define _HEAP_EXPAND 6
258#endif
259
260#ifdef __cplusplus
261}
262#endif
263
264#pragma pack(pop)
265
266#endif /* _MALLOC_H_ */