Malloc implementation principle, malloc implementation
How is malloc implemented? The answer is: it can be implemented based on the partner system (the partner system has been mentioned in the previous article), or it can be implemented based on the linked list. Zookeeper
- It has a so-called connection to available memory blocks into a long listIdle linked list
- When calling the malloc function, it searches for a memory block that is large enough to meet user requests along the connection table. The memory block is then split into two parts (the size of one part is equal to the size of the user request, and the size of the other part is the remaining bytes ). Next, pass the memory allocated to the user, and return the remaining (if any) to the connection table.
- When the free function is called, it connects the memory block released by the user to the idle chain.
This involves a problem: the idle chain will be cut into many small memory segments. If the user requests a large memory segment, there may be no fragments on the idle link that meet user requirements.
So how can we solve this problem?
Solve external fragments: malloc function request latency, and start to rummaging through the idle chain to check and sort out memory fragments,Merge adjacent small idle blocks into larger memory blocks.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.