Mov_me_baby wrote:
@Crazed123: When I tried implementing my linked-list system it was to run the kernel malloc system, which of course resulted in the infinite recursive loop (called malloc, which split a "free" node, which caused another call to malloc, which then called... and so on) I'm going to try and figure out how to do it without that problem, but I've been working all day and I haven't had a chance to program.
When actually implementing malloc you avoid making calls to malloc by storing internal variables containing the linked lists that span the heap. The key is to start out with a fixed-length heap so that one free node (or several smaller ones more likely to fit a request, depending on your algorithm) completely contain it, and then whenever any of those are split or coalesced they stay within those exact boundaries, thus not requiring a call to malloc for more memory.
An example. If I have a completely free page of memory set up to use malloc under my algorithm it would look like this:
Each of those letters is a single byte, and the ....s are the rest of the page. LLLL is the length of the block, not counting allocation overhead that's used when the page is allocated, A is a Boolean stating whether the page is allocated or not, PLFB is a pointer to the last free block, PNFB is a pointer to the next free block, and the final LLLL is another length (designed to have the same value as the first) that is used to check for block validity. The two pointers to free blocks are NULL, A is false (free block), and the lengths are the appropriate numbers. Now, if I allocate a block out of that I get:
Code: Select all
LLLLA..............LLLLllllaplfbpnfb.........llll
Where the capital letter block is allocated now (A=true), and the two pointers have disappeared into the allocated space. The lowercase l's contain the length of the new free block split off from the old one, and the other fields are alike too, but lowercase. Now let's say I free the first block, but there's no coalescence of free blocks quite yet:
Code: Select all
LLLLAPLFBPNFB...............LLLLllllaplfbpnfb.......llll
Where the capital PNFB now points to the first lowercase l, the lowercase plfb points to the first capital L in memory (before A), and the a's and lengths are set appropriately.
Now coalescence just liquifies the second set of capital L's (the barrier tags), the lowercase allocation data, calls them part of its data, thereby adding their lengths and the length stored in the lowercase l's to the length in the first LLLL, and changes the final llll to an LLLL barrier tag.
That was a hopefully understandable rundown of a fairly simple malloc. There are probably better, faster, smaller-overhead algorithms out there to use, and I encourage you to find them. Just get a solid understanding that malloc is based around WHAT you put in a FIXED length of data, thereby creating allocation data and a heap where before only existed a few empty pages.