![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4
![]() |
sizeof(void*)
I am working on a simple memory manager, which by now only have overloaded the new and delete operators. What I want to do is to keep track of how many bytes that have been allocated. In the new function I simply add the "size" to the "allocatedBytes" variable. But how do I do in the delete function? How do I check the size of the object that "adress" is pointing at? Is it even possible? Design tips are appreciated! Thanks for your help!
/Klarre unsigned int allocatedBytes = 0;
void* operator new(unsigned int size)
{
allocatedBytes += size;
return malloc(size);
}
void operator delete(void* adress)
{
allocatedBytes -= ???; // What should I replace the ??? with?
free(adress);
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
Well it would be possible if you looked through the source of the malloc implementation but that's certainly not a portable solution since different compilers generally have different malloc implementations. The only portable way would be to keep track of it yourself using a list or some other data structure.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
A more general approach would be something like this;
// example to illustrate approach. Code not fed to compiler or tested....
size_t allocated_bytes = 0;
size_t physically_allocated_bytes = 0;
void *operator new(size_t size)
{
size_t actual_allocation = size + sizeof(size_t);
char *temp = (char *)malloc(actual_allocation);
if (temp == NULL)
throw std::bad_alloc; // operator new throws exception on failure
size_t *size_store = (size_t *)temp;
*size_store = size;
allocated_bytes += size;
physically_allocated_bytes += actual_allocation; // keep track of our overhead
void *retval = temp + sizeof(size_t);
return retval;
}
void operator delete(void *pointer)
{
// warning: the assumes pointer was allocated using our operator new
// undefined behaviour will result if that isn't true.
char *char_pointer = (char *)pointer.
char *actual_memory = pointer - sizeof(size_t);
size_t *size_store = (size_t *)actual_memory;
allocated_bytes -= *size_store;
physically_allocated_bytes -= size_store;
physically_allocated_bytes -= sizeof(size_t);
free(actual_memory);
}It would also be necessary to provide corresponding versions of operator new[], operator delete[], and placement versions (eg the std::nothrow version). One other challenge would be working out how these operators interact with the allocators in standard containers (vector, etc). I'll leave that as an exercise. If you want operator delete to pick up on cases of deleting invalid memory, it will be necessary to keep some form of map of memory actually returned by operator new, and compare against that. Given that we're playing with operators new and delete, the simple approach of using std::map<> would be problematical (as default allocators for std::map<> use operators new and delete). |
|
|
|
|
|
#4 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4
![]() |
Thansk alot for the code you wrote! It helped me alot, and was exactly the answear I was looking for!
/Klarre |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|