Rcpp Version 1.1.2
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 - 2026 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 // DATAPTR_RO was introduced with R 3.5.0
80 return const_cast<void*>(DATAPTR_RO(x));
81}
82
83// [[Rcpp::register]]
84const char* char_nocheck(SEXP x) {
85 return CHAR(x);
86}
87
88static bool Rcpp_cache_know = false;
89static SEXP Rcpp_cache = R_NilValue;
90
91#define RCPP_HASH_CACHE_INDEX 4
92#define RCPP_CACHE_SIZE 5
93
94#ifndef RCPP_HASH_CACHE_INITIAL_SIZE
95#define RCPP_HASH_CACHE_INITIAL_SIZE 1024
96#endif
97
98namespace Rcpp {
99static SEXP Rcpp_precious = R_NilValue;
100// [[Rcpp::register]]
101void Rcpp_precious_init() {
102 Rcpp_precious = CONS(R_NilValue, R_NilValue); // set up
103 R_PreserveObject(Rcpp_precious); // and protect
104}
105// [[Rcpp::register]]
106void Rcpp_precious_teardown() { // #nocov start
107 R_ReleaseObject(Rcpp_precious); // release resource
108} // #nocov end
109// [[Rcpp::register]]
110SEXP Rcpp_precious_preserve(SEXP object) {
111 if (object == R_NilValue) {
112 return R_NilValue; // #nocov
113 }
114 PROTECT(object);
115 SEXP cell = PROTECT(CONS(Rcpp_precious, CDR(Rcpp_precious)));
116 SET_TAG(cell, object);
117 SETCDR(Rcpp_precious, cell);
118 if (CDR(cell) != R_NilValue) {
119 SETCAR(CDR(cell), cell);
120 }
121 UNPROTECT(2);
122 return cell;
123}
124// [[Rcpp::register]]
125void Rcpp_precious_remove(SEXP token) {
126 if (token == R_NilValue || TYPEOF(token) != LISTSXP) {
127 return;
128 }
129 SET_TAG(token, R_NilValue);
130 SEXP before = CAR(token);
131 SEXP after = CDR(token);
132 SETCDR(before, after);
133 if (after != R_NilValue) {
134 SETCAR(after, before);
135 }
136}
137}
138
139// only used for debugging
141 if (! Rcpp_cache_know) {
142
143 SEXP getNamespaceSym = Rf_install("getNamespace"); // cannot be gc()'ed once in symbol table
144 Rcpp::Shield<SEXP> RcppString(Rf_mkString("Rcpp"));
145 Rcpp::Shield<SEXP> call(Rf_lang2(getNamespaceSym, RcppString));
146 Rcpp::Shield<SEXP> RCPP(Rf_eval(call, R_GlobalEnv));
147
148#if R_VERSION < R_Version(4,5,0)
149 Rcpp_cache = Rf_findVarInFrame(RCPP, Rf_install(".rcpp_cache"));
150#else
151 Rcpp_cache = R_getVar(Rf_install(".rcpp_cache"), RCPP, TRUE);
152#endif
153 Rcpp_cache_know = true;
154 }
155 return Rcpp_cache;
156}
157
158namespace Rcpp {
159 namespace internal {
160 // [[Rcpp::register]]
161 SEXP get_Rcpp_namespace() {
162 return VECTOR_ELT(get_rcpp_cache() , 0);
163 }
164 }
165}
166
167// [[Rcpp::register]]
169 return VECTOR_ELT(get_rcpp_cache(), 3);
170}
171
172// [[Rcpp::register]]
174 SET_VECTOR_ELT(get_rcpp_cache(), 3, e);
175 return R_NilValue;
176}
177
178SEXP set_error_occured(SEXP cache, SEXP e) {
179 SET_VECTOR_ELT(cache, 1, e);
180 return R_NilValue;
181}
182
183SEXP set_current_error(SEXP cache, SEXP e) {
184 SET_VECTOR_ELT(cache, 2, e);
185 return R_NilValue;
186}
187
189 SEXP getNamespaceSym = Rf_install("getNamespace"); // cannot be gc()'ed once in symbol table
190 Rcpp::Shield<SEXP> RcppString(Rf_mkString("Rcpp"));
191 Rcpp::Shield<SEXP> call(Rf_lang2(getNamespaceSym, RcppString));
192 Rcpp::Shield<SEXP> RCPP(Rf_eval(call, R_GlobalEnv));
193 Rcpp::Shield<SEXP> cache(Rf_allocVector(VECSXP, RCPP_CACHE_SIZE));
194
195 // the Rcpp namespace
196 SET_VECTOR_ELT(cache, 0, RCPP);
197 set_error_occured(cache, Rf_ScalarLogical(FALSE)); // error occured
198 set_current_error(cache, R_NilValue); // current error
199 SET_VECTOR_ELT(cache, 3, R_NilValue); // stack trace
200 Rcpp::Shield<SEXP> tmp(Rf_allocVector(INTSXP, RCPP_HASH_CACHE_INITIAL_SIZE));
201 SET_VECTOR_ELT(cache, RCPP_HASH_CACHE_INDEX, tmp);
202 Rf_defineVar(Rf_install(".rcpp_cache"), cache, RCPP);
203
204 return cache;
205}
206
207// [[Rcpp::register]]
208SEXP reset_current_error() { // #nocov start
209 SEXP cache = get_rcpp_cache();
210
211 // error occured
212 set_error_occured(cache, Rf_ScalarLogical(FALSE));
213
214 // current error
215 set_current_error(cache, R_NilValue);
216
217 // stack trace
218 SET_VECTOR_ELT(cache, 3, R_NilValue);
219
220 return R_NilValue;
221}
222
223// [[Rcpp::register]]
225 SEXP err = VECTOR_ELT(get_rcpp_cache(), 1);
226 return LOGICAL(err)[0];
227}
228
229// [[Rcpp::internal]]
231 SEXP cache = get_rcpp_cache();
232
233 // error occured
234 set_error_occured(cache, Rf_ScalarLogical(TRUE));
235
236 // current error
237 set_current_error(cache, e);
238
239 return R_NilValue;
240}
241
242// [[Rcpp::register]]
244 return VECTOR_ELT(get_rcpp_cache(), 2); // #nocov end
245}
246
247// [[Rcpp::register]]
248int* get_cache(int m) {
249 SEXP cache = get_rcpp_cache();
250 SEXP hash_cache = VECTOR_ELT(cache, RCPP_HASH_CACHE_INDEX);
251 int n = Rf_length(hash_cache);
252 if (m > n) {
253 Rcpp::Shield<SEXP> new_hash_cache(Rf_allocVector(INTSXP, m));
254 hash_cache = new_hash_cache;
255 SET_VECTOR_ELT(cache,RCPP_HASH_CACHE_INDEX, hash_cache);
256 }
257 int *res = INTEGER(hash_cache);
258 std::fill(res, res+m, 0);
259 return res;
260}
int error_occured()
Definition barrier.cpp:224
SEXP init_Rcpp_cache()
Definition barrier.cpp:188
#define RCPP_CACHE_SIZE
Definition barrier.cpp:92
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:91
SEXP set_current_error(SEXP cache, SEXP e)
Definition barrier.cpp:183
SEXP get_rcpp_cache()
Definition barrier.cpp:140
SEXP get_vector_elt(SEXP x, R_xlen_t i)
Definition barrier.cpp:62
int * get_cache(int m)
Definition barrier.cpp:248
#define RCPP_HASH_CACHE_INITIAL_SIZE
Definition barrier.cpp:95
SEXP reset_current_error()
Definition barrier.cpp:208
SEXP rcpp_error_recorder(SEXP e)
Definition barrier.cpp:230
static bool Rcpp_cache_know
Definition barrier.cpp:88
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:84
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:243
static SEXP Rcpp_cache
Definition barrier.cpp:89
SEXP set_error_occured(SEXP cache, SEXP e)
Definition barrier.cpp:178
SEXP rcpp_get_stack_trace()
Definition barrier.cpp:168
SEXP rcpp_set_stack_trace(SEXP e)
Definition barrier.cpp:173
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
internal implementation details
Definition Date.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
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:99