annotate R/rdyncall/src/rutils_str.c @ 66:7a61dd082341 default tip

pydc: - fix double free triggered by capsule destructor, when freeing pydc and callback objects, manually
author Tassilo Philipp
date Fri, 24 May 2024 18:16:29 +0200
parents 0cfcc391201f
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_str.c
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 ** Description: Support functions for handling C string data types.
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 #define USE_RINTERNALS
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 #include <Rdefines.h>
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 <R_ext/RS.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 /* String utils */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 SEXP r_ptr2str(SEXP extptr)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 void* addr = R_ExternalPtrAddr(extptr);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 if (addr == NULL) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 return R_NilValue;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 return mkString(addr);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 SEXP r_strptr(SEXP x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 return R_MakeExternalPtr( (void*) CHAR(STRING_ELT(x, 0)), R_NilValue, x );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 void do_free(SEXP x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 void* addr = R_ExternalPtrAddr(x);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 R_Free(addr);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 SEXP r_strarrayptr(SEXP s)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 int i;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 int n;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 const char ** ptrs;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 n = LENGTH(s);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 // allocate array
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 ptrs = R_Calloc(n, const char*);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 // copy cstring pointers into array
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 for( i=0 ; i<n ; ++i ) ptrs[i] = CHAR( STRING_ELT(s, i) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 // create external pointer pointing to array
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 SEXP x = PROTECT( R_MakeExternalPtr( ptrs, R_NilValue, s ) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 R_RegisterCFinalizerEx( x, do_free, TRUE );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 UNPROTECT(1);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 return x;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 }