I’m currently working through chapter 2.5 (Dynamic Storage Allocation) of The Art of Computer Programming.
While working through the algorithm A on page 437 (First-fit method) I have stumbled over a problem understanding it. The algorithm itself is very clear and straight-forward. But the description of the step A1 gives me a puzzle. I’ll reprint here the whole algorithm in Knuth’s wording.
Algorithm A (First-fit method). Let AVAIL point to the first available block of storage, and suppose that each available block with address P has two fields: SIZE(P), the number of words in the block; and LINK(P), a pointer to the next available block. The last pointer is ?. This algorithm searches for and reserves a block of N words, or reports failure.
- A1.
- [Initialize.] Set Q ? LOC(AVAIL). (Throughout the algorithm we use two pointers, Q and P, which are generally related by the condition P = LINK(Q). We assume that LINK(LOC(AVAIL)) = AVAIL.)
- A2.
- [End of list?] Set P ? LINK(Q). If P = ?, the algorithm terminates unsuccessfully; there is no room for a block of N consecutive words.
- A3.
- [Is SIZE enough?] If SIZE(P) ? N, go to A4; otherwise set Q ? P and return to step A2.
- A4.
- [Reserve N.] Set K ? SIZE(P)-N. If K = 0, set LINK(Q) ? LINK(P) (thereby removing an empty area from the list); otherwise set SIZE(P) ? K. The algorithm terminates successfully, having reserved an area of length N beginning with location P+K.
Now what puzzles me is the assertion in step A1:
We assume that LINK(LOC(AVAIL)) = AVAIL. The explanations for LOC on page 235 reads as follows:
If V is the name of some value held in a memory cell, LOC(V) denotes the address of that cell. Consequently if V is a variable whose value is stored in a full word of memory, we have CONTENTS(LOC(V)) = V.
So this whole assertion currently only would make sense if it was LINK(LOC(AVAIL)) = P. Could any of my readers here explain me what I don't get in that explanation. Please enlighten me.