Archived

This topic is now archived and is closed to further replies.

Memory Manager?

This topic is 5016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all, i have an idea, infact, its not my idea since its probably used "everywhere" but i want to create a class that can handle my memory ^^ and keep the adress stored there in a variable.. and then be able to add new objects ive created with the className *name = new className; then to be able to add it in my memory manager using Manager->addObject(name); the problem is, i dont know "how" to do that, i dont know what type i should use when defining addObject(what goes here?) so i can use it with all my objects.. and so on..

Share this post


Link to post
Share on other sites
the way it''s usually done is a bit backwards from how you''ve described. typically a game will startup and request 256MB or some other amount of memory from the machine:

char * gameMemory = new char[BIG_ASS_AMOUNT_OF_MEMORY];

then you overload the new/delete operators to ask the memory manager for memory. the memory manager looks to find the biggest chunk of unused memory, creates your object there and returns to you a pointer for that memory address.

that way the memory manager can try and keep your memory defragmented which is a typical cause of late game slowdown if you''re not using a memory manger.

i haven''t explained this in detail partly b/c i''ve never actually coded one and partly b/c i''m rushed to get to a meeting but i believe there should be some docs in the Articles & Resources section about making a solid memory manager.

oh and if you want to do your thing, you''d pass a void*, or maybe try using the boost smartpointer thing (never used that myself but people like it).

-me

Share this post


Link to post
Share on other sites
well, if you''re passing a pointer to the memory manager, you can use void* which acts like a universal type for pointers (void* can point to any pointer type) but keep in mind that your code will have to know what to cast it back to when it gets a copy of the pointer, since you can''t assume that the void* pointer will have methods or member variables.

There is an operator new() that can be overridden, also, and this will let you handle what happens when you call new className;

Doing your own memory management is a pretty advanced topic and usually best left to the core library code. I would suggest an alternative, like deriving all game objects from a base class (kind of like Object in Java), and putting in some boilerplate code that will dictate certain behavior for all of your game objects...

Share this post


Link to post
Share on other sites
You could overload the new operator for each class that you want to manage memory for. Essentially, you would have a seperate memory pool for each class that you want to manage. Sort of like:

void* classname::operator new( size_t size )
{
// create a new heap if one doesn''t already exist for the class
if( m_heap == NULL )
m_heap = HeapFactory::CreateHeap( heapname );

return ::operator new( size, m_heap );
}

The Heap object could be used to keep some information about the objects you have already new-ed (like allocation size, where the allocation is, etc.) so that later on you can deallocate this memory (or re-use it). Another technique would be allocating a lot of memory for many objects all at one time similar to what Palidine suggested. That is definitely useful if you know that you will need to allocate a lot memory since allocating memory for each individual object usually is too slow. You can also deallocate all at once, just be careful how you do it if your objects need their destructors called.

-Dylan

Share this post


Link to post
Share on other sites