Boost logo

Boost-Commit :

From: xushiweizh_at_[hidden]
Date: 2008-05-07 03:08:02


Author: xushiwei
Date: 2008-05-07 03:08:01 EDT (Wed, 07 May 2008)
New Revision: 45190
URL: http://svn.boost.org/trac/boost/changeset/45190

Log:
1. Update GC Allocator specification. The following is minimum specification for GC Allocator:

{{{
typedef void (*DestructorType)(void* data);
 
concept GCAllocator
{
    // Allocate memory without given a cleanup function
    void* allocate(size_t cb);
 
    // Allocate unmanaged memory with a cleanup function
    void* unmanaged_alloc(size_t cb, DestructorType fn);

    // Commit unmanaged memory to be managed.
    void* manage(void* p, destructor_t fn);
 
    // Deprecated: allocate memory with a cleanup function
    void* allocate(size_t cb, DestructorType fn) {
       return manage(unmanaged_alloc(cb, fn), fn);
    }

    // Cleanup and deallocate all allocated memory by this GC Allocator
    void clear();
 
    // Swap two GCAllocator instances
    void swap(GCAllocator& o);
};
}}}

2. testExceptionSemantics
Text files modified:
   sandbox/memory/boost/detail/winapi/windef.h | 6 ++
   sandbox/memory/boost/memory/auto_alloc.hpp | 22 ++++++++
   sandbox/memory/boost/memory/basic.hpp | 98 +++++++++++++++++++++++++++++++++++++--
   sandbox/memory/boost/memory/gc_alloc.hpp | 26 +++++++++-
   sandbox/memory/libs/memory/test/test_basic/memory/exception_semantics.cpp | 14 +++++
   sandbox/memory/libs/memory/test/test_basic/test.cpp | 4
   6 files changed, 156 insertions(+), 14 deletions(-)

Modified: sandbox/memory/boost/detail/winapi/windef.h
==============================================================================
--- sandbox/memory/boost/detail/winapi/windef.h (original)
+++ sandbox/memory/boost/detail/winapi/windef.h 2008-05-07 03:08:01 EDT (Wed, 07 May 2008)
@@ -30,10 +30,14 @@
 #define CDECL
 #endif
 
-#if defined(__GNUG__)
+#if !defined(_MSC_VER)
+#ifndef __forceinline
 #define __forceinline inline
+#endif
+#ifndef __stdcall
 #define __stdcall
 #endif
+#endif
 
 #ifdef _MAC
 #define CALLBACK PASCAL

Modified: sandbox/memory/boost/memory/auto_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/auto_alloc.hpp (original)
+++ sandbox/memory/boost/memory/auto_alloc.hpp 2008-05-07 03:08:01 EDT (Wed, 07 May 2008)
@@ -177,18 +177,36 @@
 
         void* BOOST_MEMORY_CALL allocate(size_t cb, destructor_t fn)
         {
+ return manage(unmanaged_alloc(cb, fn), fn);
+ }
+
+ void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, destructor_t fn)
+ {
                 DestroyNode* pNode = (DestroyNode*)allocate(sizeof(DestroyNode) + cb);
                 pNode->fnDestroy = fn;
+ return pNode + 1;
+ }
+
+ void* BOOST_MEMORY_CALL manage(void* p, destructor_t fn)
+ {
+ DestroyNode* pNode = (DestroyNode*)p - 1;
+ BOOST_MEMORY_ASSERT(pNode->fnDestroy == fn);
+
                 pNode->pPrev = m_destroyChain;
                 m_destroyChain = pNode;
- return pNode + 1;
+ return p;
         }
 
- void* BOOST_MEMORY_CALL allocate(size_t cb, int fnZero)
+ void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, int fnZero)
         {
                 return allocate(cb);
         }
 
+ void* BOOST_MEMORY_CALL manage(void* p, int fnZero)
+ {
+ return p;
+ }
+
         void* BOOST_MEMORY_CALL reallocate(void* p, size_t oldSize, size_t newSize)
         {
                 if (oldSize >= newSize)

Modified: sandbox/memory/boost/memory/basic.hpp
==============================================================================
--- sandbox/memory/boost/memory/basic.hpp (original)
+++ sandbox/memory/boost/memory/basic.hpp 2008-05-07 03:08:01 EDT (Wed, 07 May 2008)
@@ -64,6 +64,17 @@
         #endif
 #endif
 
+// -------------------------------------------------------------------------
+
+#if !defined(_MSC_VER)
+#ifndef __forceinline
+#define __forceinline inline
+#endif
+#ifndef __stdcall
+#define __stdcall
+#endif
+#endif
+
 // =========================================================================
 // Configurations
 
@@ -244,7 +255,55 @@
 BOOST_MEMORY_DECL_CTYPE(unsigned long);
 
 // =========================================================================
-// BOOST_MEMORY_DBG_NEW_ARG
+// operator new of GC Allocator
+
+NS_BOOST_MEMORY_BEGIN
+
+enum _alloc_flag { _theAllocFlag = 0 };
+
+NS_BOOST_MEMORY_END
+
+template <class AllocT, class Type>
+__forceinline
+void* BOOST_MEMORY_CALL operator new(
+ size_t cb, AllocT& alloc, Type* zero, NS_BOOST_MEMORY::_alloc_flag)
+{
+ BOOST_MEMORY_ASSERT(cb == sizeof(Type));
+ typedef NS_BOOST_MEMORY::destructor_traits<Type> Traits;
+ return alloc.unmanaged_alloc(sizeof(Type), Traits::destruct);
+}
+
+template <class AllocT, class Type>
+__forceinline
+void BOOST_MEMORY_CALL operator delete(
+ void* p, AllocT& alloc, Type* zero, NS_BOOST_MEMORY::_alloc_flag)
+{
+ // no action
+}
+
+#if defined(_DEBUG)
+
+template <class AllocT, class Type>
+__forceinline
+void* BOOST_MEMORY_CALL operator new(
+ size_t cb, AllocT& alloc, Type* zero, NS_BOOST_MEMORY::_alloc_flag,
+ const char* file, const int line)
+{
+ BOOST_MEMORY_ASSERT(cb == sizeof(Type));
+ typedef NS_BOOST_MEMORY::destructor_traits<Type> Traits;
+ return alloc.unmanaged_alloc(sizeof(Type), Traits::destruct);
+}
+
+template <class AllocT, class Type>
+__forceinline
+void BOOST_MEMORY_CALL operator delete(
+ void* p, AllocT& alloc, Type* zero, NS_BOOST_MEMORY::_alloc_flag,
+ const char* file, const int line)
+{
+ // no action
+}
+
+#endif
 
 #if defined(_DEBUG)
 #define BOOST_MEMORY_FILE_LINE_ARG , __FILE__, __LINE__
@@ -252,6 +311,38 @@
 #define BOOST_MEMORY_FILE_LINE_ARG
 #endif
 
+#define BOOST_MEMORY_UNMANAGE_NEW(alloc, Type) \
+ ::new((alloc), (Type*)0, NS_BOOST_MEMORY::_theAllocFlag BOOST_MEMORY_FILE_LINE_ARG) Type
+
+// =========================================================================
+// NEW, NEW_ARRAY, ALLOC, ALLOC_ARRAY
+
+NS_BOOST_MEMORY_BEGIN
+
+template <class AllocT>
+class _managed
+{
+private:
+ AllocT& m_alloc;
+
+public:
+ explicit _managed(AllocT& alloc) : m_alloc(alloc) {}
+
+ template <class Type>
+ __forceinline Type* BOOST_MEMORY_CALL operator->*(Type* p) const
+ {
+ return (Type*)m_alloc.manage(p, destructor_traits<Type>::destruct);
+ }
+};
+
+template <class AllocT>
+__forceinline _managed<AllocT> BOOST_MEMORY_CALL _get_managed(AllocT& alloc)
+{
+ return _managed<AllocT>(alloc);
+}
+
+NS_BOOST_MEMORY_END
+
 #define BOOST_MEMORY_NEW_ARG(Type) sizeof(Type), NS_BOOST_MEMORY::destructor_traits<Type>::destruct
 #define BOOST_MEMORY_DBG_NEW_ARG(Type) BOOST_MEMORY_NEW_ARG(Type) BOOST_MEMORY_FILE_LINE_ARG
 
@@ -259,10 +350,7 @@
 #define BOOST_MEMORY_DBG_ALLOC_ARG(Type) sizeof(Type) BOOST_MEMORY_FILE_LINE_ARG
 #define BOOST_MEMORY_DBG_ALLOC_ARRAY_ARG(Type, count) sizeof(Type)*(count) BOOST_MEMORY_FILE_LINE_ARG
 
-// =========================================================================
-// NEW, NEW_ARRAY, ALLOC, ALLOC_ARRAY
-
-#define BOOST_MEMORY_NEW(alloc, Type) ::new((alloc).allocate(BOOST_MEMORY_DBG_NEW_ARG(Type))) Type
+#define BOOST_MEMORY_NEW(alloc, Type) NS_BOOST_MEMORY::_get_managed(alloc) ->* BOOST_MEMORY_UNMANAGE_NEW(alloc, Type)
 #define BOOST_MEMORY_NEW_ARRAY(alloc, Type, count) (alloc).newArray(BOOST_MEMORY_DBG_NEW_ARRAY_ARG(Type, count))
 
 #define BOOST_MEMORY_ALLOC(alloc, Type) ((Type*)(alloc).allocate(BOOST_MEMORY_DBG_ALLOC_ARG(Type)))

Modified: sandbox/memory/boost/memory/gc_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/gc_alloc.hpp (original)
+++ sandbox/memory/boost/memory/gc_alloc.hpp 2008-05-07 03:08:01 EDT (Wed, 07 May 2008)
@@ -453,22 +453,42 @@
         void* BOOST_MEMORY_CALL allocate(size_t cb, destructor_t fn)
         {
                 const size_t cbAlloc = cb + sizeof(MemHeaderEx);
- if (cb >= AllocSizeHuge)
+ if (cbAlloc >= AllocSizeHuge)
                         return m_hugeAlloc.allocate(cb, fn);
                 
+ return manage(unmanaged_alloc(cb, fn), fn);
+ }
+
+ void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, destructor_t fn)
+ {
+ BOOST_MEMORY_ASSERT(cb + sizeof(MemHeaderEx) < AllocSizeHuge);
+
                 DestroyInfo* pNode = (DestroyInfo*)allocate(sizeof(DestroyInfo) + cb);
                 pNode->fnDestroy = fn;
+ return pNode + 1;
+ }
+
+ void* BOOST_MEMORY_CALL manage(void* p, destructor_t fn)
+ {
+ DestroyInfo* pNode = (DestroyInfo*)p - 1;
+ BOOST_MEMORY_ASSERT(pNode->fnDestroy == fn);
+
                 pNode->pPrev = m_destroyChain;
                 m_destroyChain = (MemHeaderEx*)((char*)pNode - sizeof(MemHeader));
                 m_destroyChain->nodeType = nodeAllocedWithDestructor;
- return pNode + 1;
+ return p;
         }
 
- void* BOOST_MEMORY_CALL allocate(size_t cb, int fnZero)
+ void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, int fnZero)
         {
                 return allocate(cb);
         }
 
+ void* BOOST_MEMORY_CALL manage(void* p, int fnZero)
+ {
+ return p;
+ }
+
         void BOOST_MEMORY_CALL deallocate(void* pData, size_t cbData)
         {
                 const size_t cb = cbData + sizeof(MemHeader);

Modified: sandbox/memory/libs/memory/test/test_basic/memory/exception_semantics.cpp
==============================================================================
--- sandbox/memory/libs/memory/test/test_basic/memory/exception_semantics.cpp (original)
+++ sandbox/memory/libs/memory/test/test_basic/memory/exception_semantics.cpp 2008-05-07 03:08:01 EDT (Wed, 07 May 2008)
@@ -10,6 +10,7 @@
 // See http://www.boost.org/libs/memory/index.htm for documentation.
 //
 #include <cstdio>
+#include <boost/memory.hpp>
 
 class A
 {
@@ -56,7 +57,18 @@
 {
         try
         {
- Foo a;
+ Foo* p = new Foo;
+ }
+ catch (...)
+ {
+ printf("Unexcepted\n");
+ }
+
+ printf("Now, let's test operator new of GC Allocator...\n");
+ try
+ {
+ boost::auto_alloc alloc;
+ Foo* p = BOOST_NEW(alloc, Foo);
         }
         catch (...)
         {

Modified: sandbox/memory/libs/memory/test/test_basic/test.cpp
==============================================================================
--- sandbox/memory/libs/memory/test/test_basic/test.cpp (original)
+++ sandbox/memory/libs/memory/test/test_basic/test.cpp 2008-05-07 03:08:01 EDT (Wed, 07 May 2008)
@@ -21,8 +21,8 @@
 
 int main()
 {
- testPerformance();
-// testExceptionSemantics();
+// testPerformance();
+ testExceptionSemantics();
 // testSystemAlloc();
 // testPerformance();
 // simpleExamples();


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