Uclibc is the most used c lib library in embedded Linux system. Recently, I have been learning about the memory management of the Linux kernel. After I finished learning, I have been confused about the memory management of the user space. I have looked at the malloc part of the code in libc for a bit of research. Special summary into this article. The structure of this document: First, the preparation of knowledge 2: heap space management structure three: heap space initialization four: FA structure operation five: malloc implementation six: free implementation seven: summary one: preparation knowledge: user space, often It calls malloc and free for memory management, which allocates memory in the heap space of the process. As shown below:
Second: Management structure of heap space: data structure represented by heap space struct heap{//Representation structure of heap space free area struct heap_free_area *free_areas;//Used to lock protection in a multi-threaded environment #ifdef HEAP_USE_LOCKINGpthread_mutex_t lock; #endif}; heap_free_area structure in the code is called FA structure by the code author. Its definition is as follows: struct heap_free_area{//size of the free area size_t size; //used to construct the circular list struct heap_free_area *next, *prev;}; Note: in the FA structure does not define a pointer to the free area, this Because the FA structure is just behind the free area.
Three: heap space initialization: The global variable __malloc_heap represents the entire stack space. Its initialization process shown as follows: struct heap __malloc_heap = HEAP_INIT_WITH_FA (initial_fa); HEAP_INIT_WITH_FA defined as follows: #ifdef HEAP_USE_LOCKING # define HEAP_INIT {0, PTHREAD_MUTEX_INITIALIZER} # define HEAP_INIT_WITH_FA (fa) {& fa._fa, PTHREAD_MUTEX_INITIALIZER} # else # define HEAP_INIT {0} # define HEAP_INIT_WITH_FA (fa) {& fa._fa} #endif initial_fa variables initialized as follows: HEAP_DECLARE_STATIC_FREE_AREA (initial_fa, 256); # define HEAP_DECLARE_STATIC_FREE_AREA (name, size) \\ static struct \\ {\\ char space [( size) - sizeof (struct heap_free_area)]; \\ struct heap_free_area _fa; \\} name = {" ", {(size), 0, 0}} thus it can be seen heap initialization is 256 bytes. As you can see from the above initialization, an array space is statically defined in front of the FA structure. This also confirms the above said "FA structure just placed behind the free area" four: FA structure operation: 1: #define HEAP_FREE_AREA_SIZE (fa) ((fa)-> size) FA represents the free area Size, this size already contains the space occupied by the FA itself: #define HEAP_FREE_AREA_START(fa) ((void *)((char *)(fa + 1) - (fa)->size)) The free area represented by the FA starting point. Fa+1 reaches the end of the free area. The pointer then moves down the size unit, which is the starting position of the free area. 3: #define HEAP_FREE_AREA_END(fa) ((void *)(fa + 1)) Returns the end of the free area indicated by FA. It can be seen from the structure indicated by FA. Moves the size of a FA from the address of the FA, just to the end of the idle 4: #define HEAP_MIN_FREE_AREA_SIZE \\HEAP_ADJUST_SIZE (sizeof (struct heap_free_area) + 32) The minimum free area size. The free area must be able to store at least one heap_free_area size, in addition to 32 bytes of idle, which is considered from the efficiency. When Malloc allocates memory, sometimes it divides a FA into two segments. If the remaining memory is less than HEAP_MIN_FREE_AREA_SIZE, it is better to allocate the memory. Because maintaining free memory needs to affect efficiency. This can be seen from the malloc and free analysis behind us 5: __heap_delete (struct heap *heap, struct heap_free_area *fa) This function removes fa from the heap, its implementation is as follows: extern inline void__heap_delete (struct heap *heap , struct heap_free_area *fa){if (fa->next)fa->next->prev = fa->prev;if (fa->prev)fa->prev->next = fa ->next;elseheap->free_areas = fa->next; //If the deletion is its first node} is just a simple linked list operation, disconnecting fa from the circular list. 6: __heap_link_free_area (struct heap * heap, struct heap_free_area * fa, struct heap_free_area * prev, struct heap_free_area * next) code annotations from the point of view of the function refers to the prev and next between Fa merged together. In fact it just inserts an item between prev and next. To achieve the following: extern inline void__heap_link_free_area (struct heap * heap, struct heap_free_area * fa, struct heap_free_area * prev, struct heap_free_area * next) {fa- & gt; next = next; fa- & gt; prev = prev; if (prev) prev- & gt; next = fa; elseheap- & gt; free_areas = fa; //inserted node as the ingress node of the case if (next) next- & gt; prev = fa;} 7: __ heap_link_free_area_after (struct heap * heap, struct heap_free_area * fa, struct Heap_free_area *prev)
The use of X capacitors and Y capacitors, and their differences: 1. DC blocking 2. Bypass 3. Coupl
One serial port control Usually we use direct serial port for video server, video recorder, switcher
Computer Store News: first Linux systems for flat panel launch For those who have tired of the N
process represents a running instance of a program that is the smallest unit of allocated resources.
Install FTP under CentOS 6.2 32-bit
Teach you 5 steps to restore the password of Ubuntu system
How to fix GRUB boot in Linux system
Linux System Network Connection FAQ Diagnostics
Linux Standard Input/Output Tutorial
Method of installing Linux (CentOS) system through the network
Two ways to install SUSE Linux10 on a hard disk
Linux using fdisk tool SSH command line partition and mount using partition tutorial
Practical Tips: Several Ways to Remove Grub from Linux Systems
Simple operation Ten "fast" in Linux system
Win9 start menu and its Modern application exposure
Is the configuration of this computer very pit at the time? !
Windows 10 open the "hidden user installation" function tutorial
Five great tricks to add security to Vista and Windows 7
Win7 system to copy the Google Chrome extension to the 360 browser settings
CF very lucky event address CF very lucky event blue and white porcelain weapons free collar
How to optimize the SSD Toolbox using the SSD Toolbox
Computer failure, seeking a master.
Win8 upgrade Win8.1 prompt 0xC1900101-0x40017 error solution