alloc: Allocate memory

allocR Documentation

Allocate memory

Description

This function and its methods dynamically allocate memory (using the C routine calloc). This allocates the appropriate number of bytes to accomodate the specified type. By default, the function arranges to garbage collect the allocated memory when it is no longer referenced by an R object. This may be undesirable as the memory may still be in use by C code. In this case, one can avoid garbage collection by specifying FALSE or NULL for the finalizer argument.

Usage

alloc(nbytes, finalizer = getNativeSymbolInfo("R_free")$address, ...)

Arguments

nbytes

the number of bytes to allocate or the FFIType object that identifies the type of object being allocated.

finalizer

a logical, NativeSymbolInfo, NativeSymbol or character string that indicates which the C routine as a symbol to use to free the memory once it is no longer referenced in R. A logical value of TRUE indicates that we should use the routine R_free. A value of FALSE or NULL indicates no finalizer should be registered.

...

additional parameters for methods, e.g. the class or instance of an R type to coerce the newly created pointer to.

Value

An external pointer object.

Author(s)

Duncan Temple Lang

See Also

addFinalizer free

Examples

  ptr = alloc(10)
  gc()
  rm(ptr)
  gc()

  i = alloc(sint32Type)
  callCIF(CIF(voidType, pointerType), "fillInt", i, returnInputs = FALSE)
  getNativeValue(i, sint32Type)

    # these are somewhat dangerous.
    # They rely on the externalptr being a pointer to a 32-bit int.
  setAs("externalptr", "integer",
         function(from)
           getNativeValue(from, sint32Type))
  setAs("RCReference", "integer",
         function(from)
           getNativeValue(from, sint32Type))
  as(i, "integer")

omegahat/Rffi documentation built on Nov. 29, 2023, 12:48 a.m.