boost::gc_alloc

Recently I write a new class named "boost::gc_alloc" (see boost/memory/gc_alloc.hpp). Here is its specification:

class gc_alloc
{
public:
    gc_alloc(); // same as: gc_alloc(tls_block_pool::instance());
    gc_alloc(block_pool& pool); // initialize by given a pool instance
    gc_alloc(gc_alloc& a); // initialize by given a indirect pool instance
    ~gc_alloc(); // clear
 
    // allocate memory without cleanup function
    void* allocate(size_t cb);
 
    // allocate memory with cleanup function
    void* allocate(size_t cb, destructor_t func);
 
    // release a normal memory buffer (without cleanup function)
    void deallocate(void* p, size_t cb);
 
    // delete an object instance allocated by the allocator    
    template <class Type>
    void destroy(Type* obj);
 
    // delete an array allocated by the allocator
    template <class Type>
    void destroyArray(Type* array, szie_t count);
 
    // cleanup and release all memory allocated by the allocator
    void clear();
 
    void swap(gc_alloc& o); // swap two instances
};

Comparing with boost::scope_alloc (boost/memory/scoped_alloc.hpp), it appends these methods:

{
    // release a normal memory buffer (without cleanup function)
    void deallocate(void* p, size_t cb);
 
    // delete an object instance allocated by the allocator    
    template <class Type>
    void destroy(Type* obj);
 
    // delete an array allocated by the allocator
    template <class Type>
    void destroyArray(Type* array, szie_t count);
}

Yes, it allows you to delete objects manually. But this is OPTIONAL, not a MUST. You don't need to delete objects, if you don't want or forget to do.

However, I have some additional NOTES:

  1. boost::auto_alloc (its old name is "AutoFreeAlloc") and boost::scoped_alloc (its old name is "ScopeAlloc") had been implemented for four years (from 2004). And they were widely used and tested. They are proved in practice.
  2. boost::gc_alloc was implemented yesterday (just a pre-alpha version). It is complexer than boost::auto_alloc and boost::scoped_alloc. Is it useful? Maybe, but it needs to be proved itself.

If you are interested in it, refer the source code:

page_revision: 2, last_edited: 1209579510|%e %b %Y, %H:%M %Z (%O ago)
Unless stated otherwise Content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License