Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53200 - in sandbox/memory: boost/memory libs/memory/examples/pool
From: xushiweizh_at_[hidden]
Date: 2009-05-23 04:24:38


Author: xushiwei
Date: 2009-05-23 04:24:36 EDT (Sat, 23 May 2009)
New Revision: 53200
URL: http://svn.boost.org/trac/boost/changeset/53200

Log:
boost/memory/pools_alloc.hpp
Text files modified:
   sandbox/memory/boost/memory/pools_alloc.hpp | 29 +++++++++++++++--------------
   sandbox/memory/boost/memory/system_alloc.hpp | 1 +
   sandbox/memory/libs/memory/examples/pool/simple_examples.cpp | 15 +++++++++++++++
   3 files changed, 31 insertions(+), 14 deletions(-)

Modified: sandbox/memory/boost/memory/pools_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/pools_alloc.hpp (original)
+++ sandbox/memory/boost/memory/pools_alloc.hpp 2009-05-23 04:24:36 EDT (Sat, 23 May 2009)
@@ -37,15 +37,16 @@
         void swap(simple_alloc& o); // NO SWAP: dcl_list
 
 public:
- typedef typename PolicyT::alloc_type alloc_type;
+ typedef typename PolicyT::system_alloc_type alloc_type;
+ typedef size_t size_type;
         
 private:
- struct Chunk {};
- typedef dcl_list_node<Chunk> MemHeader;
-
- dcl_list<Chunk> m_chunks;
+ struct MemHeader : dcl_list_node<MemHeader> {};
+
+ dcl_list<MemHeader> m_chunks;
 
 public:
+ simple_alloc() {}
         ~simple_alloc() {
                 clear();
         }
@@ -60,15 +61,15 @@
                 }
                 m_chunks.clear();
         }
-
+
         void* BOOST_MEMORY_CALL allocate(size_type cb) {
                 MemHeader* p = (MemHeader*)alloc_type::allocate(cb + sizeof(MemHeader));
                 m_chunks.push_front(p);
                 return p + 1;
         }
-
- void BOOST_MEMORY_CALL deallocate(void* p) {
- MemHeader* p = (MemHeader*)p - 1;
+
+ void BOOST_MEMORY_CALL deallocate(void* ptr) {
+ MemHeader* p = (MemHeader*)ptr - 1;
                 p->erase();
                 alloc_type::deallocate(p);
         }
@@ -157,7 +158,7 @@
 private:
         void BOOST_MEMORY_CALL do_clear_()
         {
- char* pEnd = p + POOLS_TOTAL_BYTES;
+ char* pEnd = m_pools + POOLS_TOTAL_BYTES;
                 for (char* p = m_pools; p != pEnd; p += POOL_BYTES)
                 {
                         ((FixedAllocT*)p)->clear(m_alloc);
@@ -176,11 +177,11 @@
         {
                 const size_type index = (cb - 1) >> ALIGN_BITS;
                 if (index < NPOOL)
- return ((FixedAllocT*)p + index)->allocate(m_alloc);
+ return ((FixedAllocT*)m_pools + index)->allocate(m_alloc);
                 else if (cb != 0)
                         return m_large_alloc.allocate(cb);
                 else
- return &cb;
+ return this + 1;
         }
 
 public:
@@ -188,7 +189,7 @@
         {
                 const size_type index = (cb - 1) >> ALIGN_BITS;
                 if (index < NPOOL)
- ((FixedAllocT*)p + index)->deallocate(m_alloc, p);
+ ((FixedAllocT*)m_pools + index)->deallocate(m_alloc, p);
                 else if (cb != 0)
                         m_large_alloc.deallocate(p);
         }
@@ -202,7 +203,7 @@
                 if (oldIndex == newIndex && oldIndex < NPOOL)
                         return p;
                 void* p2 = allocate(newSize);
- memcpy(p2, p, oldSize);
+ memcpy(p2, p, MIN(oldSize, newSize));
                 deallocate(p, oldSize);
                 return p2;
         }

Modified: sandbox/memory/boost/memory/system_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/system_alloc.hpp (original)
+++ sandbox/memory/boost/memory/system_alloc.hpp 2009-05-23 04:24:36 EDT (Sat, 23 May 2009)
@@ -80,6 +80,7 @@
 
 public:
         typedef system_alloc alloc_type;
+ typedef system_alloc system_alloc_type;
 };
 
 class stdlib : public sys

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-05-23 04:24:36 EDT (Sat, 23 May 2009)
@@ -10,6 +10,7 @@
 // See http://www.boost.org/libs/memory/index.htm for documentation.
 //
 #include <boost/memory.hpp>
+#include <boost/memory/pools_alloc.hpp>
 
 // -------------------------------------------------------------------------
 
@@ -114,10 +115,24 @@
                 printf("ERROR: memory leaks!\n");
 }
 
+void testScopedPools()
+{
+ size_t i;
+ boost::memory::block_pool recycle;
+ boost::memory::pools_alloc<boost::memory::policy::scoped> alloc(recycle);
+
+ void* p[22];
+ for (i = 0; i < countof(p); ++i)
+ p[i] = alloc.allocate(i * 7);
+ for (i = 3; i < countof(p) - 2; ++i)
+ alloc.deallocate(p[i], i * 7);
+}
+
 int main()
 {
         NS_BOOST_MEMORY::enableMemoryLeakCheck();
 
+ testScopedPools();
         testPool();
         testScopedPool();
         testObjectPool();


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