|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r52543 - in sandbox/memory/boost: . memory memory/container
From: xushiweizh_at_[hidden]
Date: 2009-04-22 12:52:38
Author: xushiwei
Date: 2009-04-22 12:52:37 EDT (Wed, 22 Apr 2009)
New Revision: 52543
URL: http://svn.boost.org/trac/boost/changeset/52543
Log:
1. class fixed_alloc
2. class pool, scoped_pool
Added:
sandbox/memory/boost/memory/pool.hpp (contents, props changed)
Text files modified:
sandbox/memory/boost/memory.hpp | 4
sandbox/memory/boost/memory/container/dcl_list.hpp | 25 +++---
sandbox/memory/boost/memory/fixed_alloc.hpp | 143 +++++++++++++++++++++++++++------------
sandbox/memory/boost/memory/region_alloc.hpp | 2
4 files changed, 112 insertions(+), 62 deletions(-)
Modified: sandbox/memory/boost/memory.hpp
==============================================================================
--- sandbox/memory/boost/memory.hpp (original)
+++ sandbox/memory/boost/memory.hpp 2009-04-22 12:52:37 EDT (Wed, 22 Apr 2009)
@@ -40,8 +40,8 @@
#include "memory/scoped_alloc.hpp"
#endif
-#ifndef BOOST_MEMORY_FIXED_ALLOC_HPP
-#include "memory/fixed_alloc.hpp"
+#ifndef BOOST_MEMORY_POOL_HPP
+#include "memory/pool.hpp"
#endif
// -------------------------------------------------------------------------
Modified: sandbox/memory/boost/memory/container/dcl_list.hpp
==============================================================================
--- sandbox/memory/boost/memory/container/dcl_list.hpp (original)
+++ sandbox/memory/boost/memory/container/dcl_list.hpp 2009-04-22 12:52:37 EDT (Wed, 22 Apr 2009)
@@ -57,8 +57,6 @@
public:
void BOOST_MEMORY_CALL insert_me_front_(dcl_list_node_base_& head)
{
- BOOST_MEMORY_ASSERT(empty());
-
m_prev = &head;
m_next = head.m_next;
head.m_next->m_prev = this;
@@ -67,8 +65,6 @@
void BOOST_MEMORY_CALL insert_me_back_(dcl_list_node_base_& head)
{
- BOOST_MEMORY_ASSERT(empty());
-
m_next = &head;
m_prev = head.m_prev;
head.m_prev->m_next = this;
@@ -79,10 +75,14 @@
bool BOOST_MEMORY_CALL empty() const
{
BOOST_MEMORY_ASSERT(m_next == this || m_next != m_prev);
-
return m_next == this;
}
+ void BOOST_MEMORY_CALL clear()
+ {
+ m_next = m_prev = this;
+ }
+
void BOOST_MEMORY_CALL erase()
{
m_next->m_prev = m_prev;
@@ -93,17 +93,13 @@
void BOOST_MEMORY_CALL pop_front()
{
BOOST_MEMORY_ASSERT(!empty());
-
- dcl_list_node_base_* node = m_next;
- node->erase();
+ static_cast<dcl_list_node_base_*>(m_next)->erase();
}
void BOOST_MEMORY_CALL pop_back()
{
BOOST_MEMORY_ASSERT(!empty());
-
- dcl_list_node_base_* node = m_prev;
- node->erase();
+ static_cast<dcl_list_node_base_*>(m_prev)->erase();
}
};
@@ -231,9 +227,12 @@
return static_cast<const dcl_list_node_base_*>(node) == this;
}
+private:
+ typedef dcl_list_node<NodeT> BaseNodeT;
+
public:
- typedef dcl_list_iterator_<NodeT, NodeT&, NodeT*> iterator;
- typedef dcl_list_iterator_<NodeT, const NodeT&, const NodeT*> const_iterator;
+ typedef dcl_list_iterator_<BaseNodeT, BaseNodeT&, BaseNodeT*> iterator;
+ typedef dcl_list_iterator_<BaseNodeT, const BaseNodeT&, const BaseNodeT*> const_iterator;
iterator BOOST_MEMORY_CALL begin() { return iterator(first()); }
const_iterator BOOST_MEMORY_CALL begin() const { return const_iterator(first()); }
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 12:52:37 EDT (Wed, 22 Apr 2009)
@@ -22,10 +22,13 @@
NS_BOOST_MEMORY_BEGIN
-/*
// -------------------------------------------------------------------------
// class fixed_alloc
+#ifndef BOOST_MEMORY_ROUND
+#define BOOST_MEMORY_ROUND(x, y) (((x)+((y)-1)) & ~((y)-1))
+#endif
+
template <class PolicyT>
class fixed_alloc
{
@@ -41,20 +44,9 @@
#pragma pack(1)
private:
struct MemBlock;
- struct Header
+ struct Header : public dcl_list_node<MemBlock>
{
- Header* pPrev;
- Header* pNext;
size_t nUsed;
-
- public:
- Header() {
- pPrev = pNext = this;
- }
-
- bool BOOST_MEMORY_CALL empty() const {
- return pPrev == this;
- }
};
enum { HeaderSize = sizeof(Header) };
@@ -65,69 +57,128 @@
char buffer[BlockSize];
};
- enum { ChunkHeaderSize = sizeof(void*) };
- struct Chunk
+ struct ChunkHeader
{
- struct Used
- {
- MemBlock* pBlock;
- char data[1];
- };
- struct Unused
- {
- Unused* pPrev;
- Unused* pNext;
- };
+ MemBlock* pBlock;
};
-#pragma pack()
-
- class MemBlocks : public Header
- {
- private:
+ enum { ChunkHeaderSize = sizeof(ChunkHeader) };
+ struct FreeChunk : public dcl_list_node<FreeChunk>
+ {
};
+#pragma pack()
AllocT m_alloc;
- Header m_blks;
- const size_type m_cbChunk;
- const size_type m_nMaxPerBlock;
+ dcl_list<MemBlock> m_blks;
+ dcl_list<FreeChunk> m_freelist;
+ size_type m_cbChunk;
+ size_type m_nMaxPerBlock;
private:
- void init_()
+ void init_(size_type cbElem)
{
- BOOST_MEMORY_ASSERT(m_nMaxPerBlock > 0);
+ m_cbChunk = BOOST_MEMORY_ROUND(cbElem, ChunkHeaderSize) + ChunkHeaderSize;
+ m_nMaxPerBlock = BlockSize / m_cbChunk;
- m_blks.pPrev = m_blks.pNext = &m_blks;
+ BOOST_MEMORY_ASSERT(cbElem > 0 && m_nMaxPerBlock > 0);
}
public:
explicit fixed_alloc(size_type cbElem)
- : m_cbChunk(cbElem + ChunkHeaderSize), m_nMaxPerBlock(BlockSize / m_cbChunk)
{
- init_();
+ init_(cbElem);
}
fixed_alloc(AllocT alloc, size_type cbElem)
- : m_alloc(alloc), m_cbChunk(cbElem + ChunkHeaderSize), m_nMaxPerBlock(BlockSize / m_cbChunk)
+ : m_alloc(alloc)
+ {
+ init_(cbElem);
+ }
+
+ fixed_alloc(fixed_alloc& owner, size_type cbElem)
+ : m_alloc(owner.m_alloc)
+ {
+ init_(cbElem);
+ }
+
+ ~fixed_alloc()
+ {
+ clear();
+ }
+
+private:
+ __forceinline MemBlock*& BOOST_MEMORY_CALL chunkHeader_(void* const p)
{
- init_();
+ return ((ChunkHeader*)p - 1)->pBlock;
}
- fixed_alloc(fixed_alloc& owner)
- : m_alloc(owner.m_alloc), m_cbChunk(cbElem + ChunkHeaderSize), m_nMaxPerBlock(BlockSize / m_cbChunk)
+ void BOOST_MEMORY_CALL do_allocate_block_()
{
- init_();
+ MemBlock* const blk = (MemBlock*)m_alloc.allocate(sizeof(MemBlock));
+ m_blks.push_front(blk);
+
+ blk->nUsed = 0;
+
+ char* p = blk->buffer + ChunkHeaderSize;
+ for (size_type i = m_nMaxPerBlock; i--; p += m_cbChunk)
+ {
+ chunkHeader_(p) = blk;
+ m_freelist.push_front((FreeChunk*)p);
+ }
+
+ BOOST_MEMORY_ASSERT(!m_freelist.empty());
+ }
+
+ void BOOST_MEMORY_CALL do_deallocate_block_(MemBlock* const blk)
+ {
+ char* p = blk->buffer + ChunkHeaderSize;
+ for (size_type i = m_nMaxPerBlock; i--; p += m_cbChunk)
+ {
+ ((FreeChunk*)p)->erase();
+ }
+
+ blk->erase();
+
+ m_alloc.deallocate(blk);
}
public:
- void* BOOST_MEMORY_CALL allocate()
+ void BOOST_MEMORY_CALL clear()
+ {
+ MemBlock* nextBlk;
+ for (MemBlock* blk = m_blks.first(); !m_blks.done(blk); blk = nextBlk)
+ {
+ nextBlk = blk->next();
+ m_alloc.deallocate(blk)
+ }
+ m_blks.clear();
+ m_freelist.clear();
+ }
+
+ __forceinline void* BOOST_MEMORY_CALL allocate()
{
- if (m_blks.)
+ if (m_freelist.empty())
+ do_allocate_block_();
+
+ void* p = &m_freelist.front();
+ ++chunkHeader_(p)->nUsed;
+ m_freelist.pop_front();
+ return p;
+ }
+
+ __forceinline void BOOST_MEMORY_CALL deallocate(void* const p)
+ {
+ BOOST_MEMORY_ASSERT(chunkHeader_(p)->nUsed <= m_nMaxPerBlock);
+
+ MemBlock* const blk = chunkHeader_(p);
+ if (--blk->nUsed > 0)
+ m_freelist.push_front((FreeChunk*)p);
+ else
+ do_deallocate_block_(blk);
}
};
-*/
// -------------------------------------------------------------------------
// $Log: $
Added: sandbox/memory/boost/memory/pool.hpp
==============================================================================
--- (empty file)
+++ sandbox/memory/boost/memory/pool.hpp 2009-04-22 12:52:37 EDT (Wed, 22 Apr 2009)
@@ -0,0 +1,36 @@
+//
+// boost/memory/pool.hpp
+//
+// Copyright (c) 2004 - 2008 xushiwei (xushiweizh_at_[hidden])
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/memory/index.htm for documentation.
+//
+#ifndef BOOST_MEMORY_POOL_HPP
+#define BOOST_MEMORY_POOL_HPP
+
+#ifndef BOOST_MEMORY_FIXED_ALLOC_HPP
+#include "fixed_alloc.hpp"
+#endif
+
+#ifndef BOOST_MEMORY_BLOCKPOOL_HPP
+#include "block_pool.hpp"
+#endif
+
+NS_BOOST_MEMORY_BEGIN
+
+// -------------------------------------------------------------------------
+// class pool, scoped_pool
+
+typedef fixed_alloc<NS_BOOST_MEMORY_POLICY::stdlib> pool;
+typedef fixed_alloc<NS_BOOST_MEMORY_POLICY::pool> scoped_pool;
+
+// -------------------------------------------------------------------------
+// $Log: $
+
+NS_BOOST_MEMORY_END
+
+#endif /* BOOST_MEMORY_POOL_HPP */
Modified: sandbox/memory/boost/memory/region_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/region_alloc.hpp (original)
+++ sandbox/memory/boost/memory/region_alloc.hpp 2009-04-22 12:52:37 EDT (Wed, 22 Apr 2009)
@@ -61,7 +61,7 @@
private:
const region_alloc& operator=(const region_alloc&);
- MemBlock* BOOST_MEMORY_CALL chainHeader_() const
+ __forceinline MemBlock* BOOST_MEMORY_CALL chainHeader_() const
{
return (MemBlock*)(m_begin - HeaderSize);
}
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