Skip to content

Preserved list refactor #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 11, 2020
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ cpp11test/src/*.so
compile_flags.txt
inst/doc
vignettes/*_cache
compile_commands.json
8 changes: 4 additions & 4 deletions cpp11test/src/protect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

[[cpp11::register]] void protect_one_cpp11_(SEXP x, int n) {
for (R_xlen_t i = 0; i < n; ++i) {
SEXP p = cpp11::protect_sexp(x);
cpp11::release_protect(p);
SEXP p = cpp11::preserved.insert(x);
cpp11::preserved.release(p);
}
}

Expand Down Expand Up @@ -63,12 +63,12 @@
[[cpp11::register]] void protect_many_cpp11_(int n) {
std::vector<SEXP> res;
for (R_xlen_t i = 0; i < n; ++i) {
res.push_back(cpp11::protect_sexp(Rf_ScalarInteger(n)));
res.push_back(cpp11::preserved.insert(Rf_ScalarInteger(n)));
}

for (R_xlen_t i = n - 1; i >= 0; --i) {
SEXP x = res[i];
cpp11::release_protect(x);
cpp11::preserved.release(x);
res.pop_back();
}
}
Expand Down
1 change: 1 addition & 0 deletions cpp11test/src/test-sexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ context("sexp-C++") {

expect_true(Rf_inherits(out, "data.frame"));
}

test_that("scalar constructors work") {
using namespace cpp11::literals;
cpp11::writable::list out({
Expand Down
8 changes: 4 additions & 4 deletions inst/include/cpp11/doubles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ template <>
inline r_vector<double>::r_vector(std::initializer_list<named_arg> il)
: cpp11::r_vector<double>(safe[Rf_allocVector](REALSXP, il.size())),
capacity_(il.size()) {
protect_ = protect_sexp(data_);
protect_ = preserved.insert(data_);
int n_protected = 0;

try {
Expand All @@ -95,7 +95,7 @@ inline r_vector<double>::r_vector(std::initializer_list<named_arg> il)
UNPROTECT(n_protected);
});
} catch (const unwind_exception& e) {
release_protect(protect_);
preserved.release(protect_);
UNPROTECT(n_protected);
throw e;
}
Expand All @@ -106,8 +106,8 @@ inline void r_vector<double>::reserve(R_xlen_t new_capacity) {
data_ = data_ == R_NilValue ? safe[Rf_allocVector](REALSXP, new_capacity)
: safe[Rf_xlengthgets](data_, new_capacity);
SEXP old_protect = protect_;
protect_ = protect_sexp(data_);
release_protect(old_protect);
protect_ = preserved.insert(data_);
preserved.release(old_protect);

data_p_ = REAL(data_);
capacity_ = new_capacity;
Expand Down
10 changes: 5 additions & 5 deletions inst/include/cpp11/integers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "cpp11/as.hpp" // for as_sexp
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
#include "cpp11/named_arg.hpp" // for named_arg
#include "cpp11/protect.hpp" // for protect_sexp, release_protect
#include "cpp11/protect.hpp" // for preserved
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
#include "cpp11/sexp.hpp" // for sexp

Expand Down Expand Up @@ -83,10 +83,10 @@ inline void r_vector<int>::reserve(R_xlen_t new_capacity) {
SEXP old_protect = protect_;

// Protect the new data
protect_ = protect_sexp(data_);
protect_ = preserved.insert(data_);

// Release the old protection;
release_protect(old_protect);
preserved.release(old_protect);

data_p_ = INTEGER(data_);
capacity_ = new_capacity;
Expand All @@ -96,7 +96,7 @@ template <>
inline r_vector<int>::r_vector(std::initializer_list<named_arg> il)
: cpp11::r_vector<int>(safe[Rf_allocVector](INTSXP, il.size())),
capacity_(il.size()) {
protect_ = protect_sexp(data_);
protect_ = preserved.insert(data_);
int n_protected = 0;

try {
Expand All @@ -112,7 +112,7 @@ inline r_vector<int>::r_vector(std::initializer_list<named_arg> il)
UNPROTECT(n_protected);
});
} catch (const unwind_exception& e) {
release_protect(protect_);
preserved.release(protect_);
UNPROTECT(n_protected);
throw e;
}
Expand Down
12 changes: 6 additions & 6 deletions inst/include/cpp11/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "cpp11/R.hpp" // for SEXP, SEXPREC, SET_VECTOR_ELT
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
#include "cpp11/named_arg.hpp" // for named_arg
#include "cpp11/protect.hpp" // for protect_sexp, release_protect
#include "cpp11/protect.hpp" // for preserved
#include "cpp11/r_string.hpp" // for r_string
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
#include "cpp11/sexp.hpp" // for sexp
Expand Down Expand Up @@ -75,7 +75,7 @@ template <>
inline r_vector<SEXP>::r_vector(std::initializer_list<SEXP> il)
: cpp11::r_vector<SEXP>(safe[Rf_allocVector](VECSXP, il.size())),
capacity_(il.size()) {
protect_ = protect_sexp(data_);
protect_ = preserved.insert(data_);
auto it = il.begin();
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
SET_VECTOR_ELT(data_, i, *it);
Expand All @@ -86,7 +86,7 @@ template <>
inline r_vector<SEXP>::r_vector(std::initializer_list<named_arg> il)
: cpp11::r_vector<SEXP>(safe[Rf_allocVector](VECSXP, il.size())),
capacity_(il.size()) {
protect_ = protect_sexp(data_);
protect_ = preserved.insert(data_);
int n_protected = 0;

try {
Expand All @@ -102,7 +102,7 @@ inline r_vector<SEXP>::r_vector(std::initializer_list<named_arg> il)
UNPROTECT(n_protected);
});
} catch (const unwind_exception& e) {
release_protect(protect_);
preserved.release(protect_);
UNPROTECT(n_protected);
throw e;
}
Expand All @@ -114,8 +114,8 @@ inline void r_vector<SEXP>::reserve(R_xlen_t new_capacity) {
: safe[Rf_xlengthgets](data_, new_capacity);

SEXP old_protect = protect_;
protect_ = protect_sexp(data_);
release_protect(old_protect);
protect_ = preserved.insert(data_);
preserved.release(old_protect);

capacity_ = new_capacity;
}
Expand Down
12 changes: 6 additions & 6 deletions inst/include/cpp11/logicals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "cpp11/R.hpp" // for Rboolean, SEXP, SEXPREC, Rf_all...
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
#include "cpp11/named_arg.hpp" // for named_arg
#include "cpp11/protect.hpp" // for protect_sexp, release_protect
#include "cpp11/protect.hpp" // for preserved
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
#include "cpp11/sexp.hpp" // for sexp

Expand Down Expand Up @@ -71,7 +71,7 @@ inline r_vector<Rboolean>::proxy::operator Rboolean() const {
template <>
inline r_vector<Rboolean>::r_vector(std::initializer_list<Rboolean> il)
: cpp11::r_vector<Rboolean>(Rf_allocVector(LGLSXP, il.size())), capacity_(il.size()) {
protect_ = protect_sexp(data_);
protect_ = preserved.insert(data_);
auto it = il.begin();
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
SET_LOGICAL_ELT(data_, i, *it);
Expand All @@ -82,7 +82,7 @@ template <>
inline r_vector<Rboolean>::r_vector(std::initializer_list<named_arg> il)
: cpp11::r_vector<Rboolean>(safe[Rf_allocVector](LGLSXP, il.size())),
capacity_(il.size()) {
protect_ = protect_sexp(data_);
protect_ = preserved.insert(data_);
int n_protected = 0;

try {
Expand All @@ -98,7 +98,7 @@ inline r_vector<Rboolean>::r_vector(std::initializer_list<named_arg> il)
UNPROTECT(n_protected);
});
} catch (const unwind_exception& e) {
release_protect(protect_);
preserved.release(protect_);
UNPROTECT(n_protected);
throw e;
}
Expand All @@ -109,9 +109,9 @@ inline void r_vector<Rboolean>::reserve(R_xlen_t new_capacity) {
data_ = data_ == R_NilValue ? safe[Rf_allocVector](LGLSXP, new_capacity)
: safe[Rf_xlengthgets](data_, new_capacity);
SEXP old_protect = protect_;
protect_ = protect_sexp(data_);
protect_ = preserved.insert(data_);

release_protect(old_protect);
preserved.release(old_protect);

data_p_ = reinterpret_cast<Rboolean*>(LOGICAL(data_));
capacity_ = new_capacity;
Expand Down
Loading