Boost logo

Boost-Commit :

From: daniel_james_at_[hidden]
Date: 2008-01-10 15:21:27


Author: danieljames
Date: 2008-01-10 15:21:27 EST (Thu, 10 Jan 2008)
New Revision: 42661
URL: http://svn.boost.org/trac/boost/changeset/42661

Log:
Extract some of the duplicated memory tracking code from exception.hpp and test.hpp
Added:
   branches/unordered/trunk/libs/unordered/test/objects/memory.hpp (contents, props changed)
Text files modified:
   branches/unordered/trunk/libs/unordered/test/objects/exception.hpp | 141 ++++--------------------------------
   branches/unordered/trunk/libs/unordered/test/objects/test.hpp | 156 +++------------------------------------
   2 files changed, 28 insertions(+), 269 deletions(-)

Modified: branches/unordered/trunk/libs/unordered/test/objects/exception.hpp
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/objects/exception.hpp (original)
+++ branches/unordered/trunk/libs/unordered/test/objects/exception.hpp 2008-01-10 15:21:27 EST (Thu, 10 Jan 2008)
@@ -16,6 +16,7 @@
 #include <iostream>
 #include "../helpers/fwd.hpp"
 #include "../helpers/allocator.hpp"
+#include "memory.hpp"
 #include <map>
 
 #define RUN_EXCEPTION_TESTS(test_seq, param_seq) \
@@ -53,8 +54,6 @@
 #define DISABLE_EXCEPTIONS \
     ::test::exceptions_enable BOOST_PP_CAT(ENABLE_EXCEPTIONS_, __LINE__)(false)
 
-#define HASH_CHECK(test) if(!(test)) BOOST_ERROR(BOOST_STRINGIZE(test))
-
 namespace test {
     static char const* scope = "";
     bool exceptions_enabled = false;
@@ -179,123 +178,15 @@
 {
     namespace detail
     {
- // This annoymous namespace won't cause ODR violations as I won't
- // be linking multiple translation units together. I'll probably
- // move this into a cpp file before a full release, but for now it's
- // the most convenient way.
- namespace
- {
- struct memory_area {
- void const* start;
- void const* end;
-
- memory_area(void const* s, void const* e)
- : start(s), end(e)
- {
- BOOST_ASSERT(start != end);
- }
+ struct malloc_allocator_holder {
+ template <class T> struct apply {
+ typedef test::malloc_allocator<T> type;
             };
+ };
 
- struct memory_track {
- explicit memory_track(int tag = -1) :
- tag_(tag) {}
-
- int tag_;
- };
-
- // This is a bit dodgy as it defines overlapping
- // areas as 'equal', so this isn't a total ordering.
- // But it is for non-overlapping memory regions - which
- // is what'll be stored.
- //
- // All searches will be for areas entirely contained by
- // a member of the set - so it should find the area that contains
- // the region that is searched for.
-
- struct memory_area_compare {
- bool operator()(memory_area const& x, memory_area const& y) const {
- return x.end <= y.start;
- }
- };
-
- typedef std::map<memory_area, memory_track, memory_area_compare,
- test::malloc_allocator<std::pair<memory_area const, memory_track> > >
- allocated_memory_type;
- allocated_memory_type allocated_memory;
- unsigned int count_allocators = 0;
- unsigned int count_allocations = 0;
- unsigned int count_constructions = 0;
- }
-
- void allocator_ref()
- {
- if(count_allocators == 0) {
- count_allocations = 0;
- count_constructions = 0;
- allocated_memory.clear();
- }
- ++count_allocators;
- }
-
- void allocator_unref()
- {
- HASH_CHECK(count_allocators > 0);
- if(count_allocators > 0) {
- --count_allocators;
- if(count_allocators == 0) {
- bool no_allocations_left = (count_allocations == 0);
- bool no_constructions_left = (count_constructions == 0);
- bool allocated_memory_empty = allocated_memory.empty();
-
- // Clearing the data before the checks terminate the tests.
- count_allocations = 0;
- count_constructions = 0;
- allocated_memory.clear();
-
- HASH_CHECK(no_allocations_left);
- HASH_CHECK(no_constructions_left);
- HASH_CHECK(allocated_memory_empty);
- }
- }
- }
-
- void track_allocate(void *ptr, std::size_t n, std::size_t size, int tag)
- {
- if(n == 0) {
- BOOST_ERROR("Allocating 0 length array.");
- }
- else {
- ++count_allocations;
- allocated_memory[memory_area(ptr, (char*) ptr + n * size)] =
- memory_track(tag);
- }
- }
-
- void track_deallocate(void* ptr, std::size_t n, std::size_t size, int tag)
- {
- allocated_memory_type::iterator pos
- = allocated_memory.find(memory_area(ptr, (char*) ptr + n * size));
- if(pos == allocated_memory.end()) {
- BOOST_ERROR("Deallocating unknown pointer.");
- } else {
- HASH_CHECK(pos->first.start == ptr);
- HASH_CHECK(pos->first.end == (char*) ptr + n * size);
- HASH_CHECK(pos->second.tag_ == tag);
- allocated_memory.erase(pos);
- }
- HASH_CHECK(count_allocations > 0);
- if(count_allocations > 0) --count_allocations;
- }
-
- void track_construct(void* ptr, std::size_t /*size*/, int tag)
- {
- ++count_constructions;
- }
-
- void track_destroy(void* ptr, std::size_t /*size*/, int tag)
+ namespace
         {
- HASH_CHECK(count_constructions > 0);
- if(count_constructions > 0) --count_constructions;
+ test::detail::memory_tracker<malloc_allocator_holder> tracker;
         }
     }
 
@@ -519,7 +410,7 @@
             SCOPE(allocator::allocator()) {
                 EPOINT("Mock allocator default constructor.");
             }
- detail::allocator_ref();
+ detail::tracker.allocator_ref();
         }
 
         template <class Y> allocator(allocator<Y> const& x) : tag_(x.tag_)
@@ -527,7 +418,7 @@
             SCOPE(allocator::allocator()) {
                 EPOINT("Mock allocator template copy constructor.");
             }
- detail::allocator_ref();
+ detail::tracker.allocator_ref();
         }
 
         allocator(allocator const& x) : tag_(x.tag_)
@@ -535,11 +426,11 @@
             SCOPE(allocator::allocator()) {
                 EPOINT("Mock allocator copy constructor.");
             }
- detail::allocator_ref();
+ detail::tracker.allocator_ref();
         }
 
         ~allocator() {
- detail::allocator_unref();
+ detail::tracker.allocator_unref();
         }
 
         allocator& operator=(allocator const& x) {
@@ -577,7 +468,7 @@
                 ptr = (T*) malloc(n * sizeof(T));
                 if(!ptr) throw std::bad_alloc();
             }
- detail::track_allocate((void*) ptr, n, sizeof(T), tag_);
+ detail::tracker.track_allocate((void*) ptr, n, sizeof(T), tag_);
             return pointer(ptr);
 
             //return pointer(static_cast<T*>(::operator new(n * sizeof(T))));
@@ -593,7 +484,7 @@
                 ptr = (T*) malloc(n * sizeof(T));
                 if(!ptr) throw std::bad_alloc();
             }
- detail::track_allocate((void*) ptr, n, sizeof(T), tag_);
+ detail::tracker.track_allocate((void*) ptr, n, sizeof(T), tag_);
             return pointer(ptr);
 
             //return pointer(static_cast<T*>(::operator new(n * sizeof(T))));
@@ -603,7 +494,7 @@
         {
             //::operator delete((void*) p);
             if(p) {
- detail::track_deallocate((void*) p, n, sizeof(T), tag_);
+ detail::tracker.track_deallocate((void*) p, n, sizeof(T), tag_);
                 using namespace std;
                 free(p);
             }
@@ -614,11 +505,11 @@
                 EPOINT("Mock allocator construct function.");
                 new(p) T(t);
             }
- detail::track_construct((void*) p, sizeof(T), tag_);
+ detail::tracker.track_construct((void*) p, sizeof(T), tag_);
         }
 
         void destroy(pointer p) {
- detail::track_destroy((void*) p, sizeof(T), tag_);
+ detail::tracker.track_destroy((void*) p, sizeof(T), tag_);
             p->~T();
         }
 

Added: branches/unordered/trunk/libs/unordered/test/objects/memory.hpp
==============================================================================
--- (empty file)
+++ branches/unordered/trunk/libs/unordered/test/objects/memory.hpp 2008-01-10 15:21:27 EST (Thu, 10 Jan 2008)
@@ -0,0 +1,149 @@
+
+// Copyright 2006-2007 Daniel James.
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#if !defined(BOOST_UNORDERED_TEST_MEMORY_HEADER)
+#define BOOST_UNORDERED_TEST_MEMORY_HEADER
+
+#define HASH_CHECK(test) if(!(test)) BOOST_ERROR(BOOST_STRINGIZE(test))
+
+namespace test
+{
+ namespace detail
+ {
+ // This annoymous namespace won't cause ODR violations as I won't
+ // be linking multiple translation units together. I'll probably
+ // move this into a cpp file before a full release, but for now it's
+ // the most convenient way.
+
+ struct memory_area {
+ void const* start;
+ void const* end;
+
+ memory_area(void const* s, void const* e)
+ : start(s), end(e)
+ {
+ BOOST_ASSERT(start != end);
+ }
+ };
+
+ struct memory_track {
+ explicit memory_track(int tag = -1) :
+ constructed_(0),
+ tag_(tag) {}
+
+ int constructed_;
+ int tag_;
+ };
+
+ // This is a bit dodgy as it defines overlapping
+ // areas as 'equal', so this isn't a total ordering.
+ // But it is for non-overlapping memory regions - which
+ // is what'll be stored.
+ //
+ // All searches will be for areas entirely contained by
+ // a member of the set - so it should find the area that contains
+ // the region that is searched for.
+
+ struct memory_area_compare {
+ bool operator()(memory_area const& x, memory_area const& y) const {
+ return x.end <= y.start;
+ }
+ };
+
+ struct default_allocator_holder { template <class T> struct apply {
+ typedef std::allocator<T> type; }; };
+
+ template <class AllocatorHolder = default_allocator_holder>
+ struct memory_tracker {
+ typedef std::map<memory_area, memory_track, memory_area_compare,
+ BOOST_DEDUCED_TYPENAME AllocatorHolder::
+ template apply<std::pair<memory_area const, memory_track> >::type
+ > allocated_memory_type;
+
+ allocated_memory_type allocated_memory;
+ unsigned int count_allocators;
+ unsigned int count_allocations;
+ unsigned int count_constructions;
+
+ memory_tracker() :
+ count_allocators(0), count_allocations(0),
+ count_constructions(0)
+ {}
+
+ void allocator_ref()
+ {
+ if(count_allocators == 0) {
+ count_allocations = 0;
+ count_constructions = 0;
+ allocated_memory.clear();
+ }
+ ++count_allocators;
+ }
+
+ void allocator_unref()
+ {
+ HASH_CHECK(count_allocators > 0);
+ if(count_allocators > 0) {
+ --count_allocators;
+ if(count_allocators == 0) {
+ bool no_allocations_left = (count_allocations == 0);
+ bool no_constructions_left = (count_constructions == 0);
+ bool allocated_memory_empty = allocated_memory.empty();
+
+ // Clearing the data before the checks terminate the tests.
+ count_allocations = 0;
+ count_constructions = 0;
+ allocated_memory.clear();
+
+ HASH_CHECK(no_allocations_left);
+ HASH_CHECK(no_constructions_left);
+ HASH_CHECK(allocated_memory_empty);
+ }
+ }
+ }
+
+ void track_allocate(void *ptr, std::size_t n, std::size_t size, int tag)
+ {
+ if(n == 0) {
+ BOOST_ERROR("Allocating 0 length array.");
+ }
+ else {
+ ++count_allocations;
+ allocated_memory[memory_area(ptr, (char*) ptr + n * size)] =
+ memory_track(tag);
+ }
+ }
+
+ void track_deallocate(void* ptr, std::size_t n, std::size_t size, int tag)
+ {
+ BOOST_DEDUCED_TYPENAME allocated_memory_type::iterator pos
+ = allocated_memory.find(memory_area(ptr, (char*) ptr + n * size));
+ if(pos == allocated_memory.end()) {
+ BOOST_ERROR("Deallocating unknown pointer.");
+ } else {
+ HASH_CHECK(pos->first.start == ptr);
+ HASH_CHECK(pos->first.end == (char*) ptr + n * size);
+ HASH_CHECK(pos->second.tag_ == tag);
+ allocated_memory.erase(pos);
+ }
+ HASH_CHECK(count_allocations > 0);
+ if(count_allocations > 0) --count_allocations;
+ }
+
+ void track_construct(void* ptr, std::size_t /*size*/, int tag)
+ {
+ ++count_constructions;
+ }
+
+ void track_destroy(void* ptr, std::size_t /*size*/, int tag)
+ {
+ HASH_CHECK(count_constructions > 0);
+ if(count_constructions > 0) --count_constructions;
+ }
+ };
+ }
+}
+
+#endif

Modified: branches/unordered/trunk/libs/unordered/test/objects/test.hpp
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/objects/test.hpp (original)
+++ branches/unordered/trunk/libs/unordered/test/objects/test.hpp 2008-01-10 15:21:27 EST (Thu, 10 Jan 2008)
@@ -9,8 +9,9 @@
 #include <boost/config.hpp>
 #include <boost/limits.hpp>
 #include <cstddef>
-#include "../helpers/fwd.hpp"
 #include <iostream>
+#include "../helpers/fwd.hpp"
+#include "memory.hpp"
 #include <map>
 
 namespace test
@@ -203,141 +204,8 @@
 
     namespace detail
     {
- // This annoymous namespace won't cause ODR violations as I won't
- // be linking multiple translation units together. I'll probably
- // move this into a cpp file before a full release, but for now it's
- // the most convenient way.
         namespace {
- struct memory_area {
- void const* start;
- void const* end;
-
- memory_area(void const* s, void const* e)
- : start(s), end(e)
- {
- BOOST_ASSERT(start != end);
- }
- };
-
- struct memory_track {
- explicit memory_track(int tag = -1) :
- constructed_(0),
- tag_(tag) {}
-
- int constructed_;
- int tag_;
- };
-
- // This is a bit dodgy as it defines overlapping
- // areas as 'equal', so this isn't a total ordering.
- // But it is for non-overlapping memory regions - which
- // is what'll be stored.
- //
- // All searches will be for areas entirely contained by
- // a member of the set - so it should find the area that contains
- // the region that is searched for.
-
- struct memory_area_compare {
- bool operator()(memory_area const& x, memory_area const& y) const {
- return x.end <= y.start;
- }
- };
-
- typedef std::map<memory_area, memory_track, memory_area_compare> allocated_memory_type;
- allocated_memory_type allocated_memory;
- unsigned int count_allocators = 0;
- unsigned int count_allocations = 0;
- unsigned int count_constructions = 0;
- }
-
- void allocator_ref()
- {
- if(count_allocators == 0) {
- count_allocations = 0;
- count_constructions = 0;
- allocated_memory.clear();
- }
- ++count_allocators;
- }
-
- void allocator_unref()
- {
- BOOST_TEST(count_allocators > 0);
- if(count_allocators > 0) {
- --count_allocators;
- if(count_allocators == 0) {
- bool no_allocations_left = (count_allocations == 0);
- bool no_constructions_left = (count_constructions == 0);
- bool allocated_memory_empty = allocated_memory.empty();
-
- // Clearing the data before the checks terminate the tests.
- count_allocations = 0;
- count_constructions = 0;
- allocated_memory.clear();
-
- BOOST_TEST(no_allocations_left);
- BOOST_TEST(no_constructions_left);
- BOOST_TEST(allocated_memory_empty);
- }
- }
- }
-
- void track_allocate(void *ptr, std::size_t n, std::size_t size, int tag)
- {
- if(n == 0) {
- BOOST_ERROR("Allocating 0 length array.");
- }
- else {
- ++count_allocations;
- allocated_memory[memory_area(ptr, (char*) ptr + n * size)] =
- memory_track(tag);
- }
- }
-
- void track_deallocate(void* ptr, std::size_t n, std::size_t size, int tag)
- {
- allocated_memory_type::iterator pos
- = allocated_memory.find(memory_area(ptr, (char*) ptr + n * size));
- if(pos == allocated_memory.end()) {
- BOOST_ERROR("Deallocating unknown pointer.");
- } else {
- BOOST_TEST(pos->first.start == ptr);
- BOOST_TEST(pos->first.end == (char*) ptr + n * size);
- BOOST_TEST(pos->second.tag_ == tag);
- BOOST_TEST(pos->second.constructed_ == 0);
- allocated_memory.erase(pos);
- }
- BOOST_TEST(count_allocations > 0);
- if(count_allocations > 0) --count_allocations;
- }
-
- void track_construct(void* ptr, std::size_t size, int tag)
- {
- allocated_memory_type::iterator pos
- = allocated_memory.find(memory_area(ptr, (char*) ptr + size));
- if(pos == allocated_memory.end()) {
- BOOST_ERROR("Constructing unknown pointer.");
- }
- else {
- BOOST_TEST(pos->second.tag_ == tag);
- ++pos->second.constructed_;
- }
- ++count_constructions;
- }
-
- void track_destroy(void* ptr, std::size_t size, int tag)
- {
- allocated_memory_type::iterator pos
- = allocated_memory.find(memory_area(ptr, (char*) ptr + size));
- if(pos == allocated_memory.end())
- BOOST_ERROR("Destroying unknown pointer.");
- else {
- BOOST_TEST(pos->second.tag_ == tag);
- BOOST_TEST(pos->second.constructed_ > 0);
- if(pos->second.constructed_ > 0) --pos->second.constructed_;
- }
- BOOST_TEST(count_constructions > 0);
- if(count_constructions > 0) --count_constructions;
+ test::detail::memory_tracker<test::detail::default_allocator_holder> tracker;
         }
     }
 
@@ -361,40 +229,40 @@
 
         template <class U> struct rebind { typedef allocator<U> other; };
 
- explicit allocator(int t = 0) : tag_(t) { detail::allocator_ref(); }
- template <class Y> allocator(allocator<Y> const& x) : tag_(x.tag_) { detail::allocator_ref(); }
- allocator(allocator const& x) : tag_(x.tag_) { detail::allocator_ref(); }
- ~allocator() { detail::allocator_unref(); }
+ explicit allocator(int t = 0) : tag_(t) { detail::tracker.allocator_ref(); }
+ template <class Y> allocator(allocator<Y> const& x) : tag_(x.tag_) { detail::tracker.allocator_ref(); }
+ allocator(allocator const& x) : tag_(x.tag_) { detail::tracker.allocator_ref(); }
+ ~allocator() { detail::tracker.allocator_unref(); }
 
         pointer address(reference r) { return pointer(&r); }
         const_pointer address(const_reference r) { return const_pointer(&r); }
 
         pointer allocate(size_type n) {
             pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
- detail::track_allocate((void*) ptr, n, sizeof(T), tag_);
+ detail::tracker.track_allocate((void*) ptr, n, sizeof(T), tag_);
             return ptr;
         }
 
         pointer allocate(size_type n, const_pointer u)
         {
             pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
- detail::track_allocate((void*) ptr, n, sizeof(T), tag_);
+ detail::tracker.track_allocate((void*) ptr, n, sizeof(T), tag_);
             return ptr;
         }
 
         void deallocate(pointer p, size_type n)
         {
- detail::track_deallocate((void*) p, n, sizeof(T), tag_);
+ detail::tracker.track_deallocate((void*) p, n, sizeof(T), tag_);
             ::operator delete((void*) p);
         }
 
         void construct(pointer p, T const& t) {
- detail::track_construct((void*) p, sizeof(T), tag_);
+ detail::tracker.track_construct((void*) p, sizeof(T), tag_);
             new(p) T(t);
         }
 
         void destroy(pointer p) {
- detail::track_destroy((void*) p, sizeof(T), tag_);
+ detail::tracker.track_destroy((void*) p, sizeof(T), tag_);
             p->~T();
         }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk