「BUAA OS Lab2 Thinking」Thinking2.3详解

Posted by saltyfishyjk on 2022-04-10
Words 288 and Reading Time 1 Minutes
Viewed Times

「BUAA OS Lab2 Thinking」Thinking2.3详解

Thinking2.3引用的函数与定义比较多且散乱,以下从题目入手逐层展开:

通过pmap.h可以看到Page结构如下

1
2
3
4
5
6
7
8
9
10
struct Page {
Page_LIST_entry_t pp_link; /* free list link */

// Ref is the count of pointers (usually in page table entries)
// to this page. This only holds for pages allocated using
// page_alloc. Pages allocated at boot time using pmap.c's "alloc"
// do not have valid reference count fields.

u_short pp_ref;
};

又知道Page_LIST_entry_t在宏定义typedef LIST_ENTRY(Page) Page_LIST_entry_t;中声明,查看LIST_ENTRYqueue.h中声明如下:

1
2
3
4
5
#define LIST_ENTRY(type)                                                    \
struct { \
struct type *le_next; /* next element */ \
struct type **le_prev; /* address of previous next element */ \
}

到此为止,可以确定Page结构体的展开结构如下:

1
2
3
4
5
6
7
struct Page {
struct {
struct type *le_next;
struct type **le_prev;
} pp_link;
u_short pp_ref;
};

查看queue.h中的LIST_HEAD声明如下:

占老师锐评:这下工厂模式了

1
2
3
4
#define LIST_HEAD(name, type)                                               \
struct name { \
struct type *lh_first; /* first element */ \
}

因此在pmap.h中声明的IST_HEAD(Page_list, Page)完成了Page_list的构建如下:

1
2
3
struct Page_list { 
struct Page *lh_first;
}

再将Page结构体展开即可得:

1
2
3
4
5
6
7
8
9
struct Page_list { 
struct {
struct {
struct type *le_next;
struct type **le_prev;
} pp_link;
u_short pp_ref;
} *lh_first;
}

Tip:以上内容得益于占老师的大力支持\zry/\zry/\zry/


This is copyright.