一个节点定义如下:
struct node {
int value;
struct node *next;
};
通过使用sizeof(struct node)
,我了解到一个节点是 8 个字节(在 xv6 中)。所以我使用malloc
分配一些内存空间来存储一些节点。xv6 中单页是 4096 字节,如果我有 8 页,我可以存储 4096 个这样的节点。但是,这不是发生的事情,在我malloc
2048 个这样的节点之后,如果我malloc
再添加一个,则为当前进程分配更多的页面,这是为什么呢?
// Now display how many pages are allocated to the process
// Suppose there is a system call named memcount(), it is given by
// my professor, I wouldn't think there's any problem with that
//
memcount(); // which prints 3, meaning that initially, without
// allocaing anything, 3 pages = 12288 bytes of memory allocated
for(i = 0; i < 2048; ++i) {
struct node *nd = (struct node *) malloc(sizeof(struct node));
}
memcount(); // which prints 11, so 8 more pages are allocated
// If we allocated 1 more node
struct node *nd = (struct node *) malloc(sizeof(struct node));
memcount(); // which prints 19, another 8 pages are allocated
这就是我很困惑的地方,前8页不应该有很多空间吗?既然单个节点的大小只有8个字节,为什么还要给进程分配更多的页呢?