Heap Allocators
Explicit allocator = require the application to explicitly free any allocated blocks. For example , the C standard library "malloc" package
Implicit allocators = require the allocator to detect when an allocated block is no longer being used by the program and then free the block. for exmaple , garbage collectors.
1. brk and &end points to same address
2. char* str gets allocated in stack and point to unnamed heap memory
3. brk moves up ,not just 10 bytes , but mutiple of system pagesize. ( reason is in below section )
4. after free function call , memory gets freed but brk points to same address, for later future "malloc" allocation.
5. char* str stays until function that has declared this variable goes out of scope.
if we remove "memset" we can allocate whole lot more memory. more than 4236MB.
because of optimistic memory allocation scheme in linux , allocate more virtual memory than there is physical memory, based on the assumption that a program may not need to use all the memory it asks for
System call brk
resizing th eheap is acutually telling the kernel to adjust the process's program break, which lies initially just above the end(pointer) of the uninitialized data segment.
sets the program break to location specified by end_data_segment. any attempt to lower the program break than end(pointer) results in segementation fault.
sbrk = wrapper for brk()
after first program break moved up to ~7000 and then stays same no matter how many allocation happen.
this is why in above section brk didn't just jump to 10more bytes it jumps further , having more space not just ,in this case 100bytes.
L = length of block
P = pointer to previous free block
N = pointer to next free block
when program calls malloc() , allocator scans the link list of free memory blocks , assigns the block and update the data structures
if no match , them malloc() calls sbrk() to allocate more memory.
as we said above malloc() doesn't allocate exact nubmer of bytes it allocate more , to prevent calling sbrk() often.
free() = we put a chunk of memory back on the free list.
try to merge the free chunks into one free chunk. called coalsecing free chunks
also it doens't lower program breaks
- freed block might be in middle of heap
- to reduce nubmers of sbrk() call
although we only allocated 1byte system loads more bytes(32) as we discussed above. if we allocate 25 it will change.
why 24? because word is 32bit and we need our 8bits to write the length.
'Operating System > System Programming(Arif Butt)' 카테고리의 다른 글
Lec12) UNIX File System Architecture (0) | 2021.06.27 |
---|---|
Lec11) Design and Code of UNIX more utility (0) | 2021.06.25 |
Lec09) Stack Behind the Curtain (0) | 2021.06.22 |
Lec08) Exit Handlers and Resource Limits (0) | 2021.06.18 |
Lec 6, 7) Versioning Systems git-1,2 (0) | 2021.06.15 |