该书源代码可在该网站找到
http://csapp.cs.cmu.edu/public/code.html
Address Space
An address space is an ordered set of nonnegative integer addresses
{0, 1, 2, . . .}
地址空间是有序的非负整数的地址集合
The size of an address space is characterized by the number of bits that are needed
to represent the largest address. For example, a virtual address space with N = 2^n
addresses is called an n-bit address space. Modern systems typically support either
32-bit or 64-bit virtual address spaces.
地址空间的大小由表示最大地址所需的位数决定
现代计算机系统通常支持32位或64位的虚地址空间
A system also has a physical address space that corresponds to the M bytes of
physical memory in the system:
{0, 1, 2, . . . , M − 1}
Each byte of main memory has a virtual address chosen from the virtual address space, and a physical address chosen from the physical address space.
Conceptually, a virtual memory is organized as an array of N contiguous byte-sized
cells stored on disk. Each byte has a unique virtual address that serves as an index
into the array.
虚拟内存以存储在disk(磁盘)上的N个连续的单个字节大小的cell的阵列形式组织。每一个byte有唯一的虚拟地址作为该阵列中的索引
the data on disk (the lower level) is partitioned into blocks that serve as the transfer units between the disk and the
main memory (the upper level).VM systems handle this by partitioning the virtual memory into fixed-sized blocks called virtual pages (VPs). Each virtual page is P = 2^p bytes in size. Similarly, physical memory is partitioned into physical pages (PPs), also P bytes in size. (Physical pages are also referred to as page frames.)
磁盘上的数据划分成块来作为磁盘有主存字节的传输单元。虚拟内存系统通过把虚拟内存划分成固定大小的叫做虚页的块(virtual pages)来处理这个问题。
图9.3
左边第一幅图VP代表Virtual Page编号
n位的地址空间,总共有N=2^n个地址
每一个virtual page占据P=2^p个byte
因此总共的virtual page number(虚页数)=2^n/2^p=2^(n-p)
因此左边第一幅图中最后一个Virtual Page的编号为2^(n-p)-1
VM(virtual memory)
simplifying linking:
每个进程有独立的虚地址空间。separate address space allows each process to use the
same basic format for its memory image
独立的地址空间允许每个进程对它的内存映像(memory image)使用相同的基本格式。
Such uniformity greatly simplifies the design and implementation of linkers, allowing them to produce fully linked executables that are independent of the ultimate location of the code and data in physical memory.
这种一致性极大简化了链接器的设计与实现,允许他们产生完全链接好的可执行文件而不用考虑代码与数据在物理内存中的最终位置
类似于下图中每个进程拥有独立地址空间,分成Stack段,Heap段,BSS段,Data段等。
(该图引用自duartes.org by Gustavo Duarte)
simplifying memory allocation(简化存储分配):
如果一个用户进程需要额外的heap space(比如调用malloc),操作系统可以分配比如k个连续的虚拟内存页,
并且把这k个页映射到物理存储上处于任何位置的k个任意物理页(physical page),这k个物理页可以不连续地随机分散于物理内存中。
在unix系统上,malloc返回的块对齐到8字节(double word)的边界
void *Malloc(size_t size) { void *p; if ((p = malloc(size)) == NULL) unix_error("Malloc error"); return p; }
bp是指向block的payload的。而不是指向block的header。
block pointer(denoted as bp)that points to the first payload byte
mm_init函数
heap_listp+=(2*WSIZE)
当时想这个heap_listp为什么加上的是2*WSIZE
加上2*WSIZE其实heap_listp即指向了Prologue的footer啊
当时是想为什么不指向Prologue block的header(只要加WSIZE就可以了)或者指向Epilogue header(加上3*WSIZE)
其实呢heap_listp应该指向的是第一个块的payload,而Prologue block没有payload,只能指向它的header之后的内容,即footer了。
与bp指针正好有个对应
extend_heap函数中
12行
PUT(HDRP(bp),PACK(size,0));
看的时候觉得HDRP(bp)应该是先前分配的memory的epilogue block header的位置啊
然后书中有这么一句话,跟我想的一样
Thus every call to mem_sbrk returns a double-word aligned chunk of memory immediately following the header of the epilogue block. This header becomes the header of
the new free block (line 12), and the last word of the chunk becomes the new epilogue block header (line 14).