2. Outline
• Why Use DLmalloc
• Basic structures
– Chunk & Bins
• Key Algorithms
– Memory allocation
• Small request
• Large request
– Realloc
– Free
– Trim
3. Why Use DLmalloc
• If don’t use DLmalloc
– Application should use system call brk or sbrk to
get memory from Kernel.
– Disadvantage:
• Will trap into the kernel
• Lost cache locality
• Key role of DLmalloc
– Maintain the free bulks for next use without
system call.
4. Chunk & bins
• All available space are organized by chunks in
DLmalloc.
– Small chunk: Chunk size [16,248]
– Large chunk: Chunk size [256, 2^31]
• All available chunks are maintained in bins,
grouped by size.
– Small chunk corresponds to smallbins
– Large chunk corresponds to largebins
5. Small Chunk
Allocated
prev size
Current size| status
Free
Front pointer
Back pointer
Data
prev size
Current size| status
Data
Allocated
struct malloc_chunk {
size_t prev_foot; /* Size of previous chunk (if free). */
size_t head; /* Size and inuse bits. */
struct malloc_chunk* fd; /* double links -- used only if free. */
struct malloc_chunk* bk;
}; #define SIZE_T_ONE ((size_t)1)
#define SIZE_T_TWO ((size_t)2)
#define PINUSE_BIT (SIZE_T_ONE)
#define CINUSE_BIT (SIZE_T_TWO)
7. Large Chunk
Allocated
prev size
Current size| status
Free
Front pointer
Back pointer
Unused Data
prev size
Current size| status
Data
Allocated
Child 0
Child 1
parent
struct malloc_tree_chunk {
/* The first four fields must be compatible with malloc_chunk */
size_t prev_foot;
size_t head;
struct malloc_tree_chunk* fd;
struct malloc_tree_chunk* bk;
struct malloc_tree_chunk* child[2];
struct malloc_tree_chunk* parent;
bindex_t index;
};
8. Large bins
The tree is named Tries.
[768,1024)[512,768)[384,512)[256,384) ........
node
node
chunk chunk
43210largemap ...
largebins
...
node
chunk chunk
10. Optimization
• Designed Victim
– This is the preferred chunk for servicing small
requests that don't have exact fits. It is normally
the chunk split off most recently to service
another small request. Its size is cached in dvsize.
The link fields of this chunk are not maintained
since it is not kept in a bin.
11. Small Request Algorithm
1. If one exists, use a remainderless chunk in
associated smallbin.
2. If it is big enough, use the dv chunk.
3. If big enough and bigger chunk exists, split
the smallest available chunk in a bin(small
bin or larger bin), saving remainder in dv.
4. If it is big enough, use the top chunk.
5. If available, get memory from system and use
it
12. Get memory from system
1. If Top is empty and request bigger than
256KB
– Use mmap(). mmaped chunk’s head doesn’t use
status bit.(art’s case doesn’t use mmap())
2. Use brk to allocated page-align of request.
3. If brk fails, use mmap().(mmap is enabled)
4. Give back old top to chunk bins and use new
allocated space as top.
13. Large Request
1. Find the smallest available binned chunk that
fits, and use it. if it is better fitting than dv
chunk, splitting if necessary.
2. If better fitting than any binned chunk, use
the dv chunk.
3. If it is big enough, use the top chunk.
4. If available, get memory from system(brk or
mmap) and use it
14. realloc Pseudo-code
• void* dlrealloc(void* oldmem, size_t bytes)
– If oldmem = 0, call dlmalloc(bytes).
– If bytes = 0, call dlfree(oldmem).
– If bytes < oldsize and gap >16, split off remainder
– If bytes > oldsize
• If the adjacent chunk is free and big enough, extend it.
• Else
» call dlmalloc(bytes)
» memcpy it to new space.
» Give oldspace back to chunklist.
15. Free
• If free chunk is mapped(by mmap()), just
munmap it.
• Else try to merge previous and next free
chunk , insert into small/large chunk bins
except for Top and DV.
16. Trim
• If Top size is larger than 2MB, will call sys_trim.
– unmap or sbrk top space to 1 Page
– unmap all free mmapped segments