| void* p | A pointer to the root of a valid memory block or to null. |
| void* p | A pointer to the root of a valid memory block or to null. |
| uint a | A bit field containing any bits to set for this memory block. |
| void* p | A pointer to the root of a valid memory block or to null. |
| uint a | A bit field containing any bits to clear for this memory block. |
| size_t sz | The desired allocation size in bytes. |
| uint ba | A bitmask of the attributes to set on this block. |
| size_t sz | The desired allocation size in bytes. |
| uint ba | A bitmask of the attributes to set on this block. |
| size_t sz | The desired allocation size in bytes. |
| uint ba | A bitmask of the attributes to set on this block. |
| void* p | A pointer to the root of a valid memory block or to null. |
| size_t sz | The desired allocation size in bytes. |
| uint ba | A bitmask of the attributes to set on this block. |
| size_t mx | The minimum extension size in bytes. |
| size_t sz | The desired extension size in bytes. |
| size_t sz | The desired size in bytes. |
| void* p | A pointer to the root of a valid memory block or to null. |
| inout(void)* p | A pointer to the root or the interior of a valid memory block or to null. |
| void* p | A pointer to the root of a valid memory block or to null. |
| void* p | A pointer to the root or the interior of a valid memory block or to null. |
| void* p | A pointer into a GC-managed memory block or null. |
// Typical C-style callback mechanism; the passed function
// is invoked with the user-supplied context pointer at a
// later point.
extern(C) void addCallback(void function(void*), void*);
// Allocate an object on the GC heap (this would usually be
) // some application-specific context data.
auto context = new Object;
// Make sure that it is not collected even if it is no
// longer referenced from D code (stack, GC heap, …).
GC.addRoot(cast(void*)context);
// Also ensure that a moving collector does not relocate
// the object.
GC.setAttr(cast(void*)context, GC.BlkAttr.NO_MOVE);
// Now context can be safely passed to the C library.
addCallback(&myHandler, cast(void*)context);
extern(C) void myHandler(void* ctx)
{
// Assuming that the callback is invoked only once, the
// added root can be removed again now to allow the GC
// to collect it later.
GC.removeRoot(ctx);
GC.clrAttr(ctx, GC.BlkAttr.NO_MOVE);
auto context = cast(Object)ctx;
// Use context here…
}
| void* p | A pointer into a GC-managed memory block or null. |
| void* p | A pointer to a valid memory address or to null. |
| size_t sz | The size in bytes of the block to add. If sz is zero then the no operation will occur. If p is null then sz must be zero. |
// Allocate a piece of memory on the C heap. enum size = 1_000; auto rawMemory = core.stdc.stdlib.malloc(size); // Add it as a GC range. GC.addRange(rawMemory, size); // Now, pointers to GC-managed memory stored in // rawMemory will be recognized on collection.
| void* p | A pointer to a valid memory address or to null. |