UNIX 6th code reading - malloc(english translation)

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

Introduction

recently, i began to read "Lions' Commentary on UNIX".

i think that it's not enough to understand to just only read it, so i decided to make notes.

i hope it helps you who reads this book.

Lions’ Commentary on UNIX (Ascii books)

Lions’ Commentary on UNIX (Ascii books)

http://www.amazon.com/dp/1573980137

i'm going to write it from chapter 5 to the end. it's the most important thing not to give up.

malloc

two kinds of the memory resources
coremap
main memory in unit of 32word(64bytes).
swapmap
disk swap area in unit of 256word(512bytes).

one of them is passed to malloc( ) and mfree( ). the routines don't need to recognize which of them they get.

the logic of the memory assignment

it uses first fit algorithm like this

according to "Moders operating systems"(2nd, japanese version.) P96-97,
first fit is faster algorithm than nextfit, best fit and so on.

OSの基礎と応用―設計から実装、DOSから分散OS Amoebaまで

OSの基礎と応用―設計から実装、DOSから分散OS Amoebaまで

  • 作者: A.S.タネンバウム,Andrew S. Tanenbaum,引地信之,引地美恵子
  • 出版社/メーカー: ピアソンエデュケーション
  • 発売日: 2000/04
  • メディア: 単行本
  • 購入: 4人 クリック: 151回
  • この商品を含むブログ (13件) を見る

perhaps, the pdp11 don't have so large memory area that a simple algorithm, like first fit, would be fitting.

the above figure is just image. the below figure expresses the more actual logic, the array of empty entry(map structure) exists in kernel.

the last entry of it is the area which size is 0. the liner search goes through to it. that is "sentinel". i think this idea is more flexible for the number of array elements and memory area than the liner search with "for".

if required size equal an empty area size, the empty area will be removed and the following empty areas shift ahead.

the return value of malloc( ) is the relative memory address when succeed. when not succeed 0 will be returned.

map
struct map {
  char *m_size ; /* the size of available memory area*/
  char *m_addr ; /* the relative address of available memory area */
} ;

coremap and swapmap are declared as the array of integer, but they are dealt as the array of map structure.
therefore, the numbers of coremap elements and swapmap elements are declared as CMAPSIZ and SMAPSIZ respectively, but the numbers of their actual elements are CMAPSIZ/2 and SMAPSIZ/2 respectively.

(even number elements correspond m_size and odd number elements correspond m_addr? how are they handled in the processor?)

the other questions

  • how does the kernel work when there are much many empty segmentation areas? does it garbage collection?
  • how large are main memory area and disk swap area?

Conclusion

i'm going to write the notes of mfree next time.

i know my english is rubbish, so if it's not too much bother, would you please tell me more appropriate english expression than i wrote here.

it's the happiest thing to tell you world wide what i studied. thanx.

this entry is followed by http://d.hatena.ne.jp/takahirox/20110423/1303541336