Memory Management

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Adek336

Memory Management

Post by Adek336 »

Hello. How one could usually build a heap allocator? I can't think of a way to deallocate the blocks safely.

Greets,
Adrian.
mikeleany

RE:Memory Management

Post by mikeleany »

I can tell you two ways of keeping track of allocated memory so you can deallocate it somewhat safely. One way is to allocate a few extra bytes each time you allocate memory, just before the pointer returned by malloc (or new). You use these bytes to store the size of the allocated memory block. Another way is to keep a table of some sort of all allocated memory. With the first method there is no way to verify that a pointer passed to free (or delete) really points to allocated memory. With the second method, however, if something isn't in the table, it isn't allocated, so it's safer than the first method. The problem with the second method is that you have to make a table big enough for the worste case scenario.

Is that what you were asking? Or did I misunderstand the question?
Adek336

RE:Memory Management

Post by Adek336 »

yep!

i had a gloomy idea of having a linked list. but then i'd have to have a malloc for servicing itself; so I thought about a malloc2, which worked independendly from malloc and had no deallocation option. But it would take the whole memory after some time!

But when I have a static table, it's simpler.

Thanx, Adrian.
ka3r

RE:Memory Management

Post by ka3r »

When I started designing my first OS, I didn't want to write an allocator so I ported the Doug Lea's dlmalloc to the VMM, and I worked fine.
Search on the net this allocator, it is very fast.
Post Reply