Rcpp Version 1.0.14
Loading...
Searching...
No Matches
barrier.cpp
Go to the documentation of this file.
1
2// barrier.cpp: Rcpp R/C++ interface class library -- write barrier
3//
4// Copyright (C) 2010 - 2020 Dirk Eddelbuettel and Romain Francois
5// Copyright (C) 2021 - 2022 Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
6//
7// This file is part of Rcpp.
8//
9// Rcpp is free software: you can redistribute it and/or modify it
10// under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 2 of the License, or
12// (at your option) any later version.
13//
14// Rcpp is distributed in the hope that it will be useful, but
15// WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21
22#define COMPILING_RCPP
23
24#define USE_RINTERNALS
25
26#include <algorithm>
27#include <Rinternals.h>
28
29#include <Rcpp/barrier.h>
31#include <Rcpp/r/compat.h>
32
33#include "internal.h"
34
35// [[Rcpp::register]]
36SEXP get_string_elt(SEXP x, R_xlen_t i) { // #nocov start
37 return STRING_ELT(x, i);
38}
39
40// [[Rcpp::register]]
41const char* char_get_string_elt(SEXP x, R_xlen_t i) {
42 return CHAR(STRING_ELT(x, i));
43}
44
45// [[Rcpp::register]]
46void set_string_elt(SEXP x, R_xlen_t i, SEXP value) {
47 SET_STRING_ELT(x, i, value);
48}
49
50// [[Rcpp::register]]
51void char_set_string_elt(SEXP x, R_xlen_t i, const char* value) {
52 SET_STRING_ELT(x, i, Rf_mkChar(value));
53}
54
55// [[Rcpp::register]]
56SEXP* get_string_ptr(SEXP x) {
57 // TODO: should we deprecate this?
58 return const_cast<SEXP*>(RCPP_STRING_PTR(x));
59}
60
61// [[Rcpp::register]]
62SEXP get_vector_elt(SEXP x, R_xlen_t i) {
63 return VECTOR_ELT(x, i);
64}
65
66// [[Rcpp::register]]
67void set_vector_elt(SEXP x, R_xlen_t i, SEXP value) {
68 SET_VECTOR_ELT(x, i, value);
69}
70
71// [[Rcpp::register]]
72SEXP* get_vector_ptr(SEXP x) {
73 // TODO: should we deprecate this?
74 return const_cast<SEXP*>(RCPP_VECTOR_PTR(x)); // #nocov end
75}
76
77// [[Rcpp::register]]
78void* dataptr(SEXP x) {
79 return DATAPTR(x);
80}
81
82// [[Rcpp::register]]
83const char* char_nocheck(SEXP x) {
84 return CHAR(x);
85}
86
87static bool Rcpp_cache_know = false;
88static SEXP Rcpp_cache = R_NilValue;
89
90#define RCPP_HASH_CACHE_INDEX 4
91#define RCPP_CACHE_SIZE 5
92
93#ifndef RCPP_HASH_CACHE_INITIAL_SIZE
94#define RCPP_HASH_CACHE_INITIAL_SIZE 1024
95#endif
96
97namespace Rcpp {
99// [[Rcpp::register]]
100void Rcpp_precious_init() {
102 R_PreserveObject(Rcpp_precious); // and protect
103}
104// [[Rcpp::register]]
106 R_ReleaseObject(Rcpp_precious); // release resource
107}
108// [[Rcpp::register]]
110 if (object == R_NilValue) {
111 return R_NilValue;
112 }
113 PROTECT(object);
115 SET_TAG(cell, object);
117 if (CDR(cell) != R_NilValue) {
118 SETCAR(CDR(cell), cell);
119 }
120 UNPROTECT(2);
121 return cell;
122}
123// [[Rcpp::register]]
124void Rcpp_precious_remove(SEXP token) {
125 if (token == R_NilValue || TYPEOF(token) != LISTSXP) {
126 return;
127 }
128 SET_TAG(token, R_NilValue);
129 SEXP before = CAR(token);
130 SEXP after = CDR(token);
132 if (after != R_NilValue) {
134 }
135}
136}
137
138// only used for debugging
140 if (! Rcpp_cache_know) {
141
142 SEXP getNamespaceSym = Rf_install("getNamespace"); // cannot be gc()'ed once in symbol table
143 Rcpp::Shield<SEXP> RcppString(Rf_mkString("Rcpp"));
144 Rcpp::Shield<SEXP> call(Rf_lang2(getNamespaceSym, RcppString));
145 Rcpp::Shield<SEXP> RCPP(Rf_eval(call, R_GlobalEnv));
146
147 Rcpp_cache = Rf_findVarInFrame(RCPP, Rf_install(".rcpp_cache"));
148 Rcpp_cache_know = true;
149 }
150 return Rcpp_cache;
151}
152
153namespace Rcpp {
154 namespace internal {
155 // [[Rcpp::register]]
157 return VECTOR_ELT(get_rcpp_cache() , 0);
158 }
159 }
160}
161
162// [[Rcpp::register]]
164 return VECTOR_ELT(get_rcpp_cache(), 3);
165}
166
167// [[Rcpp::register]]
169 SET_VECTOR_ELT(get_rcpp_cache(), 3, e);
170 return R_NilValue;
171}
172
173SEXP set_error_occured(SEXP cache, SEXP e) {
174 SET_VECTOR_ELT(cache, 1, e);
175 return R_NilValue;
176}
177
178SEXP set_current_error(SEXP cache, SEXP e) {
179 SET_VECTOR_ELT(cache, 2, e);
180 return R_NilValue;
181}
182
184 SEXP getNamespaceSym = Rf_install("getNamespace"); // cannot be gc()'ed once in symbol table
185 Rcpp::Shield<SEXP> RcppString(Rf_mkString("Rcpp"));
186 Rcpp::Shield<SEXP> call(Rf_lang2(getNamespaceSym, RcppString));
187 Rcpp::Shield<SEXP> RCPP(Rf_eval(call, R_GlobalEnv));
188 Rcpp::Shield<SEXP> cache(Rf_allocVector(VECSXP, RCPP_CACHE_SIZE));
189
190 // the Rcpp namespace
191 SET_VECTOR_ELT(cache, 0, RCPP);
192 set_error_occured(cache, Rf_ScalarLogical(FALSE)); // error occured
193 set_current_error(cache, R_NilValue); // current error
194 SET_VECTOR_ELT(cache, 3, R_NilValue); // stack trace
195 Rcpp::Shield<SEXP> tmp(Rf_allocVector(INTSXP, RCPP_HASH_CACHE_INITIAL_SIZE));
196 SET_VECTOR_ELT(cache, RCPP_HASH_CACHE_INDEX, tmp);
197 Rf_defineVar(Rf_install(".rcpp_cache"), cache, RCPP);
198
199 return cache;
200}
201
202// [[Rcpp::register]]
203SEXP reset_current_error() { // #nocov start
204 SEXP cache = get_rcpp_cache();
205
206 // error occured
207 set_error_occured(cache, Rf_ScalarLogical(FALSE));
208
209 // current error
210 set_current_error(cache, R_NilValue);
211
212 // stack trace
213 SET_VECTOR_ELT(cache, 3, R_NilValue);
214
215 return R_NilValue;
216}
217
218// [[Rcpp::register]]
220 SEXP err = VECTOR_ELT(get_rcpp_cache(), 1);
221 return LOGICAL(err)[0];
222}
223
224// [[Rcpp::internal]]
226 SEXP cache = get_rcpp_cache();
227
228 // error occured
229 set_error_occured(cache, Rf_ScalarLogical(TRUE));
230
231 // current error
232 set_current_error(cache, e);
233
234 return R_NilValue;
235}
236
237// [[Rcpp::register]]
239 return VECTOR_ELT(get_rcpp_cache(), 2); // #nocov end
240}
241
242// [[Rcpp::register]]
243int* get_cache(int m) {
244 SEXP cache = get_rcpp_cache();
245 SEXP hash_cache = VECTOR_ELT(cache, RCPP_HASH_CACHE_INDEX);
246 int n = Rf_length(hash_cache);
247 if (m > n) {
248 Rcpp::Shield<SEXP> new_hash_cache(Rf_allocVector(INTSXP, m));
249 hash_cache = new_hash_cache;
250 SET_VECTOR_ELT(cache,RCPP_HASH_CACHE_INDEX, hash_cache);
251 }
252 int *res = INTEGER(hash_cache);
253 std::fill(res, res+m, 0);
254 return res;
255}
int error_occured()
Definition barrier.cpp:219
SEXP init_Rcpp_cache()
Definition barrier.cpp:183
#define RCPP_CACHE_SIZE
Definition barrier.cpp:91
void char_set_string_elt(SEXP x, R_xlen_t i, const char *value)
Definition barrier.cpp:51
#define RCPP_HASH_CACHE_INDEX
Definition barrier.cpp:90
SEXP set_current_error(SEXP cache, SEXP e)
Definition barrier.cpp:178
SEXP get_rcpp_cache()
Definition barrier.cpp:139
SEXP get_vector_elt(SEXP x, R_xlen_t i)
Definition barrier.cpp:62
int * get_cache(int m)
Definition barrier.cpp:243
#define RCPP_HASH_CACHE_INITIAL_SIZE
Definition barrier.cpp:94
SEXP reset_current_error()
Definition barrier.cpp:203
SEXP rcpp_error_recorder(SEXP e)
Definition barrier.cpp:225
static bool Rcpp_cache_know
Definition barrier.cpp:87
SEXP get_string_elt(SEXP x, R_xlen_t i)
Definition barrier.cpp:36
const char * char_get_string_elt(SEXP x, R_xlen_t i)
Definition barrier.cpp:41
void set_vector_elt(SEXP x, R_xlen_t i, SEXP value)
Definition barrier.cpp:67
const char * char_nocheck(SEXP x)
Definition barrier.cpp:83
SEXP * get_vector_ptr(SEXP x)
Definition barrier.cpp:72
void * dataptr(SEXP x)
Definition barrier.cpp:78
SEXP * get_string_ptr(SEXP x)
Definition barrier.cpp:56
SEXP rcpp_get_current_error()
Definition barrier.cpp:238
static SEXP Rcpp_cache
Definition barrier.cpp:88
SEXP set_error_occured(SEXP cache, SEXP e)
Definition barrier.cpp:173
SEXP rcpp_get_stack_trace()
Definition barrier.cpp:163
SEXP rcpp_set_stack_trace(SEXP e)
Definition barrier.cpp:168
void set_string_elt(SEXP x, R_xlen_t i, SEXP value)
Definition barrier.cpp:46
#define RCPP_STRING_PTR
Definition compat.h:28
#define RCPP_VECTOR_PTR
Definition compat.h:34
T as(SEXP x, ::Rcpp::traits::r_type_primitive_tag)
Definition as.h:43
attribute_hidden SEXP get_Rcpp_namespace()
Definition routines.h:121
Rcpp API.
Definition algo.h:28
attribute_hidden void Rcpp_precious_init()
Definition routines.h:142
attribute_hidden SEXP Rcpp_precious_preserve(SEXP object)
Definition routines.h:152
T as(SEXP x)
Definition as.h:151
attribute_hidden void Rcpp_precious_remove(SEXP token)
Definition routines.h:157
attribute_hidden void Rcpp_precious_teardown()
Definition routines.h:147
static SEXP Rcpp_precious
Definition barrier.cpp:98