Skip to content

Commit b39bae1

Browse files
committed
Create internal store namespace
1 parent e9bfdce commit b39bae1

File tree

11 files changed

+83
-73
lines changed

11 files changed

+83
-73
lines changed

Diff for: cpp11test/src/protect.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
[[cpp11::register]] void protect_one_cpp11_(SEXP x, int n) {
2020
for (R_xlen_t i = 0; i < n; ++i) {
21-
SEXP p = cpp11::detail::store_insert(x);
22-
cpp11::detail::store_release(p);
21+
SEXP p = cpp11::detail::store::insert(x);
22+
cpp11::detail::store::release(p);
2323
}
2424
}
2525

@@ -51,12 +51,12 @@
5151
[[cpp11::register]] void protect_many_cpp11_(int n) {
5252
std::vector<SEXP> res;
5353
for (R_xlen_t i = 0; i < n; ++i) {
54-
res.push_back(cpp11::detail::store_insert(Rf_ScalarInteger(n)));
54+
res.push_back(cpp11::detail::store::insert(Rf_ScalarInteger(n)));
5555
}
5656

5757
for (R_xlen_t i = n - 1; i >= 0; --i) {
5858
SEXP x = res[i];
59-
cpp11::detail::store_release(x);
59+
cpp11::detail::store::release(x);
6060
res.pop_back();
6161
}
6262
}

Diff for: inst/include/cpp11/doubles.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ template <>
8484
inline r_vector<double>::r_vector(std::initializer_list<named_arg> il)
8585
: cpp11::r_vector<double>(safe[Rf_allocVector](REALSXP, il.size())),
8686
capacity_(il.size()) {
87-
protect_ = detail::store_insert(data_);
87+
protect_ = detail::store::insert(data_);
8888
int n_protected = 0;
8989

9090
try {
@@ -100,7 +100,7 @@ inline r_vector<double>::r_vector(std::initializer_list<named_arg> il)
100100
UNPROTECT(n_protected);
101101
});
102102
} catch (const unwind_exception& e) {
103-
detail::store_release(protect_);
103+
detail::store::release(protect_);
104104
UNPROTECT(n_protected);
105105
throw e;
106106
}
@@ -111,8 +111,8 @@ inline void r_vector<double>::reserve(R_xlen_t new_capacity) {
111111
data_ = data_ == R_NilValue ? safe[Rf_allocVector](REALSXP, new_capacity)
112112
: safe[Rf_xlengthgets](data_, new_capacity);
113113
SEXP old_protect = protect_;
114-
protect_ = detail::store_insert(data_);
115-
detail::store_release(old_protect);
114+
protect_ = detail::store::insert(data_);
115+
detail::store::release(old_protect);
116116

117117
data_p_ = REAL(data_);
118118
capacity_ = new_capacity;

Diff for: inst/include/cpp11/integers.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "cpp11/as.hpp" // for as_sexp
1010
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
1111
#include "cpp11/named_arg.hpp" // for named_arg
12-
#include "cpp11/protect.hpp" // for store_insert, store_release
12+
#include "cpp11/protect.hpp" // for store
1313
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
1414
#include "cpp11/sexp.hpp" // for sexp
1515

@@ -87,10 +87,10 @@ inline void r_vector<int>::reserve(R_xlen_t new_capacity) {
8787
SEXP old_protect = protect_;
8888

8989
// Protect the new data
90-
protect_ = detail::store_insert(data_);
90+
protect_ = detail::store::insert(data_);
9191

9292
// Release the old protection;
93-
detail::store_release(old_protect);
93+
detail::store::release(old_protect);
9494

9595
data_p_ = INTEGER(data_);
9696
capacity_ = new_capacity;
@@ -100,7 +100,7 @@ template <>
100100
inline r_vector<int>::r_vector(std::initializer_list<named_arg> il)
101101
: cpp11::r_vector<int>(safe[Rf_allocVector](INTSXP, il.size())),
102102
capacity_(il.size()) {
103-
protect_ = detail::store_insert(data_);
103+
protect_ = detail::store::insert(data_);
104104
int n_protected = 0;
105105

106106
try {
@@ -116,7 +116,7 @@ inline r_vector<int>::r_vector(std::initializer_list<named_arg> il)
116116
UNPROTECT(n_protected);
117117
});
118118
} catch (const unwind_exception& e) {
119-
detail::store_release(protect_);
119+
detail::store::release(protect_);
120120
UNPROTECT(n_protected);
121121
throw e;
122122
}

Diff for: inst/include/cpp11/list.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "cpp11/R.hpp" // for SEXP, SEXPREC, SET_VECTOR_ELT
66
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
77
#include "cpp11/named_arg.hpp" // for named_arg
8-
#include "cpp11/protect.hpp" // for store_insert, store_release
8+
#include "cpp11/protect.hpp" // for store
99
#include "cpp11/r_string.hpp" // for r_string
1010
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
1111
#include "cpp11/sexp.hpp" // for sexp
@@ -78,7 +78,7 @@ template <>
7878
inline r_vector<SEXP>::r_vector(std::initializer_list<SEXP> il)
7979
: cpp11::r_vector<SEXP>(safe[Rf_allocVector](VECSXP, il.size())),
8080
capacity_(il.size()) {
81-
protect_ = detail::store_insert(data_);
81+
protect_ = detail::store::insert(data_);
8282
auto it = il.begin();
8383
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
8484
SET_VECTOR_ELT(data_, i, *it);
@@ -89,7 +89,7 @@ template <>
8989
inline r_vector<SEXP>::r_vector(std::initializer_list<named_arg> il)
9090
: cpp11::r_vector<SEXP>(safe[Rf_allocVector](VECSXP, il.size())),
9191
capacity_(il.size()) {
92-
protect_ = detail::store_insert(data_);
92+
protect_ = detail::store::insert(data_);
9393
int n_protected = 0;
9494

9595
try {
@@ -105,7 +105,7 @@ inline r_vector<SEXP>::r_vector(std::initializer_list<named_arg> il)
105105
UNPROTECT(n_protected);
106106
});
107107
} catch (const unwind_exception& e) {
108-
detail::store_release(protect_);
108+
detail::store::release(protect_);
109109
UNPROTECT(n_protected);
110110
throw e;
111111
}
@@ -117,8 +117,8 @@ inline void r_vector<SEXP>::reserve(R_xlen_t new_capacity) {
117117
: safe[Rf_xlengthgets](data_, new_capacity);
118118

119119
SEXP old_protect = protect_;
120-
protect_ = detail::store_insert(data_);
121-
detail::store_release(old_protect);
120+
protect_ = detail::store::insert(data_);
121+
detail::store::release(old_protect);
122122

123123
capacity_ = new_capacity;
124124
}

Diff for: inst/include/cpp11/logicals.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "cpp11/R.hpp" // for SEXP, SEXPREC, Rf_all...
88
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
99
#include "cpp11/named_arg.hpp" // for named_arg
10-
#include "cpp11/protect.hpp" // for store_insert, store_release
10+
#include "cpp11/protect.hpp" // for store
1111
#include "cpp11/r_bool.hpp" // for r_bool
1212
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
1313
#include "cpp11/sexp.hpp" // for sexp
@@ -80,7 +80,7 @@ inline bool operator==(const r_vector<r_bool>::proxy& lhs, r_bool rhs) {
8080
template <>
8181
inline r_vector<r_bool>::r_vector(std::initializer_list<r_bool> il)
8282
: cpp11::r_vector<r_bool>(Rf_allocVector(LGLSXP, il.size())), capacity_(il.size()) {
83-
protect_ = detail::store_insert(data_);
83+
protect_ = detail::store::insert(data_);
8484
auto it = il.begin();
8585
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
8686
SET_LOGICAL_ELT(data_, i, *it);
@@ -91,7 +91,7 @@ template <>
9191
inline r_vector<r_bool>::r_vector(std::initializer_list<named_arg> il)
9292
: cpp11::r_vector<r_bool>(safe[Rf_allocVector](LGLSXP, il.size())),
9393
capacity_(il.size()) {
94-
protect_ = detail::store_insert(data_);
94+
protect_ = detail::store::insert(data_);
9595
int n_protected = 0;
9696

9797
try {
@@ -107,7 +107,7 @@ inline r_vector<r_bool>::r_vector(std::initializer_list<named_arg> il)
107107
UNPROTECT(n_protected);
108108
});
109109
} catch (const unwind_exception& e) {
110-
detail::store_release(protect_);
110+
detail::store::release(protect_);
111111
UNPROTECT(n_protected);
112112
throw e;
113113
}
@@ -118,9 +118,9 @@ inline void r_vector<r_bool>::reserve(R_xlen_t new_capacity) {
118118
data_ = data_ == R_NilValue ? safe[Rf_allocVector](LGLSXP, new_capacity)
119119
: safe[Rf_xlengthgets](data_, new_capacity);
120120
SEXP old_protect = protect_;
121-
protect_ = detail::store_insert(data_);
121+
protect_ = detail::store::insert(data_);
122122

123-
detail::store_release(old_protect);
123+
detail::store::release(old_protect);
124124

125125
data_p_ = LOGICAL(data_);
126126
capacity_ = new_capacity;

Diff for: inst/include/cpp11/protect.hpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -265,28 +265,36 @@ namespace detail {
265265
//
266266
// > A static local variable in an extern inline function always refers to the
267267
// same object. 7.1.2/4 - C++98/C++14 (n3797)
268+
namespace store {
268269

269-
inline SEXP new_store() {
270+
inline SEXP init() {
270271
SEXP out = Rf_cons(R_NilValue, Rf_cons(R_NilValue, R_NilValue));
271272
R_PreserveObject(out);
272273
return out;
273274
}
274275

275-
inline SEXP store() {
276+
inline SEXP get() {
276277
// Note the `static` local variable in the inline extern function here! Guarantees we
277278
// have 1 unique preserve list across all compilation units in the package.
278-
static SEXP out = new_store();
279+
static SEXP out = init();
279280
return out;
280281
}
281282

282-
inline SEXP store_insert(SEXP x) {
283+
inline R_xlen_t count() {
284+
const R_xlen_t head = 1;
285+
const R_xlen_t tail = 1;
286+
SEXP list = get();
287+
return Rf_xlength(list) - head - tail;
288+
}
289+
290+
inline SEXP insert(SEXP x) {
283291
if (x == R_NilValue) {
284292
return R_NilValue;
285293
}
286294

287295
PROTECT(x);
288296

289-
SEXP list = store();
297+
SEXP list = get();
290298

291299
// Get references to the head of the preserve list and the next element
292300
// after the head
@@ -307,7 +315,7 @@ inline SEXP store_insert(SEXP x) {
307315
return cell;
308316
}
309317

310-
inline void store_release(SEXP cell) {
318+
inline void release(SEXP cell) {
311319
if (cell == R_NilValue) {
312320
return;
313321
}
@@ -323,8 +331,8 @@ inline void store_release(SEXP cell) {
323331
SETCAR(rhs, lhs);
324332
}
325333

326-
inline void store_print() {
327-
SEXP list = store();
334+
inline void print() {
335+
SEXP list = get();
328336
for (SEXP cell = list; cell != R_NilValue; cell = CDR(cell)) {
329337
REprintf("%p CAR: %p CDR: %p TAG: %p\n", reinterpret_cast<void*>(cell),
330338
reinterpret_cast<void*>(CAR(cell)), reinterpret_cast<void*>(CDR(cell)),
@@ -333,6 +341,8 @@ inline void store_print() {
333341
REprintf("---\n");
334342
}
335343

344+
} // namespace store
345+
336346
} // namespace detail
337347

338348
} // namespace cpp11

0 commit comments

Comments
 (0)