2005/5/10

     
 

ACDK Allocator

artefaktur

| Heaps | Allocator | Memory Debuging | RC/GC mechanism |



ACDK supports per object instance allocators via the new operator.


Content of this chapter:

   Allocating objects with standard new
   Allocating member variable with same allocator of the holding object
   Specialized version of clone


 Allocating objects with standard new

Using the standard new operator to allocate a new object, the standard Heap will be used (by default the PA_Heap).

But it is also possible to select a specific Allocator for each object instance.


ACDK_DECL_CLASS(MyClass);

class MyClass 
: extends Object
{
  RHashMap map;
public:
  MyClass()
    // use the same Allocator which was used to allocate MyClass
  : map(new (allocator()) HashMap())
  {
  }
};

// this allocates the storage
// from the standard heap 
RMyClass map = new MyClass();

this new calls (simplified):

  void* ObjectBase::operator new(size_t size)
  {
    // get the active allocator for this thread
    acdk::lang::sys::Allocator* alloc = ObjectHeap::allocator();
    // allocate the memory for this object
    void* ptr = alloc->allocate(size, acdk::lang::sys::ObjectMem);
    return ptr;
   }
The  acdk::lang::sys::Allocator is an interface to allocate and free memory. Additionally an allocator provides information about all all allocated object and invoke a garbage collection cycle. Beside the basic functionality to allocate and free memory all other operations are optional.

 Allocating member variable with same allocator of the holding object


Inside the constructor it calls another version of the new operator:


  // simplified:
void* 
ObjectBase::operator new(size_t size, ::acdk::lang::sys::Allocator* allocator)
{
  return allocator->allocate(size, acdk::lang::sys::ObjectMem);
}
This ensures, that the member map of the class MyClass will use the same allocator.

 Specialized version of clone


In ACDK two versions of clone function are provided:

  // known by Java
  RObject clone(); 
  // construct the new cloned object from given
  // allocator storage
  RObject clone(acdk::lang::sys::Allocator* alloc);