[GIT] ppl/ppl(master): Avoid pointer casts when allocating coefficients in Dense_Row::Impl.

Module: ppl/ppl Branch: master Commit: 2622a438e6d247763b436b8eeefe2c15dd9bc7f1 URL: http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=2622a438e6d24...
Author: Enea Zaffanella zaffanella@cs.unipr.it Date: Mon Feb 13 14:54:20 2012 +0100
Avoid pointer casts when allocating coefficients in Dense_Row::Impl. Detected by ECLAIR service M++.5-2-8.
We now use a std::allocator object (similar to what done in CO_Tree).
---
src/Dense_Row.cc | 22 ++++++++-------------- src/Dense_Row.defs.hh | 4 ++++ src/Dense_Row.inlines.hh | 23 +++++++++++------------ 3 files changed, 23 insertions(+), 26 deletions(-)
diff --git a/src/Dense_Row.cc b/src/Dense_Row.cc index 057e61b..031a57e 100644 --- a/src/Dense_Row.cc +++ b/src/Dense_Row.cc @@ -40,14 +40,12 @@ PPL::Dense_Row::resize(dimension_type new_size) { // Reallocation is required. // TODO: Consider using realloc() here. // TODO: Consider using a smarter allocation strategy. - dimension_type new_capacity = new_size; - Coefficient* new_vec = static_cast<Coefficient*>( - operator new(sizeof(Coefficient) * new_capacity)); + const dimension_type new_capacity = new_size; + Coefficient* new_vec = impl.coeff_allocator.allocate(new_capacity);
if (impl.vec != 0) { memcpy(new_vec, impl.vec, sizeof(Coefficient) * impl.size); - - operator delete(impl.vec); + impl.coeff_allocator.deallocate(impl.vec, impl.capacity); }
impl.vec = new_vec; @@ -87,27 +85,24 @@ PPL::Dense_Row::resize(dimension_type new_size, dimension_type new_capacity) {
PPL_ASSERT(impl.size == new_size);
- Coefficient* new_vec = static_cast<Coefficient*>( - operator new(sizeof(Coefficient) * new_capacity)); + Coefficient* new_vec = impl.coeff_allocator.allocate(new_capacity);
PPL_ASSERT(impl.vec != 0);
memcpy(new_vec, impl.vec, sizeof(Coefficient) * impl.size);
- operator delete(impl.vec); + impl.coeff_allocator.deallocate(impl.vec, impl.capacity);
impl.vec = new_vec; impl.capacity = new_capacity; } else { if (new_capacity > capacity()) {
- Coefficient* new_vec = static_cast<Coefficient*>( - operator new(sizeof(Coefficient) * new_capacity)); + Coefficient* new_vec = impl.coeff_allocator.allocate(new_capacity);
if (impl.vec != 0) { memcpy(new_vec, impl.vec, sizeof(Coefficient) * impl.size); - - operator delete(impl.vec); + impl.coeff_allocator.deallocate(impl.vec, impl.capacity); }
impl.vec = new_vec; @@ -163,8 +158,7 @@ void PPL::Dense_Row::init(const Sparse_Row& row) { impl.capacity = row.size(); impl.flags = row.flags(); - impl.vec = static_cast<Coefficient*>( - operator new(sizeof(Coefficient) * impl.capacity)); + impl.vec = impl.coeff_allocator.allocate(impl.capacity); Sparse_Row::const_iterator itr = row.begin(); Sparse_Row::const_iterator itr_end = row.end(); while (impl.size != impl.capacity) { diff --git a/src/Dense_Row.defs.hh b/src/Dense_Row.defs.hh index ea3d5ff..0bcbd50 100644 --- a/src/Dense_Row.defs.hh +++ b/src/Dense_Row.defs.hh @@ -31,6 +31,7 @@ site: http://bugseng.com/products/ppl/ . */ #include "Sparse_Row.types.hh" #include "Row_Flags.defs.hh" #include "Coefficient.defs.hh" +#include <memory> #include <vector> #include <limits>
@@ -393,6 +394,9 @@ private: //! The flags of this row. Row_Flags flags;
+ //! The allocator used to allocate/deallocate vec. + std::allocator<Coefficient> coeff_allocator; + //! The vector of coefficients. //! An empty vector may be stored as NULL instead of using a valid pointer. Coefficient* vec; diff --git a/src/Dense_Row.inlines.hh b/src/Dense_Row.inlines.hh index ba5c037..00d78d2 100644 --- a/src/Dense_Row.inlines.hh +++ b/src/Dense_Row.inlines.hh @@ -37,7 +37,7 @@ namespace Parma_Polyhedra_Library {
inline Dense_Row::Impl::Impl() - : size(0), capacity(0), flags(), vec(0) { + : size(0), capacity(0), flags(), coeff_allocator(), vec(0) { }
inline @@ -46,7 +46,7 @@ Dense_Row::Impl::~Impl() { --size; vec[size].~Coefficient(); } - operator delete(vec); + coeff_allocator.deallocate(vec, capacity); }
inline dimension_type @@ -120,11 +120,11 @@ Dense_Row::Dense_Row(const Dense_Row& y) : impl() {
impl.flags = y.flags(); + impl.coeff_allocator = y.impl.coeff_allocator;
if (y.impl.vec != 0) { impl.capacity = y.capacity(); - impl.vec = static_cast<Coefficient*>( - operator new(sizeof(Coefficient) * impl.capacity)); + impl.vec = impl.coeff_allocator.allocate(impl.capacity); while (impl.size != y.size()) { new (&impl.vec[impl.size]) Coefficient(y[impl.size]); ++impl.size; @@ -143,10 +143,9 @@ Dense_Row::Dense_Row(const Dense_Row& y, PPL_ASSERT(capacity <= max_size());
impl.flags = y.flags(); - - impl.vec = static_cast<Coefficient*>( - operator new(sizeof(Coefficient) * capacity)); impl.capacity = capacity; + impl.coeff_allocator = y.impl.coeff_allocator; + impl.vec = impl.coeff_allocator.allocate(impl.capacity);
if (y.impl.vec != 0) { while (impl.size != y.size()) { @@ -171,12 +170,11 @@ Dense_Row::Dense_Row(const Dense_Row& y, PPL_ASSERT(capacity != 0);
impl.flags = y.flags(); - - impl.vec = static_cast<Coefficient*>( - operator new(sizeof(Coefficient) * capacity)); impl.capacity = capacity; + impl.coeff_allocator = y.impl.coeff_allocator; + impl.vec = impl.coeff_allocator.allocate(impl.capacity);
- dimension_type n = std::min(sz, y.size()); + const dimension_type n = std::min(sz, y.size()); while (impl.size != n) { new (&impl.vec[impl.size]) Coefficient(y[impl.size]); ++impl.size; @@ -199,7 +197,7 @@ Dense_Row::~Dense_Row() { inline void Dense_Row::destroy() { resize(0); - operator delete(impl.vec); + impl.coeff_allocator.deallocate(impl.vec, impl.capacity); }
inline void @@ -208,6 +206,7 @@ Dense_Row::m_swap(Dense_Row& y) { swap(impl.size, y.impl.size); swap(impl.capacity, y.impl.capacity); swap(impl.flags, y.impl.flags); + swap(impl.coeff_allocator, y.impl.coeff_allocator); swap(impl.vec, y.impl.vec); PPL_ASSERT(OK()); PPL_ASSERT(y.OK());
participants (1)
-
Enea Zaffanella