diff --git a/src/lib/allocationcluster.cpp b/src/lib/allocationcluster.cpp index a9db60c20..118abbe23 100644 --- a/src/lib/allocationcluster.cpp +++ b/src/lib/allocationcluster.cpp @@ -63,8 +63,8 @@ namespace lib { size_t top_; public: - MemoryManager(TypeInfo info) { reset(info); } - ~MemoryManager() { purge(); } + MemoryManager(TypeInfo info) : top_(0) { reset(info); } + ~MemoryManager() { purge(); } void purge(); void reset(TypeInfo info); @@ -102,7 +102,7 @@ namespace lib { while (top_) type_.killIt (&mem_[--top_]); - mem_.resize(0); + mem_.clear(); }// note: unnecessary to kill pending allocations @@ -168,7 +168,7 @@ namespace lib { for (size_t i = typeHandlers_.size(); 0 < i; --i) handler(i)->purge(); - typeHandlers_.resize(0); + typeHandlers_.clear(); } catch (lumiera::Error & ex) @@ -203,7 +203,7 @@ namespace lib { Thread::Lock guard SIDEEFFECT; /////TODO: decide tradeoff: lock just the instance, or lock the AllocationCluster class? if (slot > typeHandlers_.size()) - typeHandlers_.resize(slot); + typeHandlers_.resize(slot); /////////////////////////////TODO: still a fundamental problem buried here with the way stl::vector grows... if (!handler(slot)) handler(slot).reset (new MemoryManager (type)); diff --git a/tests/common/allocationclustertest.cpp b/tests/common/allocationclustertest.cpp index 27f37cff0..001326ce1 100644 --- a/tests/common/allocationclustertest.cpp +++ b/tests/common/allocationclustertest.cpp @@ -57,7 +57,7 @@ namespace lib { char content[i]; public: - Dummy (char id) + Dummy (char id=1) { content[0] = id; checksum += id; @@ -75,6 +75,8 @@ namespace lib { { checksum -= content[0]; } + + char getID() { return content[0]; } }; typedef ScopedHolder PCluster; @@ -149,10 +151,37 @@ namespace lib { if (1 < arg.size()) NUM_OBJECTS = lexical_cast (arg[1]); if (2 < arg.size()) NUM_FAMILIES = lexical_cast (arg[2]); + simpleUsage(); checkAllocation(); checkErrorHandling(); } + + void + simpleUsage() + { + AllocationCluster clu; + + char c1(123), c2(56), c3(3), c4(4), c5(5); + Dummy<44>& ref1 = clu.create > (); + Dummy<37>& ref2 = clu.create > (c1); + Dummy<37>& ref3 = clu.create > (c2); + Dummy<1234>& rX = clu.create > (c3,c4,c5); + + ASSERT (&ref1); + ASSERT (&ref2); + ASSERT (&ref3); + ASSERT (&rX); + TRACE (test, "sizeof( Dummy<1234> ) = %d", sizeof(rX)); + + ASSERT (123==ref2.getID()); + ASSERT (3+4+5==rX.getID()); + // shows that the returned references actually + // point at the objects we created. Just use them + // and let them go. When clu goes out of scope, + // all created objects dtors will be invoked. + } + void checkAllocation() {