1 // 2 // realloc.cpp 3 // 4 // Copyright (c) Microsoft Corporation. All rights reserved. 5 // 6 // Implementation of realloc(). 7 // 8 #include <corecrt_internal.h> 9 #include <malloc.h> 10 11 // Reallocates a block of memory in the heap. 12 // 13 // This function reallocates the block pointed to by 'block' such that it is 14 // 'size' bytes in size. The new size may be either greater or less than the 15 // original size of the block. The reallocation may result in the block being 16 // moved to a new location in memory. If the block is moved, the contents of 17 // the original block are copied. 18 // 19 // Standard behavior notes: 20 // [1] realloc(nullptr, new_size) is equivalent to malloc(new_size) 21 // [2] realloc(p, 0) is equivalent to free(p), and nullptr is returned 22 // [3] If reallocation fails, the original block is left unchanged 23 // 24 // If 'block' is non-null, it must point to a valid block of memory allocated in 25 // the heap. 26 // 27 // This function supports patching and therefore must be marked noinline. 28 // Both _realloc_dbg and _realloc_base must also be marked noinline 29 // to prevent identical COMDAT folding from substituting calls to realloc 30 // with either other function or vice versa. 31 extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline) _CRTRESTRICT void* __cdecl realloc( 32 void* const block, 33 size_t const size 34 ) 35 { 36 #ifdef _DEBUG 37 return _realloc_dbg(block, size, _NORMAL_BLOCK, nullptr, 0); 38 #else 39 return _realloc_base(block, size); 40 #endif 41 } 42