Reactos
at master 187 lines 5.0 kB view raw
1// 2// malloc.h 3// 4// Copyright (c) Microsoft Corporation. All rights reserved. 5// 6// The memory allocation library. 7// 8#pragma once 9#ifndef _INC_MALLOC // include guard for 3rd party interop 10#define _INC_MALLOC 11 12#include <corecrt.h> 13#include <corecrt_malloc.h> 14 15#pragma warning(push) 16#pragma warning(disable: _UCRT_DISABLED_WARNINGS) 17_UCRT_DISABLE_CLANG_WARNINGS 18 19_CRT_BEGIN_C_HEADER 20 21 22 23// Maximum heap request the heap manager will attempt 24#ifdef _WIN64 25 #define _HEAP_MAXREQ 0xFFFFFFFFFFFFFFE0 26#else 27 #define _HEAP_MAXREQ 0xFFFFFFE0 28#endif 29 30 31 32// Constants for _heapchk and _heapwalk routines 33#define _HEAPEMPTY (-1) 34#define _HEAPOK (-2) 35#define _HEAPBADBEGIN (-3) 36#define _HEAPBADNODE (-4) 37#define _HEAPEND (-5) 38#define _HEAPBADPTR (-6) 39#define _FREEENTRY 0 40#define _USEDENTRY 1 41 42typedef struct _heapinfo 43{ 44 int* _pentry; 45 size_t _size; 46 int _useflag; 47} _HEAPINFO; 48 49#define _mm_free(a) _aligned_free(a) 50#define _mm_malloc(a, b) _aligned_malloc(a, b) 51 52 53#if defined(__GNUC__) || defined(__clang__) 54#define _alloca(x) __builtin_alloca((x)) 55#else 56_Ret_notnull_ _Post_writable_byte_size_(_Size) 57void* __cdecl _alloca(_In_ size_t _Size); 58#endif 59 60 61 62#if !defined __midl && !defined RC_INVOKED 63 64 _ACRTIMP intptr_t __cdecl _get_heap_handle(void); 65 66 _Check_return_ 67 _DCRTIMP int __cdecl _heapmin(void); 68 69 #if defined _DEBUG || defined _CRT_USE_WINAPI_FAMILY_DESKTOP_APP || defined _CORECRT_BUILD 70 _ACRTIMP int __cdecl _heapwalk(_Inout_ _HEAPINFO* _EntryInfo); 71 #endif 72 73 #if defined _CRT_USE_WINAPI_FAMILY_DESKTOP_APP || defined _CRT_USE_WINAPI_FAMILY_GAMES 74 _Check_return_ _DCRTIMP int __cdecl _heapchk(void); 75 #endif 76 77 _DCRTIMP int __cdecl _resetstkoflw(void); 78 79 #define _ALLOCA_S_THRESHOLD 1024 80 #define _ALLOCA_S_STACK_MARKER 0xCCCC 81 #define _ALLOCA_S_HEAP_MARKER 0xDDDD 82 83 #ifdef _WIN64 84 #define _ALLOCA_S_MARKER_SIZE 16 85 #else 86 #define _ALLOCA_S_MARKER_SIZE 8 87 #endif 88 89 _STATIC_ASSERT(sizeof(unsigned int) <= _ALLOCA_S_MARKER_SIZE); 90 91 92 #pragma warning(push) 93 #pragma warning(disable: 6540) // C6540: attribute annotations on this function will invalidate all 94 // of its existing __declspec annotations 95 96 __inline void* _MarkAllocaS(_Out_opt_ __crt_typefix(unsigned int*) void* _Ptr, unsigned int _Marker) 97 { 98 if (_Ptr) 99 { 100 *((unsigned int*)_Ptr) = _Marker; 101 _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE; 102 } 103 return _Ptr; 104 } 105 106 __inline size_t _MallocaComputeSize(size_t _Size) 107 { 108 size_t _MarkedSize = _Size + _ALLOCA_S_MARKER_SIZE; 109 return _MarkedSize > _Size ? _MarkedSize : 0; 110 } 111 112 #pragma warning(pop) 113 114#endif 115 116 117 118#ifdef _DEBUG 119// C6255: _alloca indicates failure by raising a stack overflow exception 120// C6386: buffer overrun 121 #ifndef _CRTDBG_MAP_ALLOC 122 #undef _malloca 123 #define _malloca(size) \ 124 __pragma(warning(suppress: 6255 6386)) \ 125 (_MallocaComputeSize(size) != 0 \ 126 ? _MarkAllocaS(malloc(_MallocaComputeSize(size)), _ALLOCA_S_HEAP_MARKER) \ 127 : NULL) 128 #endif 129 130#else 131 132 #undef _malloca 133 #define _malloca(size) \ 134 __pragma(warning(suppress: 6255 6386)) \ 135 (_MallocaComputeSize(size) != 0 \ 136 ? (((_MallocaComputeSize(size) <= _ALLOCA_S_THRESHOLD) \ 137 ? _MarkAllocaS(_alloca(_MallocaComputeSize(size)), _ALLOCA_S_STACK_MARKER) \ 138 : _MarkAllocaS(malloc(_MallocaComputeSize(size)), _ALLOCA_S_HEAP_MARKER))) \ 139 : NULL) 140 141#endif 142 143 144 145#if defined __midl && !defined RC_INVOKED 146#elif defined _DEBUG && defined _CRTDBG_MAP_ALLOC 147#else 148 149 #undef _freea 150 151 #pragma warning(push) 152 #pragma warning(disable: 6014) // leaking memory 153 __inline void __CRTDECL _freea(_Pre_maybenull_ _Post_invalid_ void* _Memory) 154 { 155 unsigned int _Marker; 156 if (_Memory) 157 { 158 _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE; 159 _Marker = *(unsigned int*)_Memory; 160 if (_Marker == _ALLOCA_S_HEAP_MARKER) 161 { 162 free(_Memory); 163 } 164 #ifdef _ASSERTE 165 else if (_Marker != _ALLOCA_S_STACK_MARKER) 166 { 167 _ASSERTE(("Corrupted pointer passed to _freea" && 0)); 168 } 169 #endif 170 } 171 } 172 #pragma warning(pop) 173 174#endif 175 176 177 178#if defined(_CRT_INTERNAL_NONSTDC_NAMES) && _CRT_INTERNAL_NONSTDC_NAMES 179 #define alloca _alloca 180#endif 181 182 183 184_CRT_END_C_HEADER 185_UCRT_RESTORE_CLANG_WARNINGS 186#pragma warning(pop) // _UCRT_DISABLED_WARNINGS 187#endif // _INC_MALLOC