Memory pool & char array questions.

May 10, 2011 at 10:02pm
Hi,
I was recently assigned a project and I'm looking for a little advice.
I have to implement a memory manager that has a char array for a memory pool.
So what I have concepted out was to segment the pool up into variable sized chunks, using the begining of the mempool to hold the location of the chunks as well as if they are being used.
I think its a fairly good solution as the conditions are that no other static or global variable may be created (the reason I went with this route was the hint that if we need to save data, we need to make it fit in the pool)

In any case on to my questions...
1)Anyone have any experience with variable pools, and would this approach theoretically work? I wouldnt want to get a day or two into it and hit a major design flaw that requires scrapping the whole project.

2)A character array should be able to store addresses of other portions of the same array, can anyone offer some suggested reading on how to go about doing so (syntax-wise)? (my debugger is giving me all sorts of issues trying to access the array...keep getting the "Symbol not found" even though its clearly there and being accessed)

Any advice would be greatly appreciated!!

Thanks!
-G
May 10, 2011 at 10:33pm
You can find interesting reads over here (with example sources):
http://www.tantalon.com/pete.htm
May 10, 2011 at 10:51pm
I have read some of his stuff (even have Game Gems 3 that detailed his allocator code)
It did give me a few ideas to think about, but a lot of it has to do with vectors and not particularly applicable to my project. I did however bookmark the site to study up on it further....thanks for the link!
May 11, 2011 at 6:02am
I suggest you first think about the design before jumping into the code. Writing a memory pool isn't easy.

Allocating memory from the pool for different types
memory holes, fragmentation are some of the initial thoughts which will be a pain to implement optimally.

I have a book on memory management at home. I didn't read that one yet and don't know whats inside that but i think it contains matter relating to developing memory pools. Will mail you that if that has some discussion regarding memory pools.
May 11, 2011 at 9:24am
to segment the pool up into variable sized chunks
Can you explain what the aim is.

)A character array should be able to store addresses of other portions of the same array.
What does this mean?

It may be helpful if to stop thinking about char arrays and thought in terms of memory blocks that you manage and suballocate from.

You will have to think about handling large allocations, lots of little allocations, reducing fragmentation, error detection/heap integrity checks, concurrency, speed.
May 11, 2011 at 6:09pm
@writeonsharma - I have written very little code at the moment (as far as this project is concerned), I am currently concepting out how this should all work. Definitely want to have a solid plan / algorithm before I start coding.

@kbw - the program will need to store various sized chunks.
So my initial thought was to have several linked lists (a list for each size chunk - ex: 8byte chunks, 32 byte chunks etc...) and have the beginning of my memory pool point to the location of the head of each list. Unfortunately, that doesnt really work since its a char array and not an array of pointers.

That being the case, I have started to think about going another route. Creating a struct that handles managing the memory pool, and storing an object of that struct within the memory pool itself.
That way, I can retrieve the object anywhere in the program since the memory pool is the only global.

The biggest problem with this project is being able to share variables across the program without making any global or static variables. This approach I am going with now seems to work. Or, at least I cant see any major issues with doing so.

Any thoughts?
May 11, 2011 at 6:17pm
Sounds to me this is the same thing: http://www.cplusplus.com/forum/general/42812/.
May 11, 2011 at 6:46pm
It is pretty similar. Only that his project had a class that he inherited from.
Im just dealing with functions. However, reading through that post at least reaffirms that im going in the right direction with the use of a struct that points to the memory pool.
Now I just need to work out if I want a few lists of fixed sizes(should be easier) or have 2 lists, one allocated, one free and worry about loss of memory due to fragmentation.
May 12, 2011 at 2:46am
After a good days work, everything seems to be working correctly. Lists are linked properly and allocate well. EXCEPT the below...and I have no idea why!!!
My 2 largest sizes are giving me trouble when building the linked list.
They are going to be 4000 x 4 and 16000 x 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pListStart = &MM_pool[(sizeof(CMemoryPoolManager)+1) + 17408]; //start the new list at the end of the last one
MemoryBlock *pCurrent = (MemoryBlock *)pListStart; //pCurrent is the current "chunk" being assigned
pCurrent = (MemoryBlock *)pListStart;
for (int i=0; i < 4; i++) //there will be 4 chunks of this size
	{
		pCurrent = (MemoryBlock *)pListStart + i * 4000;
		pCurrent->pPrevious = NULL; // at the 4th iteration it causes an 
					//"Unhandled exception at 0x01391275 in MemoryManager.exe: 0xC0000005: Access violation writing location 0x013c6c8d."
		pCurrent->pNext = pFree4000; //assign next to head of list
		if (pFree4000 != NULL) //check if its the first chunk
		{
			pFree4000->pPrevious = pCurrent; //linked list stuff.
		}
		pFree4000 = pCurrent;
	}


it cant be because I am running out of space in the memory pool because I even changed the "start list" to start at the beginning of the memory pool to test it(it has a size of 65536)
4000x4 (or 16000) should fit in the pool.

Im having trouble understanding why it would throw an error at pCurrent->pPrevious = NULL on the 4th iteration.
I havent had this problem on any other smaller sizes.

Anyone have any ideas on what could be the cause of the problem?
If you need any other information, i'd be glad to supply it.
Hopefully someone can shed some light as I have been battleling with this problem for a few hours now.

Thanks!
Topic archived. No new replies allowed.