From f505c46d1d83c49fa525d97d414a756a578c0f25 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 25 Sep 2011 19:23:45 +0200 Subject: [PATCH] finish simple allocator frontend. Unit-test pass --- src/lib/simple-allocator.hpp | 87 ++++++++++++++++++++++++----- tests/40components.tests | 2 +- tests/lib/simple-allocator-test.cpp | 16 ++++-- 3 files changed, 86 insertions(+), 19 deletions(-) diff --git a/src/lib/simple-allocator.hpp b/src/lib/simple-allocator.hpp index 0cbf6b5c9..b79b2081d 100644 --- a/src/lib/simple-allocator.hpp +++ b/src/lib/simple-allocator.hpp @@ -51,11 +51,9 @@ #include "lib/meta/generator.hpp" #include "lib/meta/typelist-util.hpp" #include "lib/format.hpp" -//#include "lib/typed-counter.hpp" +#include "lib/typed-counter.hpp" #include "include/logging.h" - -//#include #include #include @@ -63,24 +61,71 @@ namespace lib { -// using std::tr1::shared_ptr; using lumiera::typelist::Types; using lumiera::typelist::IsInList; using lumiera::typelist::InstantiateForEach; - namespace { - } + /* === Policies for simple custom allocator === */ + + /** + * Policy: use just plain heap allocations + */ template class CustomAllocator : public std::allocator + { }; + + + /** + * Policy: maintain explicit per type instance count + * @note this imposes additional locking + */ + struct UseInstantiationCounting { + template + size_t + allocationCount() const + { + return allocCnt_.get(); + } + template + void + incrementCount() + { + allocCnt_.inc(); + } + + template + void + decrementCount() + { + allocCnt_.dec(); + } + + private: + lib::TypedCounter allocCnt_; }; + /** + * Policy: no additional instantiation accounting + */ + struct NoInstantiationCount + { + template size_t allocationCount() const { return 0; } + template void incrementCount() { /* NOP */ } + template void decrementCount() { /* NOP */ } + }; + + + + + /* === Allocator frontend === */ + /** * Frontend for explicit allocations, using a custom allocator. * This template is to be instantiated for the collection of types @@ -90,13 +135,16 @@ namespace lib { * * @todo currently (as of 8/09) the low-level pooled allocator * isn't implemented; instead we do just heap allocations. - * see Ticket #835 + * ////////////////////////////////////////////////////////////////////////////////////////////Ticket #835 */ - template + template class SimpleAllocator - : InstantiateForEach< typename TYPES::List // for each of those types... - , CustomAllocator // ...mix in the custom allocator + : InstantiateForEach< typename TYPES::List // for each of those types... + , CustomAllocator // ...mix in the custom allocator > + , COUNTER { /** forward plain memory allocation */ @@ -105,7 +153,9 @@ namespace lib { allocateSlot () { TRACE (memory, "allocate %s", util::tyStr().c_str()); - return CustomAllocator::allocate (1); + XX * newStorage = CustomAllocator::allocate (1); + COUNTER::template incrementCount(); + return newStorage; } template @@ -114,6 +164,7 @@ namespace lib { { TRACE (memory, "release %s", util::tyStr().c_str()); CustomAllocator::deallocate (entry, 1); + COUNTER::template decrementCount(); } @@ -126,8 +177,8 @@ namespace lib { BOOST_STATIC_ASSERT (IsSupportedType::value); } - - + + public: /* ==== build objects with managed allocation ==== */ @@ -234,10 +285,18 @@ namespace lib { releaseSlot (entry); } + + /** diagnostics */ + template + size_t + numSlots() const + { + return COUNTER::template allocationCount(); + } }; - + } // namespace lib #endif diff --git a/tests/40components.tests b/tests/40components.tests index d0f849145..615fe5bfe 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -1141,7 +1141,7 @@ return: 0 END -PLANNED "custom allocator(I)" SimpleAllocator_test < #include #include @@ -36,7 +34,6 @@ namespace test{ using util::isSameObject; -// using std::tr1::shared_ptr; using std::string; using std::rand; @@ -62,6 +59,13 @@ namespace test{ checksum_ += (crap_[i] = rand() % 128); } + DummyObj (DummyObj const& o) + { + REQUIRE (siz); + for (uint i=0; i,DummyObj<23>,string> > TestAllocator; + typedef Types,DummyObj<23>,string> SupportedTypes; + typedef SimpleAllocator TestAllocator; } @@ -148,6 +153,9 @@ namespace test{ allocator.destroy (pDxx); allocator.destroy (pSxx); + CHECK (0 == allocator.numSlots >()); + CHECK (0 == allocator.numSlots >()); + CHECK (0 == allocator.numSlots()); CHECK (0 == checksum_); } };