PKUOS - Pintos
Pintos source browser for PKU Operating System course
|
A simple implementation of malloc(). More...
Data Fields | |
size_t | block_size |
Size of each element in bytes. More... | |
size_t | blocks_per_arena |
Number of blocks in an arena. More... | |
struct list | free_list |
List of free blocks. More... | |
struct lock | lock |
Lock. More... | |
A simple implementation of malloc().
The size of each request, in bytes, is rounded up to a power of 2 and assigned to the "descriptor" that manages blocks of that size. The descriptor keeps a list of free blocks. If the free list is nonempty, one of its blocks is used to satisfy the request.
Otherwise, a new page of memory, called an "arena", is obtained from the page allocator (if none is available, malloc() returns a null pointer). The new arena is divided into blocks, all of which are added to the descriptor's free list. Then we return one of the new blocks.
When we free a block, we add it to its descriptor's free list. But if the arena that the block was in now has no in-use blocks, we remove all of the arena's blocks from the free list and give the arena back to the page allocator.
We can't handle blocks bigger than 2 kB using this scheme, because they're too big to fit in a single page with a descriptor. We handle those by allocating contiguous pages with the page allocator and sticking the allocation size at the beginning of the allocated block's arena header. Descriptor.
size_t desc::block_size |
Size of each element in bytes.
Definition at line 40 of file malloc.c.
Referenced by arena_to_block(), block_size(), block_to_arena(), free(), malloc(), and malloc_init().
size_t desc::blocks_per_arena |
Number of blocks in an arena.
Definition at line 41 of file malloc.c.
Referenced by arena_to_block(), free(), malloc(), and malloc_init().
struct list desc::free_list |
List of free blocks.
Definition at line 42 of file malloc.c.
Referenced by free(), malloc(), and malloc_init().
struct lock desc::lock |