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