views:

84

answers:

3

Hi,

I am currently writing a small custom memory Allocator in c++, and want to use it together with operator overloading of new/delete. Anyways, my memory Allocator basicall checks if the requested memory is over a certain threshold, and if so uses malloc to allocate the requested memory chunk. Otherwise the memory will be provided by some fixedPool allocators. that generally works, but for my deallocation function looks like this:

     void MemoryManager::deallocate(void * _ptr, size_t _size){
  if(_size > heapThreshold)
   deallocHeap(_ptr);
  else 
   deallocFixedPool(_ptr, _size);
 }

so I need to provide the size of the chunk pointed to, to deallocate from the right place.

Now the problem is that the delete keyword does not provide any hint on the size of the deleted chunk, so I would need something like this:

 void operator delete(void * _ptr, size_t _size){ 
     MemoryManager::deallocate(_ptr, _size); 
 }

But as far as I can see, there is no way to determine the size inside the delete operator.- If I want to keep things the way it is right now, would I have to save the size of the memory chunks myself? Any ideas on how to solve this are welcome! Thanks!

+2  A: 

You could keep a map of memory address to size for your pool-allocated memory. When you delete, check if the pointer is in the map, if it is delete that size, if it isn't call regular delete.

JRL
thank, that sounds like a valid option.
+4  A: 

allocate more memory than neccessary and store the size information there. That's what your system allocator probably does already. Something like this (demonstrate with malloc for simplicity):

void *allocate(size_t size) {
    size_t *p = malloc(size + sizeof(size_t));
    p[0] = size;           // store the size in the first few bytes
    return (void*)(&p[1]); // return the memory just after the size we stored
}

void deallocate(void *ptr) {
    size_t *p = (size_t*)ptr; // make the pointer the right type
    size_t size = p[-1];      // get the data we stored at the beginning of this block

    // do what you need with size here...

    void *p2 = (void*)(&p[-1]); // get a pointer to the memory we originally really allocated
    free(p2);                   // free it
}
Evan Teran
I will propably try this! Thank you!
A: 

For class type, C++ already supports it directly. For nonclass types, you need to store the size manually like the other solution shows.

struct MyClass {
  void operator delete(void *p, size_t size) {
    MemoryManager::deallocate(p, size);
  }
};
Johannes Schaub - litb
ah, thats good to know too, thx!
hey, can I dynamically check if the type is a class/struct?
@user300713: what litb is suggesting is that you overload delete/new for each class you would like to apply it to instead of globally (look carefully at the example he gave). So no need to "detect" if the pointer is a class type, the right version of the function is called based on the per class overload.
Evan Teran