|
Boost-Commit : |
From: xushiweizh_at_[hidden]
Date: 2008-04-29 23:17:17
Author: xushiwei
Date: 2008-04-29 23:17:16 EDT (Tue, 29 Apr 2008)
New Revision: 44910
URL: http://svn.boost.org/trac/boost/changeset/44910
Log:
ticket #1885: initial version of gc_alloc.
Text files modified:
sandbox/memory/boost/memory/auto_alloc.hpp | 6
sandbox/memory/boost/memory/basic.hpp | 44 ++++++++-----
sandbox/memory/boost/memory/gc_alloc.hpp | 121 ++++++++++++++++++++++++---------------
sandbox/memory/boost/memory/policy.hpp | 21 +++++
4 files changed, 120 insertions(+), 72 deletions(-)
Modified: sandbox/memory/boost/memory/auto_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/auto_alloc.hpp (original)
+++ sandbox/memory/boost/memory/auto_alloc.hpp 2008-04-29 23:17:16 EDT (Tue, 29 Apr 2008)
@@ -118,9 +118,9 @@
_MemBlock* pHeader = _ChainHeader();
while (pHeader)
{
- _MemBlock* pTemp = pHeader->pPrev;
- m_alloc.deallocate(pHeader);
- pHeader = pTemp;
+ _MemBlock* curr = pHeader;
+ pHeader = pHeader->pPrev;
+ m_alloc.deallocate(curr);
}
m_begin = m_end = (char*)HeaderSize;
}
Modified: sandbox/memory/boost/memory/basic.hpp
==============================================================================
--- sandbox/memory/boost/memory/basic.hpp (original)
+++ sandbox/memory/boost/memory/basic.hpp 2008-04-29 23:17:16 EDT (Tue, 29 Apr 2008)
@@ -30,6 +30,8 @@
#include <crtdbg.h> // _CrtSetDbgFlag
#endif
+#pragma pack() // default pack
+
// =========================================================================
#ifndef BOOST_MEMORY_CALL
@@ -126,6 +128,11 @@
destructArrayN((Type*)(hdr + 1), hdr->count);
}
+ static size_t BOOST_MEMORY_CALL getArrayAllocSize(size_t count)
+ {
+ return sizeof(array_destructor_header)+sizeof(Type)*count;
+ }
+
template <class AllocT>
static void* BOOST_MEMORY_CALL allocArray(AllocT& alloc, size_t count)
{
@@ -136,9 +143,9 @@
return hdr + 1;
}
- static void* BOOST_MEMORY_CALL getArrayBuffer(void* array)
+ static char* BOOST_MEMORY_CALL getArrayBuffer(void* array)
{
- return (array_destructor_header*)array - 1;
+ return (char*)array - sizeof(array_destructor_header);
}
static size_t BOOST_MEMORY_CALL getArraySize(void* array)
@@ -170,13 +177,17 @@
\
static void BOOST_MEMORY_CALL destructArrayN(Type* array, size_t count) {} \
\
+ static size_t BOOST_MEMORY_CALL getArrayAllocSize(size_t count) { \
+ return sizeof(Type)*count; \
+ } \
+ \
template <class AllocT> \
static void* BOOST_MEMORY_CALL allocArray(AllocT& alloc, size_t count) {\
return alloc.allocate(sizeof(Type)*count); \
} \
\
- static void* BOOST_MEMORY_CALL getArrayBuffer(void* array) { \
- return array; \
+ static char* BOOST_MEMORY_CALL getArrayBuffer(void* array) { \
+ return (char*)array; \
} \
\
static size_t BOOST_MEMORY_CALL getArraySize(void* array) { \
@@ -207,10 +218,6 @@
}; \
_NS_BOOST_END
-#define BOOST_INT_NO_CONSTRUCTOR(Type) \
- BOOST_NO_CONSTRUCTOR(unsigned Type); \
- BOOST_NO_CONSTRUCTOR(signed Type)
-
// -------------------------------------------------------------------------
// C Standard Types Support
@@ -218,20 +225,21 @@
BOOST_NO_CONSTRUCTOR(Type); \
BOOST_NO_DESTRUCTOR(Type)
-#define BOOST_DECL_INT_CTYPE(Type) \
- BOOST_NO_CONSTRUCTOR(Type); \
- BOOST_INT_NO_DESTRUCTOR(Type)
-
-// -------------------------------------------------------------------------
-
BOOST_DECL_CTYPE(bool);
BOOST_DECL_CTYPE(float);
BOOST_DECL_CTYPE(double);
-BOOST_DECL_INT_CTYPE(int);
-BOOST_DECL_INT_CTYPE(char);
-BOOST_DECL_INT_CTYPE(short);
-BOOST_DECL_INT_CTYPE(long);
+BOOST_DECL_CTYPE(int);
+BOOST_DECL_CTYPE(unsigned int);
+
+BOOST_DECL_CTYPE(char);
+BOOST_DECL_CTYPE(unsigned char);
+
+BOOST_DECL_CTYPE(short);
+BOOST_DECL_CTYPE(unsigned short);
+
+BOOST_DECL_CTYPE(long);
+BOOST_DECL_CTYPE(unsigned long);
// =========================================================================
// MEMORY_DBG_NEW_ARG
Modified: sandbox/memory/boost/memory/gc_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/gc_alloc.hpp (original)
+++ sandbox/memory/boost/memory/gc_alloc.hpp 2008-04-29 23:17:16 EDT (Tue, 29 Apr 2008)
@@ -137,48 +137,50 @@
_MemBlock* m_blockList;
_FreeListNode* m_freeList;
_HugeGCAlloc m_hugeAlloc;
+ static _FreeMemHeader _null;
private:
const gen_alloc& operator=(const gen_alloc&);
- static bool BOOST_MEMORY_CALL _IsValid(void* obj, destructor_t fn)
+ static bool BOOST_MEMORY_CALL _IsValid(void* obj, size_t cb, destructor_t fn)
{
_MemHeaderEx* node = (_MemHeaderEx*)obj - 1;
BOOST_MEMORY_ASSERT(node->fnDestroy == fn);
- BOOST_MEMORY_ASSERT(node->cbSize == sizeof(Type) + sizeof(_DestroyInfo));
+ BOOST_MEMORY_ASSERT(node->cbSize == cb + sizeof(_DestroyInfo));
BOOST_MEMORY_ASSERT(node->blkType == nodeAllocedWithDestructor);
return node->fnDestroy == fn &&
- node->cbSize == sizeof(Type) + sizeof(_DestroyInfo) &&
+ node->cbSize == cb + sizeof(_DestroyInfo) &&
node->blkType == nodeAllocedWithDestructor;
}
- static bool BOOST_MEMORY_CALL _IsValid(void* obj, int fnZero)
+ static bool BOOST_MEMORY_CALL _IsValid(void* obj, size_t cb, int fnZero)
{
_MemHeader* node = (_MemHeader*)obj - 1;
- BOOST_MEMORY_ASSERT(node->cbSize == sizeof(Type));
+ BOOST_MEMORY_ASSERT(node->cbSize == cb);
BOOST_MEMORY_ASSERT(node->blkType == nodeAlloced);
- return node->cbSize == sizeof(Type) && node->blkType == nodeAlloced;
+ return node->cbSize == cb && node->blkType == nodeAlloced;
}
template <class Type>
static bool BOOST_MEMORY_CALL _IsValid(Type* obj)
{
- return _IsValid(obj, destructor_traits<Type>::destruct);
+ return _IsValid(obj, sizeof(Type), destructor_traits<Type>::destruct);
}
template <class Type>
static bool BOOST_MEMORY_CALL _IsValidArray(Type* array, size_t count)
{
void* buf = destructor_traits<Type>::getArrayBuffer(array);
+ size_t cb = destructor_traits<Type>::getArrayAllocSize(count);
if (buf == array)
{
- return _IsValid(buf, sizeof(Type)*count);
+ return _IsValid(buf, cb, sizeof(Type)*count);
}
else
{
size_t count1 = destructor_traits<Type>::getArraySize(array);
BOOST_MEMORY_ASSERT(count1 == count);
- bool fValid = _IsValid(buf, destructor_traits<Type>::destructArray);
+ bool fValid = _IsValid(buf, cb, destructor_traits<Type>::destructArray);
return count1 == count && fValid;
}
}
@@ -216,7 +218,7 @@
enum { HeaderSize = sizeof(void*) };
enum { BlockSize = MemBlockSize - HeaderSize };
enum { AllocSizeBig = MIN(_Policy::AllocSizeBig, BlockSize/2) };
- enum { AllocSizeHuge = (1 << 30) };
+ enum { AllocSizeHuge = _Policy::AllocSizeHuge };
enum { RecycleSizeMin = MAX(_Policy::RecycleSizeMin, 128) };
public:
@@ -263,43 +265,15 @@
_MemBlock* pHeader = m_blockList;
while (pHeader)
{
- _MemBlock* pTemp = pHeader->pPrev;
- m_alloc.deallocate(pHeader);
- pHeader = pTemp;
+ _MemBlock* curr = pHeader;
+ pHeader = pHeader->pPrev;
+ m_alloc.deallocate(curr);
}
- m_begin = m_end = NULL;
+ m_begin = m_end = _null.getData();
m_blockList = NULL;
m_freeList = NULL;
}
- template <class Type>
- void BOOST_MEMORY_CALL destroy(Type* obj)
- {
- BOOST_MEMORY_ASSERT( _IsValid(obj) );
-
- _MemHeader* p = (_MemHeader*)obj - 1;
- p->blkType = nodeFree;
- obj->~Type();
- }
-
- template <class Type>
- Type* BOOST_MEMORY_CALL newArray(size_t count, Type* zero)
- {
- Type* array = (Type*)destructor_traits<Type>::allocArray(*this, count);
- return constructor_traits<Type>::constructArray(array, count);
- }
-
- template <class Type>
- void BOOST_MEMORY_CALL destroyArray(Type* array, size_t count)
- {
- BOOST_MEMORY_ASSERT( _IsValidArray(obj, count) );
-
- void* buf = destructor_traits<Type>::getArrayBuffer(array);
- _MemHeader* p = (_MemHeader*)buf - 1;
- p->blkType = nodeFree;
- destructor_traits<Type>::destructArrayN(array, count);
- }
-
void* BOOST_MEMORY_CALL allocate(size_t cbData)
{
const size_t cb = cbData + sizeof(_MemHeader);
@@ -314,8 +288,6 @@
if (cb >= BlockSize)
{
pNew = _NewMemBlock(cb + HeaderSize);
- m_blockList->pPrev = pNew;
- m_blockList = pNew;
_MemHeader* p = (_MemHeader*)pNew->buffer;
p->blkType = nodeAlloced;
p->cbSize = cbData;
@@ -343,6 +315,9 @@
void* BOOST_MEMORY_CALL allocate(size_t cb, destructor_t fn)
{
+ if (cb >= AllocSizeHuge)
+ return m_hugeAlloc.allocate(cb, fn);
+
_DestroyInfo* pNode = (_DestroyInfo*)allocate(sizeof(_DestroyInfo) + cb);
pNode->fnDestroy = fn;
pNode->pPrev = m_destroyChain;
@@ -359,18 +334,68 @@
void BOOST_MEMORY_CALL deallocate(void* pData, size_t cbData)
{
if (cbData >= AllocSizeHuge)
- return m_hugeAlloc.deallocate(p, cbData);
+ {
+ m_hugeAlloc.deallocate(pData, cbData);
+ }
+ else
+ {
+ _MemHeader* p = (_MemHeader*)pData - 1;
+ BOOST_MEMORY_ASSERT(p->cbSize == cbData);
+ BOOST_MEMORY_ASSERT(p->blkType == nodeAlloced);
- _MemHeader* p = (_MemHeader*)pData - 1;
- BOOST_MEMORY_ASSERT(p->cbSize == cbData);
- BOOST_MEMORY_ASSERT(p->blkType == nodeAlloced);
+ p->blkType = nodeFree;
+ }
+ }
+
+public:
+ template <class Type>
+ void BOOST_MEMORY_CALL destroy(Type* obj)
+ {
+ BOOST_MEMORY_ASSERT( _IsValid(obj) );
+ BOOST_MEMORY_ASSERT( sizeof(Type) < AllocSizeHuge );
+
+ obj->~Type();
+ _MemHeaderEx* p = (_MemHeaderEx*)obj - 1;
p->blkType = nodeFree;
}
+ template <class Type>
+ Type* BOOST_MEMORY_CALL newArray(size_t count, Type* zero)
+ {
+ size_t cb = destructor_traits<Type>::getArrayAllocSize(count);
+ if (cb >= AllocSizeHuge)
+ return m_hugeAlloc.newArray(count, zero);
+
+ Type* array = (Type*)destructor_traits<Type>::allocArray(*this, count);
+ return constructor_traits<Type>::constructArray(array, count);
+ }
+
+ template <class Type>
+ void BOOST_MEMORY_CALL destroyArray(Type* array, size_t count)
+ {
+ BOOST_MEMORY_ASSERT( _IsValidArray(obj, count) );
+
+ size_t cb = destructor_traits<Type>::getArrayAllocSize(count);
+ if (cb >= AllocSizeHuge)
+ {
+ m_hugeAlloc.destroyArray(array, count);
+ }
+ else
+ {
+ destructor_traits<Type>::destructArrayN(array, count);
+ void* pData = destructor_traits<Type>::getArrayBuffer(array),
+ _MemHeaderEx* p = (_MemHeaderEx*)pData - 1;
+ p->blkType = nodeFree;
+ }
+ }
+
_BOOST_FAKE_DBG_ALLOCATE();
};
+template <class _Policy>
+typename gen_alloc<_Policy>::_FreeMemHeader gen_alloc<_Policy>::_null;
+
// -------------------------------------------------------------------------
// class gc_alloc
Modified: sandbox/memory/boost/memory/policy.hpp
==============================================================================
--- sandbox/memory/boost/memory/policy.hpp (original)
+++ sandbox/memory/boost/memory/policy.hpp 2008-04-29 23:17:16 EDT (Tue, 29 Apr 2008)
@@ -64,7 +64,7 @@
{
_MemBlock* curr = m_memChain;
m_memChain = m_memChain->pPrev;
- _SysAlloc::deallocate(m_memChain);
+ _SysAlloc::deallocate(curr);
}
}
@@ -74,6 +74,19 @@
// no action
}
+ template <class Type>
+ Type* BOOST_MEMORY_CALL newArray(size_t count, Type* zero)
+ {
+ Type* array = (Type*)destructor_traits<Type>::allocArray(*this, count);
+ return constructor_traits<Type>::constructArray(array, count);
+ }
+
+ template <class Type>
+ void BOOST_MEMORY_CALL destroyArray(Type* array, size_t count)
+ {
+ // no action
+ }
+
void* BOOST_MEMORY_CALL allocate(size_t cb)
{
_MemBlock* p = (_MemBlock*)_SysAlloc::allocate(cb + sizeof(_MemBlock));
@@ -113,10 +126,12 @@
public:
enum { MemBlockSize = BOOST_MEMORY_BLOCK_SIZE };
- enum { AllocSizeBig = Default };
+ typedef system_alloc allocator_type;
+
enum { RecycleSizeMin = 256 };
+ enum { AllocSizeBig = Default };
+ enum { AllocSizeHuge = 1024*1024 };
- typedef system_alloc allocator_type;
typedef simple_gc_alloc<system_alloc> huge_gc_allocator;
};
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