This procedure returns the area of size “size” at address “aa” to the “resource map” designated by “mp”. The body of the procedure consists of a one line “for” statement, followed by a multiline “if” statement.
The semicolon at the end of this line is extremely significant, terminating as it does the empty statement. (It would aid legibility if this character were moved to a line on its own, as is done on line 2394.)
Depending on your point of view, this statement demonstrates either the power or the obscurity of the “C” language. Try writing equivalent code to this statement in another language such as Pascal or PL/1.
Step “bp” through the list until an element is encountered either with an address greater than the address of the area being returned.
i.e. not “bp-m_addr
a”
or which indicates the end of the list
i.e. not “bp-m_size != 0”;
We have now located the element in front of which we should insert the new list element. The question is: Will the list grow larger by one element or will amalgamation keep the number of elements the same or even reduce it by one?
If “bp mp” we are not trying to insert at the beginning of the list. If
(bp-l)-m_addr+(bp-l)-
m_size==a
then the area being return abuts the previous element in the list;
Increase the size of the previous list element by the size of the area being returned;
Does the area being returned also abut the next element of the list? If so
Add the size of the next element of the list to the size of the previous element;
Move all the remaining list elements (up to the one containing the final zero size) down one place.
Note that if the test on line 2567 fortuitously gives a true result when “bp-m_size” is zero no harm is done;
This statement is reached if the test on line 2565 failed i.e. the area being returned cannot be amalgamated with the previous element on the list.
Can it be amalgamated with the next element? Note the check that the next element is not null;
Provided the area being returned is genuinely non-null (perhaps this test should have been made sooner?) add a new element to the list and push all the remaining elements up one place.