gcAllocator-class {externalVector}R Documentation

Class "gcAllocator", memory allocator class for external resources

Description

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 from the Class

Objects can be created by calls of the form new("gcAllocator").

Extends

Class "externalAllocator", directly.

Methods

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:

allocate(resource, alloc, size, type, ...):
Allocate the external pointer in 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.
deallocate(resource, alloc):
Replace the protected field of resource@ptr with R_NilValue.
external.size
Return the size of the allocated memory in resource.
external.size<-(resource, copy, alloc, value):
If 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.
reinitializePointer(resource, alloc):
If the object 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.

Examples

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)

[Package externalVector version 1.0.14 Index]