Topic: C++0x Support advance memory management & persistence


Author: "Dean Jiang" <deanjiang@hotmail.com>
Date: Mon, 13 May 2002 04:24:16 GMT
Raw View
Firstly I introduce a placement delete expression:

delete-expression:
        [::] delete [placement] cast-expression
        [::] delete [ ] [placement] cast-expression

The follows are original placement new expression and delete expression .
new-expression:
[::] new [placement] new-type-name [new-initializer]
        [::] new [placement] ( type-name ) [new-initializer]
delete-expression:
        [::] delete cast-expression
        [::] delete [ ] cast-expression


The usage of placement delete expression:
1)using with memory management.
2)using with object persistence.

Details:
1)Memory management:
    In standard, placement mechanism is provided mainly for reconstruct a
object. so the operator delete function is declared in standard:
        void * operator new(size_t size, void *pointer);
    But other form are also permitted. typical usage of the self-defined
placement operator is memory management.  For example:
        void * operator new(size_t size,CAllocator *pAllocator);
    Corresponding placement delete is also necessary in standard:
        void operator delete(void *ptr,CAllocator  *pAllocator);
    In which, CAllocator is a memory management class, and pAllocator point
to a instance of it. we can consider a CAllocator object as a user defined
heap. Then the memory can be allocated and managed by the user's heap.
    We can call this function in following form:
        CBaseType* pBase=new(pAllocator) CType;
    But we can't call corresponding delete function in similar form:
        delete(pAllocator) pBase; //ill-form usage of delete expression
    In many case we can simulate the action of delete:
        pBase->~CType();
        CType* pType=dynamic_cast<CType*>(pBase);
        operator delete(pType, pAllocator);
    But in some case, original type of the heap object (CType) is unbeknown,
e.g. objects in a list of base class pointer. How can we determine the
offset from pBase to pType.
    Another important thing is that such a complicated form will keep the
programers from doing that.
    As we have known that delete expression can translate the base class
pointer to the pointer of actual type.    Introducing the placement delete
expression (see also the begining) into the standard is the one and only
solution. Then we can write compact and accordant code.
    One of usage of the Allocators may be thread local heap management. One
Allocator is corresponding to a thread, and no synchronization operation is
necessary. It's also useful to check the leak of memory of specific thread.

2)Object persistence:
    Another important usage of Allocator is use with the memory map files.
You can access
the memory map of a file as a heap, which is managed by a Allocator. Just as
you had done on the usual heap, the memory map can be access conveniently.
if you use only the POD (plain old data, just like the data struct in C, and
except the pointer type.) type, Allocator can maintain a offset table which
record the offset of the object allocated in the memory map file. I think
it's more reasonable than memory-mapped container-like interface.

Do you think so?















---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]