core.memory
The memory module provides an interface to the garbage collector and to any other OS or API-level memory management facilities.License:
BSD Style, see LICENSE
Authors:
Sean Kelly
- This struct encapsulates all garbage collection functionality for the D
programming language.
- Enables the garbage collector if collections have previously been
suspended by a call to disable. This function is reentrant, and
must be called once for every call to disable before the garbage
collector is enabled.
- Disables the garbage collector. This function is reentrant, but
enable must be called once for each call to disable.
- Begins a full collection. While the meaning of this may change based
on the garbage collector implementation, typical behavior is to scan
all stack segments for roots, mark accessible memory blocks as alive,
and then to reclaim free space. This action may need to suspend all
running threads for at least part of the collection process.
- Indicates that the managed memory space be minimized by returning free
physical memory to the operating system. The amount of free memory
returned depends on the allocator design and on program behavior.
- Elements for a bit field representing memory block attributes. These
are manipulated via the getAttr, setAttr, clrAttr functions.
- Finalize the data in this block on collect.
- Do not scan through this block on collect.
- Do not move this memory block on collect.
- Contains aggregate information about a block of managed memory. The
purpose of this struct is to support a more efficient query style in
instances where detailed information is needed.
base = A pointer to the base of the block in question. size = The size of the block, calculated from base. attr = Attribute bits set on the memory block.
- Returns a bit field representing all block attributes set for the memory
referenced by p. If p references memory not originally allocated by
this garbage collector, points to the interior of a memory block, or if
p is null, zero will be returned.
Params:
void* p A pointer to the root of a valid memory block or to null.
Returns:
A bit field containing any bits set for the memory block referenced by p or zero on error.
- Sets the specified bits for the memory references by p. If p references
memory not originally allocated by this garbage collector, points to the
interior of a memory block, or if p is null, no action will be
performed.
Params:
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.
The result of a call to getAttr after the specified bits have been set.
- Clears the specified bits for the memory references by p. If p
references memory not originally allocated by this garbage collector,
points to the interior of a memory block, or if p is null, no action
will be performed.
Params:
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.
Returns:
The result of a call to getAttr after the specified bits have been cleared.
- Requests an aligned block of managed memory from the garbage collector.
This memory may be deleted at will with a call to free, or it may be
discarded and cleaned up automatically during a collection run. If
allocation fails, this function will call onOutOfMemory which is
expected to throw an OutOfMemoryException.
Params:
size_t sz The desired allocation size in bytes. uint ba A bitmask of the attributes to set on this block.
Returns:
A reference to the allocated memory or null if insufficient memory is available.
Throws:
OutOfMemoryException on allocation failure.
- Requests an aligned block of managed memory from the garbage collector,
which is initialized with all bits set to zero. This memory may be
deleted at will with a call to free, or it may be discarded and cleaned
up automatically during a collection run. If allocation fails, this
function will call onOutOfMemory which is expected to throw an
OutOfMemoryException.
Params:
size_t sz The desired allocation size in bytes. uint ba A bitmask of the attributes to set on this block.
Returns:
A reference to the allocated memory or null if insufficient memory is available.
Throws:
OutOfMemoryException on allocation failure.
- 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 OutOfMemoryException. 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.
Params:
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.
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:
OutOfMemoryException on allocation failure.
- 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, if p references memory
not originally allocated by this garbage collector, or if p points to
the interior of a memory block, no action will be taken.
Params:
size_t mx The minimum extension size in bytes. size_t sz The desired extension size in bytes.
Returns:
The size in bytes of the extended memory block referenced by p or zero if no extension occurred.
- Requests that at least sz bytes of memory be obtained from the operating
system and marked as free.
Params:
size_t sz The desired size in bytes.
Returns:
The actual number of bytes reserved or zero on error.
- Deallocates the memory referenced by p. If p is null, no action
occurs. 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. The block will not be finalized regardless
of whether the FINALIZE attribute is set. If finalization is desired,
use delete instead.
Params:
void* p A pointer to the root of a valid memory block or to null.
- Returns the base address of the memory block containing p. This value
is useful to determine whether p is an interior pointer, and the result
may be passed to routines such as sizeOf which may otherwise fail. If p
references memory not originally allocated by this garbage collector, if
p is null, or if the garbage collector does not support this operation,
null will be returned.
Params:
void* p A pointer to the root or the interior of a valid memory block or to null.
Returns:
The base address of the memory block referenced by p or null on error.
- Returns the true size of the memory block referenced by p. This value
represents the maximum number of bytes for which a call to realloc may
resize the existing block in place. If p references memory not
originally allocated by this garbage collector, points to the interior
of a memory block, or if p is null, zero will be returned.
Params:
void* p A pointer to the root of a valid memory block or to null.
Returns:
The size in bytes of the memory block referenced by p or zero on error.
- Returns aggregate information about the memory block containing p. If p
references memory not originally allocated by this garbage collector, if
p is null, or if the garbage collector does not support this operation,
BlkInfo.init will be returned. Typically, support for this operation
is dependent on support for addrOf.
Params:
void* p A pointer to the root or the interior of a valid memory block or to null.
Returns:
Information regarding the memory block referenced by p or BlkInfo.init on error.
- Adds the memory address referenced by p to an internal list of roots to
be scanned during a collection. If p is null, no operation is
performed.
Params:
void* p A pointer to a valid memory address or to null.
- Adds the memory block referenced by p and of size sz to an internal list
of ranges to be scanned during a collection. If p is null, no operation
is performed.
Params:
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.
- Removes the memory block referenced by p from an internal list of roots
to be scanned during a collection. If p is null or does not represent
a value previously passed to add(void*) then no operation is performed.
p = A pointer to a valid memory address or to null.
- Removes the memory block referenced by p from an internal list of ranges
to be scanned during a collection. If p is null or does not represent
a value previously passed to add(void*, size_t) then no operation is
performed.
Params:
void* p A pointer to a valid memory address or to null.
- Get handle to the collector.
The only thing that can be done with this handle is pass it to
setHandle(). getHandle/setHandle/endHandle work together so that
if there are multiple instances of the gc running, only one instance
can be set to run.
The most common case of this is under Windows where a D application
calls functions in a DLL that is implemented in D.
- Set handle to the collector.
The handle p is an opaque handle, acquired by a call to
getHandle().
- Call when done using the collector specified by the
call to setHandle().