Function core.memory.GC.realloc
Extend, shrink or allocate a new block of memory keeping the contents of an existing block
If sz
is zero, the memory referenced by p will be deallocated as if
by a call to free
.
If p
is null
, new memory will be allocated via malloc
.
If p
is pointing to memory not allocated from the GC or to the interior
of an allocated memory block, no operation is performed and null is returned.
Otherwise, a new memory block of size sz
will be allocated as if by a
call to malloc
, or the implementation may instead resize or shrink 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.
The caller guarantees that there are no other live pointers to the
passed memory block, still it might not be freed immediately by realloc
.
The garbage collector can reclaim the memory block in a later
collection if it is unused.
If allocation fails, this function will throw an OutOfMemoryError
.
If ba
is zero (the default) the attributes of the existing memory
will be used for an allocation.
If ba
is not zero and no new memory is allocated, the bits in ba will
replace those of the current memory block.
Parameters
Name | Description |
---|---|
p | A pointer to the base of a valid memory block or to null . |
sz | The desired allocation size in bytes. |
ba | A bitmask of the BlkAttr 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 or the pointer does not point to the base of an GC allocated
memory block.
Throws
OutOfMemoryError
on allocation failure.
Example
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);
GC .BlkInfo info = GC .query(data2);
assert(info .size >= size2);
Authors
Sean Kelly, Alex Rønne Petersen