目的: 檢驗學員對 Linux 核心記憶體管理 的認知
1
延伸 第 11 週測驗題 (上) 和 第 11 週測驗題 (中),考慮 buddy 這個 buddy memory allocator 的實作,請補完下列函式:
作答區
A1 = ?
(a)
node_is_split[index / 8] ^= 1 << (index % 8);
(b)
node_is_split[index / 8] = 1 << (index / 8);
A2 = ?
(a)
bucket++;
(b)
size *= 2;
(c)
bucket--; size *= 2;
(d)
bucket++; size *= 2;
A3 = ?
(a)
/* no operation */
(b)
list_init(&buckets[--bucket_limit]);
(c)
list_init(&buckets[bucket_limit]);
(d)
list_init(&buckets[bucket_limit++]);
A4 = ?
(a)
/* no operation */
(b)
ptr = (uint8_t *)list_pop(&buckets[--bucket]);
(c)
ptr = (uint8_t *)list_pop(&buckets[bucket]);
A5 = ?
(a)
list_remove((list_t *)ptr_for_node(i + 1, bucket));
(b)
list_remove((list_t *)ptr_for_node(i, bucket));
(c)
list_remove((list_t *)ptr_for_node(i - 1, bucket));
(d)
list_remove((list_t *)ptr_for_node(((i - 1) ^ 1) + 1, bucket));
延伸問題:
sbrk
系統呼叫使用,注意其 alignment 行為,需要探討到 Linux 核心內部設計sbrk
換為 mmap
或者其他系統呼叫,並且該如何設計測試程式,才能涵蓋 malloc
和 free
的行為?