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.extend

Requests that the managed memory block referenced by p be extended in place by at least mx bytes, with a desired extension of sz bytes. If an extension of the required size is not possible or if p references memory not originally allocated by this garbage collector, no action will be taken.

static ulong extend (
  void* p,
  ulong mx,
  ulong sz,
  const(TypeInfo) ti = null
) pure;

Parameters

NameDescription
p A pointer to the root of a valid memory block or to null.
mx The minimum extension size in bytes.
sz The desired extension size in bytes.
ti TypeInfo to describe the full memory block. The GC might use this information to improve scanning for pointers or to call finalizers.

Returns

The size in bytes of the extended memory block referenced by p or zero if no extension occurred.

Note

Extend may also be used to extend slices (or memory blocks with APPENDABLE info). However, use the return value only as an indicator of success. capacity should be used to retrieve actual usable slice capacity.

Example

Standard extending

size_t size = 1000;
int* p = cast(int*)GC.malloc(size * int.sizeof, GC.BlkAttr.NO_SCAN);

//Try to extend the allocated data by 1000 elements, preferred 2000.
size_t u = GC.extend(p, 1000 * int.sizeof, 2000 * int.sizeof);
if (u != 0)
    size = u / int.sizeof;

Example

slice extending

int[] slice = new int[](1000);
int*  p     = slice.ptr;

//Check we have access to capacity before attempting the extend
if (slice.capacity)
{
    //Try to extend slice by 1000 elements, preferred 2000.
    size_t u = GC.extend(p, 1000 * int.sizeof, 2000 * int.sizeof);
    if (u != 0)
    {
        slice.length = slice.capacity;
        assert(slice.length >= 2000);
    }
}

Authors

Sean Kelly, Alex Rønne Petersen

License

Boost License 1.0