gcAllocator-class {externalVector} | R Documentation |
A memory allocator class for external resources that allocates memory using R memory management system. The allocated memory is freed by the R garbage collector when all refernce to it is removed.
Objects can be created by calls of the form new("gcAllocator")
.
Class "externalAllocator"
, directly.
Signature components for the methods:
resource | The class "externalResource" |
alloc | The class of "gcAllocator" |
size | The class "ANY" |
type | The class "ANY" |
copy | The class "logical" |
value | The class "ANY" |
Description of the methods:
resource
.
If type
is a basic vector object, then
allocate an object of same mode with length
size
. Otherwise allocate size
bytes of raw
memory. The allocation is done by creating an R basic vector
with same class as type and storing it in the protected field of
the newly created external pointer. Ends with a call to
initializeResource
to initialize the resource
object. resource@ptr
with R_NilValue
. resource
. value
is same as external.size(resource)
, then no
action is taken. Otherwise, reallocate the memory in
resource
with new size value
. If copy
is
TRUE
(the default), then the new memory is initialized to
the content of the old memory for the minimum of old and new
sizes. Content of any uninitialized memory is undefined. resource
was saved as an R image (by serialization
code, by saving the R workspace, or by an explicit call to
save
) then the raw memory pointer in any
"externalptr"
object in it would be set to 0
. This
method reinitializes the memory (if possible) by using the
protected field of the external pointer. library(externalVector) ## set a storage class setClass("testStorage", representation(ptr="externalptr"), contains="externalResource") setMethod("initializeResource", "testStorage", function(resource, ptr, size, type, ...) { resource@ptr <- ptr resource }) setMethod("allocator", "testStorage", function(resource) new("gcAllocator")) setMethod("getPointer", "testStorage", function(resource) resource@ptr) setMethod("allocatedSize", "testStorage", function(resource) 32) ## Now create an object from the class x <- new("testStorage") x external.size(x) external.size(x) <- 64 x external.size(x) deallocate(x) x external.size(x)