Page 1 of 1
Memory Management
Posted: Wed May 28, 2003 11:00 pm
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.
RE:Memory Management
Posted: Thu May 29, 2003 11:00 pm
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?
RE:Memory Management
Posted: Fri May 30, 2003 11:00 pm
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.
RE:Memory Management
Posted: Fri May 30, 2003 11:00 pm
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.