annotate R/rdyncall/src/rutils.c @ 0:0cfcc391201f

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:26:28 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 /** ===========================================================================
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 ** R-Package: rdyncall
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 ** File: src/rutils.c
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 ** Description: misc utility functions to work with low-level data structures in R
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 **/
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 // uses: DATAPTR macro
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 #define USE_RINTERNALS
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 #include <Rinternals.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 #include <stddef.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 SEXP r_isnullptr(SEXP x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 return ScalarLogical( ( R_ExternalPtrAddr(x) == NULL ) ? TRUE : FALSE );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 SEXP r_asextptr(SEXP x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 if (isVector(x)) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 return R_MakeExternalPtr( DATAPTR(x), R_NilValue, x );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 error("expected a vector type");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 return R_NilValue; /* dummy */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 SEXP r_offsetPtr(SEXP x, SEXP offset)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 if ( LENGTH(offset) == 0 ) error("offset is missing");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 ptrdiff_t offsetval = INTEGER(offset)[0];
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 unsigned char* ptr = 0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 if (isVector(x)) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 ptr = (unsigned char*) DATAPTR(x);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 } else if (TYPEOF(x) == EXTPTRSXP ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 ptr = (unsigned char*) R_ExternalPtrAddr(x);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 } else {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 error("unsupported type");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 return R_MakeExternalPtr( ptr + offsetval , R_NilValue, x );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40