View source code
Display the source code in core/memory.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function core.memory.GC.realloc

If sz is zero, the memory referenced by p will be deallocated as if by a call to free. A new memory block of size sz will then be allocated as if by a call to malloc, or the implementation may instead resize the memory block in place. The contents of the new memory block will be the same as the contents of the old memory block, up to the lesser of the new and old sizes. Note that existing memory will only be freed by realloc if sz is equal to zero. The garbage collector is otherwise expected to later reclaim the memory block if it is unused. If allocation fails, this function will call onOutOfMemory which is expected to throw an OutOfMemoryError. If p references memory not originally allocated by this garbage collector, or if it points to the interior of a memory block, no action will be taken. If ba is zero (the default) and p references the head of a valid, known memory block then any bits set on the current block will be set on the new block if a reallocation is required. If ba is not zero and p references the head of a valid, known memory block then the bits in ba will replace those on the current memory block and will also be set on the new block if a reallocation is required.

static void* realloc (
  void* p,
  ulong sz,
  uint ba = 0u,
  const(TypeInfo) ti = null
) pure;

Parameters

NameDescription
p A pointer to the root of a valid memory block or to null.
sz The desired allocation size in bytes.
ba A bitmask of the attributes to set on this block.
ti TypeInfo to describe the memory. The GC might use this information to improve scanning for pointers or to call finalizers.

Returns

A reference to the allocated memory on success or null if sz is zero. On failure, the original value of p is returned.

Throws

OutOfMemoryError on allocation failure.

Example

Issue 13111

enum size1 = 1 << 11 + 1; // page in large object pool
enum size2 = 1 << 22 + 1; // larger than large object pool size

auto data1 = cast(ubyte*)GC.calloc(size1);
auto data2 = cast(ubyte*)GC.realloc(data1, size2);

BlkInfo info = query(data2);
assert(info.size >= size2);

Authors

Sean Kelly, Alex Rønne Petersen

License

Boost License 1.0