Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53223 - sandbox/memory/boost/memory
From: xushiweizh_at_[hidden]
Date: 2009-05-23 22:42:03


Author: xushiwei
Date: 2009-05-23 22:42:02 EDT (Sat, 23 May 2009)
New Revision: 53223
URL: http://svn.boost.org/trac/boost/changeset/53223

Log:
1. fixed_alloc => pool_type
2. pools_alloc::get_pool
Text files modified:
   sandbox/memory/boost/memory/block_pool.hpp | 10 +++--
   sandbox/memory/boost/memory/fixed_alloc.hpp | 58 ++++++++++++++++++++++++-------------
   sandbox/memory/boost/memory/object_pool.hpp | 49 ++++++++-----------------------
   sandbox/memory/boost/memory/pool.hpp | 56 +-----------------------------------
   sandbox/memory/boost/memory/pools_alloc.hpp | 61 ++++++++++++++++++++++++---------------
   5 files changed, 96 insertions(+), 138 deletions(-)

Modified: sandbox/memory/boost/memory/block_pool.hpp
==============================================================================
--- sandbox/memory/boost/memory/block_pool.hpp (original)
+++ sandbox/memory/boost/memory/block_pool.hpp 2009-05-23 22:42:02 EDT (Sat, 23 May 2009)
@@ -37,13 +37,15 @@
         proxy_alloc(AllocT& alloc) : m_alloc(&alloc) {}
 
 public:
+ typedef size_t size_type;
+
         enum { Padding = AllocT::Padding };
 
 public:
- __forceinline void* BOOST_MEMORY_CALL allocate(size_t cb) { return m_alloc->allocate(cb); }
- __forceinline void BOOST_MEMORY_CALL deallocate(void* p) { m_alloc->deallocate(p); }
- __forceinline void BOOST_MEMORY_CALL swap(proxy_alloc& o) { std::swap(m_alloc, o.m_alloc); }
- __forceinline size_t BOOST_MEMORY_CALL alloc_size(void* p) const { return m_alloc->alloc_size(p); }
+ __forceinline void* BOOST_MEMORY_CALL allocate(size_type cb) { return m_alloc->allocate(cb); }
+ __forceinline void BOOST_MEMORY_CALL deallocate(void* p) { m_alloc->deallocate(p); }
+ __forceinline void BOOST_MEMORY_CALL swap(proxy_alloc& o) { std::swap(m_alloc, o.m_alloc); }
+ __forceinline size_type BOOST_MEMORY_CALL alloc_size(void* p) const { return m_alloc->alloc_size(p); }
         __forceinline operator AllocT&() const { return *m_alloc; }
         __forceinline AllocT* operator&() const { return m_alloc; }
 };

Modified: sandbox/memory/boost/memory/fixed_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/fixed_alloc.hpp (original)
+++ sandbox/memory/boost/memory/fixed_alloc.hpp 2009-05-23 22:42:02 EDT (Sat, 23 May 2009)
@@ -51,6 +51,8 @@
         fixed_alloc(const fixed_alloc&);
         void operator=(const fixed_alloc&);
 
+ void swap(fixed_alloc& o); // NO SWAP: dcl_list
+
 public:
         typedef typename PolicyT::alloc_type alloc_type;
         typedef size_t size_type;
@@ -89,38 +91,54 @@
         dcl_list<FreeChunk> m_freelist;
         dcl_list<MemBlock> m_blks;
         size_type m_cbChunk;
+ size_type m_nChunkPerBlock;
         MemBlock* m_lastBlock;
+ alloc_type m_alloc;
 
-public:
- explicit fixed_alloc(size_type cbElem)
+private:
+ void BOOST_MEMORY_CALL init_(size_type cbElem)
         {
                 cbElem = ROUND(cbElem, sizeof(void*));
                 m_cbChunk = MAX(cbElem, MinElemBytes) + ChunkHeaderSize;
                 m_lastBlock = NULL;
+ m_nChunkPerBlock = BlockSize/m_cbChunk;
 
- BOOST_MEMORY_ASSERT(BlockSize/m_cbChunk > 0);
+ BOOST_MEMORY_ASSERT(m_nChunkPerBlock > 0);
+ }
+
+public:
+ explicit fixed_alloc(size_type cbElem)
+ {
+ init_(cbElem);
+ }
+
+ fixed_alloc(alloc_type alloc, size_type cbElem) : m_alloc(alloc)
+ {
+ init_(cbElem);
         }
 
-#if defined(_DEBUG)
         ~fixed_alloc()
         {
- BOOST_MEMORY_ASSERT(m_blks.empty());
- BOOST_MEMORY_ASSERT(m_freelist.empty());
+ clear();
         }
-#endif
 
         size_type BOOST_MEMORY_CALL element_size() const
         {
                 return m_cbChunk - ChunkHeaderSize;
         }
 
- void BOOST_MEMORY_CALL clear(alloc_type alloc)
+ alloc_type BOOST_MEMORY_CALL get_alloc() const
+ {
+ return m_alloc;
+ }
+
+ void BOOST_MEMORY_CALL clear()
         {
                 MemBlock* nextBlk;
                 for (MemBlock* blk = this->m_blks.first(); !this->m_blks.done(blk); blk = nextBlk)
                 {
                         nextBlk = blk->next();
- alloc.deallocate(blk);
+ m_alloc.deallocate(blk);
                 }
                 this->m_blks.clear();
                 this->m_freelist.clear();
@@ -132,16 +150,16 @@
                 return ((ChunkHeader*)p - 1)->pBlock;
         }
 
- void BOOST_MEMORY_CALL do_allocate_block_(alloc_type alloc)
+ void BOOST_MEMORY_CALL do_allocate_block_()
         {
- MemBlock* const blk = (MemBlock*)alloc.allocate(sizeof(MemBlock));
+ MemBlock* const blk = (MemBlock*)m_alloc.allocate(sizeof(MemBlock));
                 m_blks.push_front(blk);
                 m_lastBlock = blk;
 
                 blk->nUsed = 0;
 
                 char* p = blk->buffer + ChunkHeaderSize;
- for (size_type i = BlockSize/m_cbChunk; i--; p += m_cbChunk)
+ for (size_type i = m_nChunkPerBlock; i--; p += m_cbChunk)
                 {
                         chunkHeader_(p) = blk;
                         m_freelist.push_front((FreeChunk*)p);
@@ -150,24 +168,24 @@
                 BOOST_MEMORY_ASSERT(!m_freelist.empty());
         }
 
- void BOOST_MEMORY_CALL do_deallocate_block_(alloc_type alloc, MemBlock* const blk)
+ void BOOST_MEMORY_CALL do_deallocate_block_(MemBlock* const blk)
         {
                 char* p = blk->buffer + ChunkHeaderSize;
- for (size_type i = BlockSize/m_cbChunk; i--; p += m_cbChunk)
+ for (size_type i = m_nChunkPerBlock; i--; p += m_cbChunk)
                 {
                         ((FreeChunk*)p)->erase();
                 }
 
                 blk->erase();
 
- alloc.deallocate(blk);
+ m_alloc.deallocate(blk);
         }
 
 public:
- __forceinline void* BOOST_MEMORY_CALL allocate(alloc_type alloc)
+ __forceinline void* BOOST_MEMORY_CALL allocate()
         {
                 if (m_freelist.empty())
- do_allocate_block_(alloc);
+ do_allocate_block_();
 
                 void* p = &m_freelist.front();
                 ++chunkHeader_(p)->nUsed;
@@ -177,16 +195,16 @@
                 return p;
         }
 
- __forceinline void BOOST_MEMORY_CALL deallocate(alloc_type alloc, void* const p)
+ __forceinline void BOOST_MEMORY_CALL deallocate(void* const p)
         {
                 MemBlock* const blk = chunkHeader_(p);
 
- BOOST_MEMORY_ASSERT(blk->nUsed > 0 && blk->nUsed <= BlockSize/m_cbChunk);
+ BOOST_MEMORY_ASSERT(blk->nUsed > 0 && blk->nUsed <= m_nChunkPerBlock);
                 BOOST_MEMORY_DBG_FILL(p, element_size());
 
                 m_freelist.push_front((FreeChunk*)p);
                 if (--blk->nUsed == 0 && blk != m_lastBlock)
- do_deallocate_block_(alloc, blk);
+ do_deallocate_block_(blk);
         }
 };
 

Modified: sandbox/memory/boost/memory/object_pool.hpp
==============================================================================
--- sandbox/memory/boost/memory/object_pool.hpp (original)
+++ sandbox/memory/boost/memory/object_pool.hpp 2009-05-23 22:42:02 EDT (Sat, 23 May 2009)
@@ -43,9 +43,7 @@
         typedef typename PoolT::size_type size_type;
         
         using PoolT::element_size;
-
-private:
- alloc_type m_alloc;
+ using PoolT::get_alloc;
         
 protected:
         struct ChunkHeader
@@ -81,7 +79,7 @@
                 BOOST_MEMORY_STATIC_ASSERT(sizeof(ChunkHeader) == sizeof(typename PoolT::ChunkHeader));
         }
         
- explicit norm_object_pool_(alloc_type alloc) : PoolT(sizeof(Type)), m_alloc(alloc)
+ explicit norm_object_pool_(alloc_type alloc) : PoolT(m_alloc, sizeof(Type))
         {
                 BOOST_MEMORY_STATIC_ASSERT(sizeof(ChunkHeader) == sizeof(typename PoolT::ChunkHeader));
         }
@@ -91,11 +89,6 @@
                 clear();
         }
 
- alloc_type BOOST_MEMORY_CALL get_alloc() const
- {
- return m_alloc;
- }
-
 private:
         static void BOOST_MEMORY_CALL do_clear_block_(MemBlock* const blk, size_t cbChunk)
         {
@@ -133,7 +126,7 @@
         __forceinline void* BOOST_MEMORY_CALL allocate(size_t cb, destructor_t fn)
         {
                 BOOST_MEMORY_ASSERT(cb == sizeof(Type) && fn == BOOST_MEMORY_DESTRUCTOR(Type));
- void* p = PoolT::allocate(m_alloc);
+ void* p = PoolT::allocate();
                 mark_allocated_(p);
                 return p;
         }
@@ -141,7 +134,7 @@
         
         __forceinline void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, destructor_t fn) {
                 BOOST_MEMORY_ASSERT(cb == sizeof(Type) && fn == BOOST_MEMORY_DESTRUCTOR(Type));
- return PoolT::allocate(m_alloc);
+ return PoolT::allocate();
         }
 
         __forceinline void BOOST_MEMORY_CALL manage(void* p, destructor_t fn) {
@@ -150,7 +143,7 @@
         }
 
         Type* BOOST_MEMORY_CALL construct() {
- Type* p = new(PoolT::allocate(m_alloc)) Type;
+ Type* p = new(PoolT::allocate()) Type;
                 mark_allocated_(p);
                 return p;
         }
@@ -158,7 +151,7 @@
         void BOOST_MEMORY_CALL destroy(Type* obj) {
                 mark_deallocated_(obj);
                 obj->~Type();
- PoolT::deallocate(m_alloc, obj);
+ PoolT::deallocate(obj);
         }
 };
 
@@ -185,9 +178,8 @@
         typedef typename PoolT::size_type size_type;
 
         using PoolT::element_size;
-
-private:
- alloc_type m_alloc;
+ using PoolT::get_alloc;
+ using PoolT::clear;
         
 public:
         pod_object_pool_()
@@ -195,45 +187,30 @@
         }
 
         explicit pod_object_pool_(alloc_type alloc)
- : PoolT(sizeof(Type)), m_alloc(alloc) {
- }
-
- ~pod_object_pool_()
- {
- PoolT::clear(m_alloc);
- }
-
- alloc_type BOOST_MEMORY_CALL get_alloc() const
- {
- return m_alloc;
- }
-
- void BOOST_MEMORY_CALL clear()
- {
- PoolT::clear(m_alloc);
+ : PoolT(alloc, sizeof(Type)) {
         }
 
 #if defined(BOOST_MEMORY_NO_STRICT_EXCEPTION_SEMANTICS)
         __forceinline void* BOOST_MEMORY_CALL allocate(size_t cb, int fnZero) {
                 BOOST_MEMORY_ASSERT(cb == sizeof(Type));
- return PoolT::allocate(m_alloc);
+ return PoolT::allocate();
         }
 #endif
 
         __forceinline void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, int fnZero) {
                 BOOST_MEMORY_ASSERT(cb == sizeof(Type));
- return PoolT::allocate(m_alloc);
+ return PoolT::allocate();
         }
 
         __forceinline void BOOST_MEMORY_CALL manage(void* p, int fnZero) {
         }
 
         __forceinline Type* BOOST_MEMORY_CALL construct() {
- return new (PoolT::allocate(m_alloc)) Type;
+ return new (PoolT::allocate()) Type;
         }
 
         __forceinline void BOOST_MEMORY_CALL destroy(Type* obj) {
- PoolT::deallocate(m_alloc, obj);
+ PoolT::deallocate(obj);
         }
 };
 

Modified: sandbox/memory/boost/memory/pool.hpp
==============================================================================
--- sandbox/memory/boost/memory/pool.hpp (original)
+++ sandbox/memory/boost/memory/pool.hpp 2009-05-23 22:42:02 EDT (Sat, 23 May 2009)
@@ -25,60 +25,8 @@
 // -------------------------------------------------------------------------
 // class pool, scoped_pool
 
-#pragma pack(1)
-
-template <class PolicyT>
-class pool_alloc : private fixed_alloc<PolicyT>
-{
-private:
- typedef fixed_alloc<PolicyT> PoolT;
-
- pool_alloc(const pool_alloc& o);
- void operator=(const pool_alloc& o);
-
- void swap(pool_alloc& o); // NO SWAP: dcl_list
-
-public:
- typedef typename PoolT::alloc_type alloc_type;
- typedef typename PoolT::size_type size_type;
-
- using PoolT::element_size;
-
-private:
- alloc_type m_alloc;
-
-public:
- __forceinline explicit pool_alloc(size_type cbElem)
- : PoolT(cbElem) {
- }
- __forceinline explicit pool_alloc(alloc_type alloc, size_type cbElem)
- : PoolT(cbElem), m_alloc(alloc) {
- }
- __forceinline ~pool_alloc() {
- PoolT::clear(m_alloc);
- }
-
- __forceinline alloc_type BOOST_MEMORY_CALL get_alloc() const {
- return m_alloc;
- }
-
- __forceinline void BOOST_MEMORY_CALL clear() {
- PoolT::clear(m_alloc);
- }
-
- __forceinline void* BOOST_MEMORY_CALL allocate() {
- return PoolT::allocate(m_alloc);
- }
-
- __forceinline void BOOST_MEMORY_CALL deallocate(void* p) {
- return PoolT::deallocate(m_alloc, p);
- }
-};
-
-typedef pool_alloc<NS_BOOST_MEMORY_POLICY::stdlib> pool;
-typedef pool_alloc<NS_BOOST_MEMORY_POLICY::scoped> scoped_pool;
-
-#pragma pack()
+typedef fixed_alloc<NS_BOOST_MEMORY_POLICY::stdlib> pool;
+typedef fixed_alloc<NS_BOOST_MEMORY_POLICY::scoped> scoped_pool;
 
 // -------------------------------------------------------------------------
 // $Log: $

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 22:42:02 EDT (Sat, 23 May 2009)
@@ -92,12 +92,12 @@
         void swap(pools_alloc& o); // NO SWAP: dcl_list
 
 private:
- typedef fixed_alloc<PolicyT> FixedAllocT;
         typedef simple_alloc<PolicyT> LargeAllocT;
 
 public:
- typedef typename FixedAllocT::alloc_type alloc_type;
- typedef typename FixedAllocT::size_type size_type;
+ typedef fixed_alloc<PolicyT> pool_type;
+ typedef typename pool_type::alloc_type alloc_type;
+ typedef typename pool_type::size_type size_type;
 
         enum { MemBlockSize = PolicyT::MemBlockBytes - alloc_type::Padding };
 
@@ -108,7 +108,7 @@
         enum { MIN_BYTES = ALIGN };
         enum { MAX_BYTES = ALIGN * NPOOL };
         
- enum { POOL_BYTES = sizeof(FixedAllocT) };
+ enum { POOL_BYTES = sizeof(pool_type) };
         enum { POOLS_TOTAL_BYTES = POOL_BYTES * NPOOL };
 
         //
@@ -116,33 +116,32 @@
         // pool 1: ALIGN + 1 ~ 2*ALIGN
         // pool 2: ...
         //
- char m_pools[POOLS_TOTAL_BYTES];
-
- alloc_type m_alloc;
+ char m_pools[POOLS_TOTAL_BYTES];
         LargeAllocT m_large_alloc;
 
-private:
- void BOOST_MEMORY_CALL init_()
+public:
+ pools_alloc()
         {
                 BOOST_MEMORY_ASSERT(ALIGN >= sizeof(void*));
                 
                 char* p = m_pools;
                 for (size_t cb = MIN_BYTES; cb <= MAX_BYTES; cb += ALIGN)
                 {
- new(p) FixedAllocT(cb);
+ new(p) pool_type(cb);
                         p += POOL_BYTES;
                 }
         }
         
-public:
- pools_alloc()
+ explicit pools_alloc(alloc_type alloc)
         {
- init_();
- }
-
- explicit pools_alloc(alloc_type alloc) : m_alloc(alloc)
- {
- init_();
+ BOOST_MEMORY_ASSERT(ALIGN >= sizeof(void*));
+
+ char* p = m_pools;
+ for (size_t cb = MIN_BYTES; cb <= MAX_BYTES; cb += ALIGN)
+ {
+ new(p) pool_type(alloc, cb);
+ p += POOL_BYTES;
+ }
         }
 
         ~pools_alloc()
@@ -152,19 +151,33 @@
 
         alloc_type BOOST_MEMORY_CALL get_alloc() const
         {
- return m_alloc;
+ return ((pool_type*)m_pools)->get_alloc();
         }
-
+
+ pool_type& BOOST_MEMORY_CALL get_pool(size_type cb) const
+ {
+ BOOST_MEMORY_ASSERT(has_pool(cb));
+
+ const size_type index = (cb - 1) >> ALIGN_BITS;
+ return *((pool_type*)m_pools + index);
+ }
+
+ bool BOOST_MEMORY_CALL has_pool(size_type cb) const
+ {
+ const size_type index = (cb - 1) >> ALIGN_BITS;
+ return index < (size_type)NPOOL;
+ }
+
 private:
         void BOOST_MEMORY_CALL do_clear_()
         {
                 char* pEnd = m_pools + POOLS_TOTAL_BYTES;
                 for (char* p = m_pools; p != pEnd; p += POOL_BYTES)
                 {
- ((FixedAllocT*)p)->clear(m_alloc);
+ ((pool_type*)p)->clear();
                 }
         }
-
+
 public:
         void BOOST_MEMORY_CALL clear()
         {
@@ -188,7 +201,7 @@
         {
                 const size_type index = (cb - 1) >> ALIGN_BITS;
                 if (index < (size_type)NPOOL)
- return ((FixedAllocT*)m_pools + index)->allocate(m_alloc);
+ return ((pool_type*)m_pools + index)->allocate();
                 else
                         return do_allocate_(cb);
         }
@@ -198,7 +211,7 @@
         {
                 const size_type index = (cb - 1) >> ALIGN_BITS;
                 if (index < (size_type)NPOOL)
- ((FixedAllocT*)m_pools + index)->deallocate(m_alloc, p);
+ ((pool_type*)m_pools + index)->deallocate(p);
                 else if (cb != 0)
                         m_large_alloc.deallocate(p);
         }


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