Skip to content
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

correct 'allocated?' logic and actually call free() (closes #20) #22

Merged
merged 2 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2019-10-14 Dirk Eddelbuettel <edd@debian.org>

* inst/include/RcppGSL_types.h: Correct logic, and actually call free()

2018-09-02 Dirk Eddelbuettel <edd@debian.org>

* .travis.yml: Switch Travis CI to R 3.5 repo
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: RcppGSL
Type: Package
Title: 'Rcpp' Integration for 'GNU GSL' Vectors and Matrices
Version: 0.3.6
Date: 2018-06-10
Version: 0.3.6.1
Date: 2019-10-14
Author: Dirk Eddelbuettel and Romain Francois
Maintainer: Dirk Eddelbuettel <edd@debian.org>
Description: 'Rcpp' integration for 'GNU GSL' vectors and matrices
Expand Down
90 changes: 47 additions & 43 deletions inst/include/RcppGSL_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//
// RcppGSL_types.h: Type macros for Seamless R and GSL Integration
//
// Copyright (C) 2010 - 2018 Romain Francois and Dirk Eddelbuettel
// Copyright (C) 2010 - 2019 Romain Francois and Dirk Eddelbuettel
//
// This file is part of RcppGSL.
//
// RcppGSL is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
//
// RcppGSL is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Expand All @@ -34,7 +34,7 @@ template <> struct matrix_view_type<__T__> { \
typedef gsl_matrix##__SUFFIX__##_view type; \
typedef gsl_matrix##__SUFFIX__##_const_view const_type; \
}; \
template <> class vector<__T__> { \
template <> class vector<__T__> { \
public: \
typedef __T__ type; \
typedef __T__* pointer; \
Expand Down Expand Up @@ -77,33 +77,33 @@ public: \
const static int RTYPE = \
::Rcpp::traits::r_sexptype_traits<type>::rtype; \
vector(SEXP x) : \
data(0), isAllocated(true) { \
data(nullptr), allocated(false) { \
SEXP y = ::Rcpp::r_cast<RTYPE>(x); \
int size = ::Rf_length(y); \
data = gsl_vector##__SUFFIX__##_calloc(size); \
::Rcpp::internal::export_range<__CAST__*>(y, \
reinterpret_cast<__CAST__*>(data->data)); \
} \
vector(gsltype* x) : data(x), isAllocated(true) {} \
vector(gsltype* x) : data(x), allocated(false) {} \
vector(int size) : \
data(gsl_vector##__SUFFIX__##_calloc(size)), \
isAllocated(true) {} \
~vector() {} \
allocated(true) {} \
~vector() { free(); } \
operator gsltype*() { return data; } \
operator const gsltype*() const { return data; } \
gsltype* operator->() const { return data; } \
gsltype& operator*() const { return *data; } \
vector(const vector& x) : data(x.data), isAllocated(true) {} \
vector(const vector& x) : data(x.data), allocated(x.allocated) {} \
vector& operator=(const vector& other) { \
data = other.data; \
isAllocated = other.isAllocated; \
allocated = other.allocated; \
return *this; \
} \
inline Proxy operator[](int i) { \
return Proxy(data, i); \
return Proxy(data, i); \
} \
inline ConstProxy operator[](int i) const { \
return ConstProxy(data, i); \
return ConstProxy(data, i); \
} \
inline iterator begin() { return iterator(Proxy(*this, 0)); } \
inline iterator end() { return iterator(Proxy(*this,data->size)); } \
Expand All @@ -115,15 +115,16 @@ public: \
} \
inline size_t size() const { return data->size; } \
inline void free() { \
if (isAllocated) { \
if (allocated && data) { \
gsl_vector##__SUFFIX__##_free(data); \
isAllocated = false; \
allocated = false; \
data = nullptr; \
} \
} \
private: \
bool isAllocated; \
bool allocated; \
}; \
template <> class matrix<__T__> { \
template <> class matrix<__T__> { \
public: \
typedef __T__ type; \
typedef __T__* pointer; \
Expand Down Expand Up @@ -164,20 +165,20 @@ public: \
const gsltype* parent; \
}; \
matrix(SEXP x) : \
data(0), isAllocated(true) { import(x); } \
matrix(gsltype* x) : data(x), isAllocated(true) {} \
data(nullptr), allocated(false) { import(x); } \
matrix(gsltype* x) : data(x), allocated(false) {} \
matrix(int nrow, int ncol) : \
data(gsl_matrix##__SUFFIX__##_alloc(nrow, ncol)), \
isAllocated(true) {} \
~matrix() {} \
allocated(true) {} \
~matrix() { free(); } \
operator gsltype*() { return data; } \
operator const gsltype*() const { return data; } \
gsltype* operator->() const { return data; } \
gsltype& operator*() const { return *data; } \
matrix(const matrix& x) : data(x.data), isAllocated(true) {} \
matrix(const matrix& x) : data(x.data), allocated(x.allocated) {} \
matrix& operator=(const matrix& other) { \
data = other.data; \
isAllocated = other.isAllocated; \
allocated = other.allocated; \
return *this; \
} \
inline size_t nrow() const { return data->size1; } \
Expand All @@ -190,14 +191,15 @@ public: \
return ConstProxy( *this, row, col); \
} \
void free(){ \
if (isAllocated) { \
if (allocated && data) { \
gsl_matrix##__SUFFIX__##_free(data); \
isAllocated = false; \
allocated = false; \
data = nullptr; \
} \
} \
private: \
inline void import(SEXP x); \
bool isAllocated; \
bool allocated; \
}; \


Expand All @@ -210,7 +212,7 @@ template <> struct matrix_view_type<__T__> { \
typedef gsl_matrix_view type; \
typedef gsl_matrix_const_view const_type; \
}; \
template <> class vector<__T__> { \
template <> class vector<__T__> { \
public: \
typedef __T__ type; \
typedef __T__* pointer; \
Expand Down Expand Up @@ -253,25 +255,25 @@ public: \
const static int RTYPE = \
::Rcpp::traits::r_sexptype_traits<type>::rtype; \
vector(SEXP x) : \
data(0), isAllocated(true) { \
data(nullptr), allocated(false) { \
SEXP y = ::Rcpp::r_cast<RTYPE>(x); \
int size = ::Rf_length(y); \
data = gsl_vector_calloc(size); \
::Rcpp::internal::export_range<__CAST__*>(y, \
reinterpret_cast<__CAST__*>(data->data)); \
} \
vector(gsltype* x) : data(x), isAllocated(true) {} \
vector(gsltype* x) : data(x), allocated(false) {} \
vector(int size) : \
data(gsl_vector_calloc(size)), isAllocated(true) {} \
~vector() {} \
data(gsl_vector_calloc(size)), allocated(true) {} \
~vector() { free(); } \
operator gsltype*() { return data; } \
operator const gsltype*() const { return data; } \
gsltype* operator->() const { return data; } \
gsltype& operator*() const { return *data; } \
vector(const vector& x) : data(x.data), isAllocated(true) {} \
vector(const vector& x) : data(x.data), allocated(x.allocated) {} \
vector& operator=(const vector& other) { \
data = other.data; \
isAllocated = other.isAllocated; \
allocated = other.allocated; \
return *this; \
} \
inline Proxy operator[](int i) { \
Expand All @@ -290,13 +292,14 @@ public: \
} \
inline size_t size() const { return data->size; } \
inline void free() { \
if (isAllocated) { \
if (allocated && data) { \
gsl_vector_free(data); \
isAllocated = false; \
allocated = false; \
data = nullptr; \
} \
} \
private: \
bool isAllocated; \
bool allocated; \
}; \
template <> class matrix<__T__> { \
public: \
Expand Down Expand Up @@ -339,19 +342,19 @@ public: \
const gsltype* parent; \
}; \
matrix(SEXP x) : \
data(0), isAllocated(true) { import(x); } \
matrix(gsltype* x) : data(x), isAllocated(true) {} \
data(nullptr), allocated(false) { import(x); } \
matrix(gsltype* x) : data(x), allocated(false) {} \
matrix(int nrow, int ncol) : \
data(gsl_matrix_alloc(nrow, ncol)), isAllocated(true) {} \
~matrix() {} \
data(gsl_matrix_alloc(nrow, ncol)), allocated(true) {} \
~matrix() { free(); } \
operator gsltype*() { return data; } \
operator const gsltype*() const { return data; } \
gsltype* operator->() const { return data; } \
gsltype& operator*() const { return *data; } \
matrix(const matrix& x) : data(x.data), isAllocated(true) {} \
matrix(const matrix& x) : data(x.data), allocated(x.allocated) {} \
matrix& operator=(const matrix& other) { \
data = other.data; \
isAllocated = other.isAllocated; \
allocated = other.allocated; \
return *this; \
} \
inline size_t nrow() const { return data->size1; } \
Expand All @@ -364,14 +367,15 @@ public: \
return ConstProxy(*this, row, col); \
} \
void free() { \
if (isAllocated) { \
if (allocated && data) { \
gsl_matrix_free(data); \
isAllocated = false; \
allocated = false; \
data = nullptr; \
} \
} \
private: \
inline void import(SEXP x); \
bool isAllocated; \
bool allocated; \
}; \

#endif