Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52547 - in sandbox/memory: boost/memory libs/memory/examples/pool
From: xushiweizh_at_[hidden]
Date: 2009-04-22 13:39:29


Author: xushiwei
Date: 2009-04-22 13:39:27 EDT (Wed, 22 Apr 2009)
New Revision: 52547
URL: http://svn.boost.org/trac/boost/changeset/52547

Log:
1. check last block (fixed_alloc::m_lastBlock)
2. example pool: testScopedPool
Text files modified:
   sandbox/memory/boost/memory/fixed_alloc.hpp | 5 ++++-
   sandbox/memory/libs/memory/examples/pool/simple_examples.cpp | 28 ++++++++++++++++++++++++++++
   2 files changed, 32 insertions(+), 1 deletions(-)

Modified: sandbox/memory/boost/memory/fixed_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/fixed_alloc.hpp (original)
+++ sandbox/memory/boost/memory/fixed_alloc.hpp 2009-04-22 13:39:27 EDT (Wed, 22 Apr 2009)
@@ -81,6 +81,7 @@
         dcl_list<FreeChunk> m_freelist;
         size_type m_cbChunk;
         size_type m_nMaxPerBlock;
+ MemBlock* m_lastBlock;
 
 private:
         void init_(size_type cbElem)
@@ -88,6 +89,7 @@
                 cbElem = ROUND(cbElem, sizeof(void*));
                 m_cbChunk = MAX(cbElem, MinElemBytes) + ChunkHeaderSize;
                 m_nMaxPerBlock = BlockSize / m_cbChunk;
+ m_lastBlock = NULL;
 
                 BOOST_MEMORY_ASSERT(m_nMaxPerBlock > 0);
         }
@@ -125,6 +127,7 @@
         {
                 MemBlock* const blk = (MemBlock*)m_alloc.allocate(sizeof(MemBlock));
                 m_blks.push_front(blk);
+ m_lastBlock = blk;
 
                 blk->nUsed = 0;
 
@@ -182,7 +185,7 @@
                 MemBlock* const blk = chunkHeader_(p);
                 if (--blk->nUsed > 0)
                         m_freelist.push_front((FreeChunk*)p);
- else
+ else if (blk != m_lastBlock)
                         do_deallocate_block_(blk);
         }
 };

Modified: sandbox/memory/libs/memory/examples/pool/simple_examples.cpp
==============================================================================
--- sandbox/memory/libs/memory/examples/pool/simple_examples.cpp (original)
+++ sandbox/memory/libs/memory/examples/pool/simple_examples.cpp 2009-04-22 13:39:27 EDT (Wed, 22 Apr 2009)
@@ -43,10 +43,38 @@
                 alloc.allocate();
 }
 
+void testScopedPool()
+{
+ int i;
+ boost::memory::block_pool recycle;
+ boost::memory::scoped_pool alloc(recycle, sizeof(int));
+
+ void* p[3000];
+ for (i = 0; i < countof(p); ++i)
+ p[i] = alloc.allocate();
+ for (i = 0; i < countof(p); ++i)
+ alloc.deallocate(p[i]);
+
+ void* p1 = alloc.allocate();
+ void* p2 = alloc.allocate();
+ void* p3 = alloc.allocate();
+
+ alloc.deallocate(p2);
+ alloc.deallocate(p1);
+ alloc.deallocate(p3);
+
+ //
+ // Note: It's ok even if you forget to free allocated memory!
+ //
+ for (i = 0; i < 1000; ++i)
+ alloc.allocate();
+}
+
 int main()
 {
         NS_BOOST_MEMORY::enableMemoryLeakCheck();
 
         testPool();
+ testScopedPool();
         return 0;
 }


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