Memory Allocation
Pintos contains two memory allocators, one that allocates memory in units of a page, and one that can allocate blocks of any size.
Page allocator
The page allocator declared in threads/palloc.h
allocates memory in units of a page.
It is most often used to allocate memory one page at a time, but it can also allocate multiple contiguous pages at once.
Memory Pools
The page allocator divides the memory it allocates into two pools, called the kernel and user pools.
By default, each pool gets half of system memory above 1 MB, but the division can be changed with the
-ul
kernel command line option.An allocation request draws from one pool or the other. If one pool becomes empty, the other may still have free pages.
The user pool should be used for allocating memory for user processes and the kernel pool for all other allocations. This will only become important starting with project 3. Until then, all allocations should be made from the kernel pool.
Each pool's usage is tracked with a bitmap, one bit per page in the pool. A request to allocate n pages scans the bitmap for n consecutive bits set to false, indicating that those pages are free, and then sets those bits to true to mark them as used. This is a "first fit" allocation strategy (see Wilson).
Types and Functions
Page allocator types and functions are described below.
Notes
The page allocator is subject to fragmentation. That is, it may not be possible to allocate n contiguous pages even though n or more pages are free, because the free pages are separated by used pages. In fact, in pathological cases it may be impossible to allocate 2 contiguous pages even though half of the pool's pages are free. Single-page requests can't fail due to fragmentation, so requests for multiple contiguous pages should be limited as much as possible.
Pages may not be allocated from interrupt context, but they may be freed.
When a page is freed, all of its bytes are cleared to
0xcc
, as a debugging aid (see section Tips).
Block allocator
The block allocator, declared in threads/malloc.h
, can allocate blocks of any size.
It is layered on top of the page allocator described in the previous section.
Blocks returned by the block allocator are obtained from the kernel pool.
The block allocator uses two different strategies for allocating memory.
The first strategy applies to blocks that are 1 kB or smaller (one-fourth of the page size). These allocations are rounded up to the nearest power of 2, or 16 bytes, whichever is larger. Then they are grouped into a page used only for allocations of that size.
The second strategy applies to blocks larger than 1 kB. These allocations (plus a small amount of overhead) are rounded up to the nearest page in size, and then the block allocator requests that number of contiguous pages from the page allocator.
Do you get deja vu? Did you implement these strategies in the Malloc Lab in ICS?
Types and Functions
The block allocator functions are described below. Their interfaces are the same as the standard C library functions of the same names.
Notes
In either case, the difference between the allocation requested size and the actual block size is wasted. A real operating system would carefully tune its allocator to minimize this waste, but this is unimportant in an instructional system like Pintos.
As long as a page can be obtained from the page allocator, small allocations always succeed. Most small allocations do not require a new page from the page allocator at all, because they are satisfied using part of a page already allocated.
However, large allocations always require calling into the page allocator, and any allocation that needs more than one contiguous page can fail due to fragmentation, as already discussed in the previous section. Thus, you should minimize the number of large allocations in your code, especially those over approximately 4 kB each.
When a block is freed, all of its bytes are cleared to
0xcc
, as a debugging aid (see section Tips).The block allocator may not be called from interrupt context.
Last updated