Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53195 - sandbox/memory/boost/memory
From: xushiweizh_at_[hidden]
Date: 2009-05-23 01:03:26


Author: xushiwei
Date: 2009-05-23 01:03:23 EDT (Sat, 23 May 2009)
New Revision: 53195
URL: http://svn.boost.org/trac/boost/changeset/53195

Log:
refactor fixed_alloc, pool, object_pool

Text files modified:
   sandbox/memory/boost/memory/block_pool.hpp | 16 ++++--
   sandbox/memory/boost/memory/fixed_alloc.hpp | 69 ++++++++++---------------------
   sandbox/memory/boost/memory/object_pool.hpp | 86 +++++++++++++++++++++++++++------------
   sandbox/memory/boost/memory/pool.hpp | 54 +++++++++++++++++++++++-
   sandbox/memory/boost/memory/region_alloc.hpp | 6 +
   sandbox/memory/boost/memory/system_alloc.hpp | 4 +
   6 files changed, 152 insertions(+), 83 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 01:03:23 EDT (Sat, 23 May 2009)
@@ -25,6 +25,8 @@
 // -------------------------------------------------------------------------
 // class proxy_alloc
 
+#pragma pack(1)
+
 template <class AllocT>
 class proxy_alloc
 {
@@ -46,13 +48,15 @@
         __forceinline AllocT* operator&() const { return m_alloc; }
 };
 
+#pragma pack()
+
 // -------------------------------------------------------------------------
 // class block_pool
 
 #pragma pack(1)
 
 template <class PolicyT>
-class block_pool_
+class block_alloc
 {
 private:
         typedef typename PolicyT::alloc_type AllocT;
@@ -68,16 +72,16 @@
         const int m_nFreeLimit;
 
 private:
- block_pool_(const block_pool_&);
- void operator=(const block_pool_&);
+ block_alloc(const block_alloc&);
+ void operator=(const block_alloc&);
 
 public:
- block_pool_(int cbFreeLimit = INT_MAX)
+ block_alloc(int cbFreeLimit = INT_MAX)
                 : m_freeList(NULL), m_nFree(0),
                   m_nFreeLimit(cbFreeLimit / m_cbBlock)
         {
         }
- ~block_pool_()
+ ~block_alloc()
         {
                 clear();
         }
@@ -137,7 +141,7 @@
         }
 };
 
-typedef block_pool_<NS_BOOST_MEMORY_POLICY::sys> block_pool;
+typedef block_alloc<NS_BOOST_MEMORY_POLICY::sys> block_pool;
 
 #pragma pack()
 

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 01:03:23 EDT (Sat, 23 May 2009)
@@ -48,17 +48,15 @@
 class fixed_alloc
 {
 private:
- typedef typename PolicyT::alloc_type AllocT;
-
         fixed_alloc(const fixed_alloc&);
         void operator=(const fixed_alloc&);
 
 public:
- enum { MemBlockSize = PolicyT::MemBlockBytes - AllocT::Padding };
-
- typedef AllocT alloc_type;
+ typedef typename PolicyT::alloc_type alloc_type;
         typedef size_t size_type;
 
+ enum { MemBlockSize = PolicyT::MemBlockBytes - alloc_type::Padding };
+
 protected:
         struct MemBlock;
         friend struct MemBlock;
@@ -90,60 +88,39 @@
 
         dcl_list<FreeChunk> m_freelist;
         dcl_list<MemBlock> m_blks;
- AllocT m_alloc;
         size_type m_cbChunk;
- size_type m_nMaxPerBlock;
         MemBlock* m_lastBlock;
 
-private:
- void init_(size_type cbElem)
+public:
+ explicit fixed_alloc(size_type cbElem)
         {
                 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);
- }
-
-public:
- explicit fixed_alloc(size_type cbElem)
- {
- init_(cbElem);
- }
- fixed_alloc(AllocT alloc, size_type cbElem)
- : m_alloc(alloc)
- {
- init_(cbElem);
+ BOOST_MEMORY_ASSERT(BlockSize/m_cbChunk > 0);
         }
 
+#if defined(_DEBUG)
         ~fixed_alloc()
         {
- clear();
+ BOOST_MEMORY_ASSERT(m_blks.empty());
+ BOOST_MEMORY_ASSERT(m_freelist.empty());
         }
+#endif
 
         size_type BOOST_MEMORY_CALL element_size() const
         {
                 return m_cbChunk - ChunkHeaderSize;
         }
 
- alloc_type BOOST_MEMORY_CALL get_alloc() const
- {
- return m_alloc;
- }
-
- void BOOST_MEMORY_CALL swap(fixed_alloc& o)
- {
- swap_object(this, &o);
- }
-
- void BOOST_MEMORY_CALL clear()
+ void BOOST_MEMORY_CALL clear(alloc_type alloc)
         {
                 MemBlock* nextBlk;
                 for (MemBlock* blk = this->m_blks.first(); !this->m_blks.done(blk); blk = nextBlk)
                 {
                         nextBlk = blk->next();
- this->m_alloc.deallocate(blk);
+ alloc.deallocate(blk);
                 }
                 this->m_blks.clear();
                 this->m_freelist.clear();
@@ -155,16 +132,16 @@
                 return ((ChunkHeader*)p - 1)->pBlock;
         }
 
- void BOOST_MEMORY_CALL do_allocate_block_()
+ void BOOST_MEMORY_CALL do_allocate_block_(alloc_type alloc)
         {
- MemBlock* const blk = (MemBlock*)m_alloc.allocate(sizeof(MemBlock));
+ MemBlock* const blk = (MemBlock*)alloc.allocate(sizeof(MemBlock));
                 m_blks.push_front(blk);
                 m_lastBlock = blk;
 
                 blk->nUsed = 0;
 
                 char* p = blk->buffer + ChunkHeaderSize;
- for (size_type i = m_nMaxPerBlock; i--; p += m_cbChunk)
+ for (size_type i = BlockSize/m_cbChunk; i--; p += m_cbChunk)
                 {
                         chunkHeader_(p) = blk;
                         m_freelist.push_front((FreeChunk*)p);
@@ -173,24 +150,24 @@
                 BOOST_MEMORY_ASSERT(!m_freelist.empty());
         }
 
- void BOOST_MEMORY_CALL do_deallocate_block_(MemBlock* const blk)
+ void BOOST_MEMORY_CALL do_deallocate_block_(alloc_type alloc, MemBlock* const blk)
         {
                 char* p = blk->buffer + ChunkHeaderSize;
- for (size_type i = m_nMaxPerBlock; i--; p += m_cbChunk)
+ for (size_type i = BlockSize/m_cbChunk; i--; p += m_cbChunk)
                 {
                         ((FreeChunk*)p)->erase();
                 }
 
                 blk->erase();
 
- m_alloc.deallocate(blk);
+ alloc.deallocate(blk);
         }
 
 public:
- __forceinline void* BOOST_MEMORY_CALL allocate()
+ __forceinline void* BOOST_MEMORY_CALL allocate(alloc_type alloc)
         {
                 if (m_freelist.empty())
- do_allocate_block_();
+ do_allocate_block_(alloc);
 
                 void* p = &m_freelist.front();
                 ++chunkHeader_(p)->nUsed;
@@ -200,16 +177,16 @@
                 return p;
         }
 
- __forceinline void BOOST_MEMORY_CALL deallocate(void* const p)
+ __forceinline void BOOST_MEMORY_CALL deallocate(alloc_type alloc, void* const p)
         {
                 MemBlock* const blk = chunkHeader_(p);
 
- BOOST_MEMORY_ASSERT(blk->nUsed > 0 && blk->nUsed <= m_nMaxPerBlock);
+ BOOST_MEMORY_ASSERT(blk->nUsed > 0 && blk->nUsed <= BlockSize/m_cbChunk);
                 BOOST_MEMORY_DBG_FILL(p, element_size());
 
                 m_freelist.push_front((FreeChunk*)p);
                 if (--blk->nUsed == 0 && blk != m_lastBlock)
- do_deallocate_block_(blk);
+ do_deallocate_block_(alloc, 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 01:03:23 EDT (Sat, 23 May 2009)
@@ -25,14 +25,23 @@
 // -------------------------------------------------------------------------
 // class norm_object_pool_
 
+#pragma pack(1)
+
 template <class Type, class PolicyT>
 class norm_object_pool_ : private fixed_alloc<PolicyT>
 {
 private:
         typedef fixed_alloc<PolicyT> PoolT;
- typedef typename PoolT::alloc_type AllocT;
 
-#pragma pack(1)
+public:
+ typedef typename PoolT::alloc_type alloc_type;
+ typedef typename PoolT::size_type size_type;
+
+ using PoolT::element_size;
+
+private:
+ alloc_type m_alloc;
+
 protected:
         struct ChunkHeader
         {
@@ -42,7 +51,6 @@
                         size_t tag;
                 };
         };
-#pragma pack()
 
 protected:
         __forceinline static size_t BOOST_MEMORY_CALL is_allocated_(void*p)
@@ -63,18 +71,12 @@
         }
         
 public:
- typedef AllocT alloc_type;
-
-public:
- using PoolT::get_alloc;
-
-public:
         norm_object_pool_() : PoolT(sizeof(Type))
         {
                 BOOST_MEMORY_STATIC_ASSERT(sizeof(ChunkHeader) == sizeof(typename PoolT::ChunkHeader));
         }
         
- explicit norm_object_pool_(AllocT alloc) : PoolT(alloc, sizeof(Type))
+ explicit norm_object_pool_(alloc_type alloc) : PoolT(sizeof(Type)), m_alloc(alloc)
         {
                 BOOST_MEMORY_STATIC_ASSERT(sizeof(ChunkHeader) == sizeof(typename PoolT::ChunkHeader));
         }
@@ -84,6 +86,11 @@
                 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)
         {
@@ -117,11 +124,15 @@
                 }
         }
 
+ __forceinline void BOOST_MEMORY_CALL swap(norm_object_pool_& o) {
+ swap_object(this, &o);
+ }
+
 #if defined(BOOST_MEMORY_NO_STRICT_EXCEPTION_SEMANTICS)
         __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();
+ void* p = PoolT::allocate(m_alloc);
                 mark_allocated_(p);
                 return p;
         }
@@ -129,7 +140,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();
+ return PoolT::allocate(m_alloc);
         }
 
         __forceinline void BOOST_MEMORY_CALL manage(void* p, destructor_t fn) {
@@ -138,7 +149,7 @@
         }
 
         Type* BOOST_MEMORY_CALL construct() {
- Type* p = new(PoolT::allocate()) Type;
+ Type* p = new(PoolT::allocate(m_alloc)) Type;
                 mark_allocated_(p);
                 return p;
         }
@@ -146,58 +157,81 @@
         void BOOST_MEMORY_CALL destroy(Type* obj) {
                 mark_deallocated_(obj);
                 obj->~Type();
- PoolT::deallocate(obj);
+ PoolT::deallocate(m_alloc, obj);
         }
 };
 
+#pragma pack()
+
 // -------------------------------------------------------------------------
 // class pod_object_pool_
 
+#pragma pack(1)
+
 template <class Type, class PolicyT>
 class pod_object_pool_ : private fixed_alloc<PolicyT>
 {
 private:
         typedef fixed_alloc<PolicyT> PoolT;
- typedef typename PoolT::alloc_type AllocT;
 
 public:
- typedef AllocT alloc_type;
+ typedef typename PoolT::alloc_type alloc_type;
+ typedef typename PoolT::size_type size_type;
 
-public:
- using PoolT::get_alloc;
- using PoolT::clear;
+ using PoolT::element_size;
 
+private:
+ alloc_type m_alloc;
+
 public:
- pod_object_pool_() : PoolT(sizeof(Type)) {}
+ pod_object_pool_()
+ : PoolT(sizeof(Type)) {
+ }
+
+ explicit pod_object_pool_(alloc_type alloc)
+ : PoolT(sizeof(Type)), m_alloc(alloc) {
+ }
 
- explicit pod_object_pool_(AllocT alloc)
- : PoolT(alloc, sizeof(Type)) {
+ alloc_type BOOST_MEMORY_CALL get_alloc() const
+ {
+ return m_alloc;
+ }
+
+ void BOOST_MEMORY_CALL clear()
+ {
+ PoolT::clear(m_alloc);
+ }
+
+ __forceinline void BOOST_MEMORY_CALL swap(pod_object_pool_& o) {
+ swap_object(this, &o);
         }
 
 #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();
+ return PoolT::allocate(m_alloc);
         }
 #endif
 
         __forceinline void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, int fnZero) {
                 BOOST_MEMORY_ASSERT(cb == sizeof(Type));
- return PoolT::allocate();
+ return PoolT::allocate(m_alloc);
         }
 
         __forceinline void BOOST_MEMORY_CALL manage(void* p, int fnZero) {
         }
 
         __forceinline Type* BOOST_MEMORY_CALL construct() {
- return new (PoolT::allocate()) Type;
+ return new (PoolT::allocate(m_alloc)) Type;
         }
 
         __forceinline void BOOST_MEMORY_CALL destroy(Type* obj) {
- PoolT::deallocate(obj);
+ PoolT::deallocate(m_alloc, obj);
         }
 };
 
+#pragma pack()
+
 // -------------------------------------------------------------------------
 // class object_pool_traits
 

Modified: sandbox/memory/boost/memory/pool.hpp
==============================================================================
--- sandbox/memory/boost/memory/pool.hpp (original)
+++ sandbox/memory/boost/memory/pool.hpp 2009-05-23 01:03:23 EDT (Sat, 23 May 2009)
@@ -1,5 +1,5 @@
 //
-// boost/memory/pool.hpp
+// boost/memory/pool_alloc.hpp
 //
 // Copyright (c) 2004 - 2008 xushiwei (xushiweizh_at_[hidden])
 //
@@ -25,8 +25,56 @@
 // -------------------------------------------------------------------------
 // class pool, scoped_pool
 
-typedef fixed_alloc<NS_BOOST_MEMORY_POLICY::stdlib> pool;
-typedef fixed_alloc<NS_BOOST_MEMORY_POLICY::scoped> scoped_pool;
+#pragma pack(1)
+
+template <class PolicyT>
+class pool_alloc : private fixed_alloc<PolicyT>
+{
+private:
+ typedef fixed_alloc<PolicyT> PoolT;
+
+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 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 swap(pool_alloc& o) {
+ swap_object(this, &o);
+ }
+
+ __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()
 
 // -------------------------------------------------------------------------
 // $Log: $

Modified: sandbox/memory/boost/memory/region_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/region_alloc.hpp (original)
+++ sandbox/memory/boost/memory/region_alloc.hpp 2009-05-23 01:03:23 EDT (Sat, 23 May 2009)
@@ -21,6 +21,8 @@
 // -------------------------------------------------------------------------
 // class region_alloc
 
+#pragma pack(1)
+
 template <class PolicyT>
 class region_alloc
 {
@@ -35,7 +37,6 @@
 
         typedef AllocT alloc_type;
 
-#pragma pack(1)
 private:
         struct MemBlock;
         friend struct MemBlock;
@@ -53,7 +54,6 @@
                 DestroyNode* pPrev;
                 destructor_t fnDestroy;
         };
-#pragma pack()
         
         char* m_begin;
         char* m_end;
@@ -225,6 +225,8 @@
         }
 };
 
+#pragma pack()
+
 // -------------------------------------------------------------------------
 // $Log: region_alloc.hpp,v $
 

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 01:03:23 EDT (Sat, 23 May 2009)
@@ -25,6 +25,8 @@
 // =========================================================================
 // class stdlib_alloc
 
+#pragma pack(1)
+
 #if defined(__GNUG__)
 #define _msize malloc_usable_size
 #endif
@@ -53,6 +55,8 @@
         }
 };
 
+#pragma pack()
+
 // -------------------------------------------------------------------------
 // class system_alloc
 


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