|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r53199 - sandbox/memory/boost/memory
From: xushiweizh_at_[hidden]
Date: 2009-05-23 02:12:55
Author: xushiwei
Date: 2009-05-23 02:12:55 EDT (Sat, 23 May 2009)
New Revision: 53199
URL: http://svn.boost.org/trac/boost/changeset/53199
Log:
pools_alloc
Added:
sandbox/memory/boost/memory/pools_alloc.hpp (contents, props changed)
Text files modified:
sandbox/memory/boost/memory/object_pool.hpp | 23 +++++++++++++++--------
sandbox/memory/boost/memory/pool.hpp | 12 ++++++++----
2 files changed, 23 insertions(+), 12 deletions(-)
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 02:12:55 EDT (Sat, 23 May 2009)
@@ -33,6 +33,11 @@
private:
typedef fixed_alloc<PolicyT> PoolT;
+ norm_object_pool_(const norm_object_pool_& o);
+ void operator=(const norm_object_pool_& o);
+
+ void swap(norm_object_pool_& o); // NO SWAP: dcl_list
+
public:
typedef typename PoolT::alloc_type alloc_type;
typedef typename PoolT::size_type size_type;
@@ -124,10 +129,6 @@
}
}
- __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)
{
@@ -174,6 +175,11 @@
private:
typedef fixed_alloc<PolicyT> PoolT;
+ pod_object_pool_(const pod_object_pool_& o);
+ void operator=(const pod_object_pool_& o);
+
+ void swap(pod_object_pool_& o); // NO SWAP: dcl_list
+
public:
typedef typename PoolT::alloc_type alloc_type;
typedef typename PoolT::size_type size_type;
@@ -191,6 +197,11 @@
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
{
@@ -202,10 +213,6 @@
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));
Modified: sandbox/memory/boost/memory/pool.hpp
==============================================================================
--- sandbox/memory/boost/memory/pool.hpp (original)
+++ sandbox/memory/boost/memory/pool.hpp 2009-05-23 02:12:55 EDT (Sat, 23 May 2009)
@@ -33,6 +33,11 @@
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;
@@ -49,6 +54,9 @@
__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;
@@ -58,10 +66,6 @@
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);
}
Added: sandbox/memory/boost/memory/pools_alloc.hpp
==============================================================================
--- (empty file)
+++ sandbox/memory/boost/memory/pools_alloc.hpp 2009-05-23 02:12:55 EDT (Sat, 23 May 2009)
@@ -0,0 +1,218 @@
+//
+// boost/memory/pools_alloc.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_POOLS_ALLOC_HPP
+#define BOOST_MEMORY_POOLS_ALLOC_HPP
+
+#ifndef BOOST_MEMORY_BASIC_HPP
+#include "basic.hpp"
+#endif
+
+#ifndef BOOST_MEMORY_BASIC_HPP
+#include "fixed_alloc.hpp"
+#endif
+
+NS_BOOST_MEMORY_BEGIN
+
+// -------------------------------------------------------------------------
+// class simple_alloc
+
+#pragma pack(1)
+
+template <class PolicyT>
+class simple_alloc
+{
+private:
+ simple_alloc(const simple_alloc&);
+ const simple_alloc& operator=(const simple_alloc&);
+
+ void swap(simple_alloc& o); // NO SWAP: dcl_list
+
+public:
+ typedef typename PolicyT::alloc_type alloc_type;
+
+private:
+ struct Chunk {};
+ typedef dcl_list_node<Chunk> MemHeader;
+
+ dcl_list<Chunk> m_chunks;
+
+public:
+ ~simple_alloc() {
+ clear();
+ }
+
+ void BOOST_MEMORY_CALL clear()
+ {
+ MemHeader* nextChunk;
+ for (MemHeader* chunk = m_chunks.first(); !m_chunks.done(chunk); chunk = nextChunk)
+ {
+ nextChunk = chunk->next();
+ alloc_type::deallocate(chunk);
+ }
+ 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;
+ p->erase();
+ alloc_type::deallocate(p);
+ }
+};
+
+#pragma pack()
+
+// -------------------------------------------------------------------------
+// class pools_alloc
+
+#pragma pack(1)
+
+template <class PolicyT>
+class pools_alloc
+{
+private:
+ pools_alloc(const pools_alloc&);
+ const pools_alloc& operator=(const pools_alloc&);
+
+ 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;
+
+ enum { MemBlockSize = PolicyT::MemBlockBytes - alloc_type::Padding };
+
+private:
+ enum { ALIGN_BITS = 3 };
+ enum { NPOOL = 16 };
+ enum { ALIGN = 1 << ALIGN_BITS };
+ enum { MIN_BYTES = ALIGN };
+ enum { MAX_BYTES = ALIGN * NPOOL };
+
+ enum { POOL_BYTES = sizeof(FixedAllocT) };
+ enum { POOLS_TOTAL_BYTES = POOL_BYTES * NPOOL };
+
+ //
+ // pool 0: 1 ~ ALIGN
+ // pool 1: ALIGN + 1 ~ 2*ALIGN
+ // pool 2: ...
+ //
+ char m_pools[POOLS_TOTAL_BYTES];
+
+ alloc_type m_alloc;
+ LargeAllocT m_large_alloc;
+
+private:
+ void BOOST_MEMORY_CALL init_()
+ {
+ 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);
+ p += POOL_BYTES;
+ }
+ }
+
+public:
+ pools_alloc()
+ {
+ init_();
+ }
+
+ explicit pools_alloc(alloc_type alloc) : m_alloc(alloc)
+ {
+ init_();
+ }
+
+ ~pools_alloc()
+ {
+ do_clear_();
+ }
+
+ alloc_type BOOST_MEMORY_CALL get_alloc() const
+ {
+ return m_alloc;
+ }
+
+private:
+ void BOOST_MEMORY_CALL do_clear_()
+ {
+ char* pEnd = p + POOLS_TOTAL_BYTES;
+ for (char* p = m_pools; p != pEnd; p += POOL_BYTES)
+ {
+ ((FixedAllocT*)p)->clear(m_alloc);
+ }
+ }
+
+public:
+ void BOOST_MEMORY_CALL clear()
+ {
+ do_clear_();
+ m_large_alloc.clear();
+ }
+
+public:
+ void* BOOST_MEMORY_CALL allocate(size_type cb)
+ {
+ const size_type index = (cb - 1) >> ALIGN_BITS;
+ if (index < NPOOL)
+ return ((FixedAllocT*)p + index)->allocate(m_alloc);
+ else if (cb != 0)
+ return m_large_alloc.allocate(cb);
+ else
+ return &cb;
+ }
+
+public:
+ void BOOST_MEMORY_CALL deallocate(void* p, size_t cb)
+ {
+ const size_type index = (cb - 1) >> ALIGN_BITS;
+ if (index < NPOOL)
+ ((FixedAllocT*)p + index)->deallocate(m_alloc, p);
+ else if (cb != 0)
+ m_large_alloc.deallocate(p);
+ }
+
+public:
+ void* BOOST_MEMORY_CALL reallocate(void* p, size_t oldSize, size_t newSize)
+ {
+ const size_type oldIndex = (oldSize - 1) >> ALIGN_BITS;
+ const size_type newIndex = (newSize - 1) >> ALIGN_BITS;
+
+ if (oldIndex == newIndex && oldIndex < NPOOL)
+ return p;
+ void* p2 = allocate(newSize);
+ memcpy(p2, p, oldSize);
+ deallocate(p, oldSize);
+ return p2;
+ }
+};
+
+#pragma pack()
+
+// -------------------------------------------------------------------------
+// $Log: pools_alloc.hpp,v $
+
+NS_BOOST_MEMORY_END
+
+#endif /* BOOST_MEMORY_POOLS_ALLOC_HPP */
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