Jump to content
  • Advertisement

Archived

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

Austrian Coder

Deallocation of memory manager

This topic is 5718 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! I am wirtting a memory manager. Allocatation works perfectly. I only have some problems with the deallocation routine. This is the way, how i allocate:
          
void* CMemoryManager::Allocate(unsigned int size)
{
	...
	void *r = m_MemAlloc + m_UsedMemory;
	m_UsedMemory += size;
	m_FreeMemory = m_MemorySize - m_UsedMemory;
    
	return r;
}
          
This is how a situation of deallocation can look like: a... used memory d... memory(pointer) to deallocate b... used memory f... free memory I have an CMemorySegment class, which saves the beginning and ending of a pointer. So i run through all memory segements to find ending. The beginning i am getting so: unsigned int start = (unsigned int)pointer; After i have the ending i set the pointer to null. Now i need to move b at the ending of a. Is there a way to do this? Thanks, Christian [edited by - Austrian Coder on April 20, 2003 8:08:33 AM]

Share this post


Link to post
Share on other sites
Advertisement
One of the things you can do (which, AFAIK, is also done by at least one implementation the C runtime library) is allocate memory, store the size of this block and return a pointer following it.

Something like this:

  
...
unsigned int *r = (unsigned int *)(m_MemAlloc + m_UsedMemory);
//Store size and advance pointer

*r++ = size;
//Extra memory usage is the size info

m_UsedMemory += size + sizeof (unsigned int);
...
return r;


Then, when you deallocate a block:


  
void CMemoryManager::Deallocate (void *ptr)
{
unsigned int *i = (unsigned int *)ptr;
unsigned int size = i [-1];
...
}


You can also use this trick to store other information, such as alloc IDs, debug padding values (MSVC debug runtime does this) or even the type of allocation.

Share this post


Link to post
Share on other sites
Have a look at the Quake I source code. the files which implement a memory manager are (from my memory):

zone.c - implementation
zone.h - header

The memory manager is initialized somewhere in common.c, from what i remember.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!