www.digitalmars.com

D Programming Language 2.0

Last update Sun Sep 11 12:31:02 2011

std.allocators.gc

This module contains a struct that wraps D's builtin garbage collected allocator in an allocator interface as described in std.allocators.allocator.

Author:
David Simcha

License:
Boost License 1.0.

struct GCAllocator;
This struct provides an interface to D's garbage collector using the standard allocator interface.

bool scanForPointers;
This variable controls whether memory allocated via allocate may contain pointers and should be scanned by the garbage collector. This does not affect memory allocated by create, newArray, array or uninitializedArray since these functions have type information available. The default is true.

void* allocate(size_t nBytes);
Forwards to core.memory.GC.malloc and sets the GC.BlkAttr.NO_SCAN bit if scanForPointers is false.

void free(void* ptr);
Forwards to core.memory.GC.free.

static bool resize(void* ptr, size_t newSize);
Forwards to core.memory.GC.extend.

bool freeIsChecked;
False because the GC doesn't do any checking to ensure that free'd pointers are valid.

static size_t alignBytes(size_t s);
The GC aligns allocations of <16 bytes to 16 bytes, allocations between 16 bytes and the page size to the next larger power of two, and allocations greater than a page size to page boundaries.

static size_t allocSize(size_t s);
The GC rounds allocations of less than 16 bytes to 16 bytes, allocations of between 16 bytes and a page to the next power of two, and allocations greater than a page to the nearest page.

bool isAutomatic;
True because this is just a wrapper around the GC and gets automatically reclaimed.

bool isScoped;
False because GC allocated memory is not freed deterministically when this allocator goes out of scope.

C create(C, Args...)(auto ref Args args);
Forwards to the new operator for classes.

T* create(T, Args...)(auto ref Args args);
Forwards to the new operator for non-class objects.

T newArray(T, I...)(I sizes);
Forwards to the new operator for arrays.

    auto alloc = GCAllocator.init;

    // The following are equivalent:
    auto arr = new int[][](42, 86);
    auto arr = alloc.newArray!(int[][])(42, 86);

T uninitializedArray(T, I...)(I sizes);
Same as newArray except skips initialization of elements.

static auto array(R)(R range);
Forwards to std.array.array.

@property DynamicAllocator dynamicAllocator();
Returns a std.allocators.allocator.DynamicAllocator using this as the underlying allocator. The DynamicAllocator is allocated on the GC heap.