1.dynamic memory allocators:应用程序运行期间申请的内存(malloc)。
2.堆:dynamic memory allocator管理的进程内存区域
3.types of allocator:隐式分配器(new and garbage collection)和显示分配器(malloc & free)
1.malloc、free、realloc
2.16-byte (x86-64) alignment on 64-bit systems
3.内部碎片产生的原因:
4.外部碎片:Occurs when there is enough aggregate heap memory, but no single free block is large enough。(大块被划分成小块,虽然整个空闲内存能够放下程序,但是找不到单独的一块去存放)
5.keep track of free blocks:显式、隐式链表、Segregated free list(Different free lists for different size classes)、Blocks sorted by size(红黑树,长度为key)
1.block structures
typedef struct block
{
word_t header;//包含1bit的是否分配、剩下的表示payload大小
unsigned char payload[0];
} block_t;
2.Header access
return header & 0x1;
return header & ~0xfL;
block->header = size | alloc;
3.Traversing list
static block_t *find_next(block_t *block)
{
return (block_t *) ((unsigned char *) block
+ get_size(block));
}
static block_t *find_fit(size_t asize)
{
block_t *block;
for (block = heap_start; block != heap_end;
block = find_next(block)) {
{
if (!(get_alloc(block))
&& (asize <= get_size(block)))
return block;
}
return NULL; // No fit found
}
5.Allocating in Free Block and Splitting Free Block
6.Freeing a Block and Coalescing and Bidirectional Coalescing
1.Unordered(插入时间复杂度O(1))
1.按照不同的大小划分不同的List
2.To allocate a block of size n:
3.If no block is found:
1.How does the memory manager know when memory can be freed?
2.Must make certain assumptions about pointers
3.采用有向图标记:堆中分配的block看成节点,边看成指针。利用dfs算法,可以标记现在或者未来依然会使用的堆中的block。
看了几个博客,用隐式链表的话能得个50分左右,采用显式空闲链表+LIFO或者显式空闲链表+地址排序能得个80分左右,采用Segregated List得分较高,有100分左右。