UNIX 6th code reading - mfree(english translation)

this entry is the english translation of http://d.hatena.ne.jp/takahirox/20101107/1289099957

introduction

today, i'll make notes of mfree( ) that releases assigned memory area.

this entry follows http://d.hatena.ne.jp/takahirox/20110421/1303387509

logic

the logic of mfree( ) is like this.

the routine moves the pointer to an empty entry which follows released entry address at 2564. the upper image of the above figure expresses it.

2564 : for( bp = mp; bp->m_addr <= a && bp->m_size != 0; bp ++ ) ;


"for" of 2564 works because the empty entries are in address order. (but i think it's better to use "while" than "for" for readability. )

and then, the released entry is inserted to the array of map structure at 2565 - 2587. the lower image of the above figure expresses it.

when there are adjoined empty entries, they have to be joined. that's the "Rule". therefore the logic of mfree( ) branches. it depends on forward and backward entries.

  • 2565-2574 : in case that the released entry touches a forward empty entry. they are joined.(2566)。
    • 2567-2574 : besides in case that it touches a backward empty entry. they are joined and following entries shift to forward.(2569-2573)
  • 2575-2587 : in case that the released entry doesn't touch a forward empty entry.
    • 2576-2578 : in case that the released entry touches a backward empty entry. they are joined.
    • 2579-2586 : in case that the released entry doesn't touch a backward entry. the released entry is inserted to the array, and then the following entries shift to backward.

conclusion

i'm going to make notes of prf.c next time.

this entry is followed by http://d.hatena.ne.jp/takahirox/20110522/1306045995