Memory Manager?

Started by
2 comments, last by Mupp 20 years ago
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..
Advertisement
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
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...
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."
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

This topic is closed to new replies.

Advertisement