Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 5th, 2006, 5:04 PM   #1
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4 Klarre is on a distinguished road
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);
}
Klarre is offline   Reply With Quote
Old Mar 5th, 2006, 5:16 PM   #2
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
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.
Kaja Fumei is offline   Reply With Quote
Old Mar 5th, 2006, 6:08 PM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
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);
}
Essentially, it is necessary to store the size of the block BEFORE the block to be able to access it. Storing it after the block gives the conundrum of needing to know the length to be able to extract the length.

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).
grumpy is offline   Reply With Quote
Old Mar 6th, 2006, 5:31 PM   #4
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4 Klarre is on a distinguished road
Thansk alot for the code you wrote! It helped me alot, and was exactly the answear I was looking for!

/Klarre
Klarre is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC