So this is the way I?m doing it:
I use a struct before every new allocated memory block:
Code: Select all
struct mem_block {
byte available; // free or not?
dword size;
struct mem_block *next, *prev;
}
When freeing a block, I set the *next, *prev pointers to the next/previous freed block(can be anywhere, it?s like a linked list). When I want to allocate something, I look through the linked list of freed blocks and try to find the "best fitting" block. If there?s no free fitting blocks available, I create a new block (at the address where the pointer I mentioned earlier points).
After the mem_block-information and the allocated data, I put the size again, so every block looks like:
available
size
next/prev
..DATA...
size
this makes it simple for me to reach the previous block from a block. Because I can reach the next/prev block (Not the next/prev Available block that are stored in the struct), it?s easy to merge freed blocks bordering on eachother.
Just wondered if it?s completly crazy done or what? This way I wont waste memory, and the allocation is fast (the only thing that takes time is to look through the linked list to find the best freed block).