Understanding Malloc() Internals: What I've Learnt So Far
Intro
I've been learning C language, and used malloc() multiple times as an inbuilt function. I was curious about what work it actually did under the hood and how did it actually allocate memory. So I did some research, and watched a couple of videos on youtube.. this was one of the videos that was very useful.
https://youtu.be/HPDBOhiKaD8?si=T4HeEA3ACE3macqO
This post documents everything that I have learnt so far. This is a work-in-progress, i will continue updating this post as i get more knowledge about this topic.
What is Malloc()
malloc() is a C function that is used to allocate memory for a program.
You can access the system heap using the inbuilt C function: malloc().
It tries to give your program a chunk of memory that is large enough to hold size bits.
Concepts
Memory is the address space that is in the RAM or swap.
Accessing Heaps in C
Heaps can be accessed in C using
sbrk()-> main heapnmap()-> additional heap
Note:
- when you request a memory block of 100 bytes, malloc recieves memory slightly above 100 bytes, due the the extra headers stored
- when memory is freed, it does not go back to OS directly -- it is stored in bins
Why brk() and mmap() aren't used directly in the program instead of malloc()
What are they?
brk() --> low level system call in Linux systems that changes size of heap by moving program break(the boundary marking end of process data seg)
mmap() --> system call in Linux that creates memory by mapping files or devices directly to processes address space.
This allows you to access file contents as if it were regular memory.
Why not use them directly?
brk()limitation -- only creates extra space at the end of the heap. if you free a large block in the middle, then it remains locked until everything is freed.mmap()limitation -- limited number of mappings (kernel limits number of seperate mmap() regions that you can have.)
Malloc's advantage: Malloc is an inbuilt function within C++ that intelligently chooses between brk() and mmap() based on allocation size.
- if size req is small --> uses
brk() - if size req is big --> uses mmap()
How malloc works
Heap: fixed lump of mapped memory
malloc() also helps organize and manage heap. It returns the address within the heap that contains the free lump of memory of required size.
But how does malloc know which memory is free??
Method1- DLmalloc()
Algorithm:
for each chunk assigned-- stores a block size of chuck b4 it and keeps 4 bytes empty for how free is implemented
What I'm still learning:
- How the bin system works in detail
- Other malloc implementations
- How to create a basic allocator by myself?
Comments
Post a Comment