Programmer's Self-cultivation Notes: Chapter 10

Chapter 10 Memory
1. Calling Conventions
A calling convention generally stipulates the following aspects:
the order and method of passing function parameters:
the order in which parameters are pushed onto the stack. Some conventions also allow the use of registers to pass parameters.
How to maintain the site:
whether the pop-up of parameters is done by the caller of the function or the function itself;
name modification strategy
; function return value transfer

2. Heap
: The program applies for a heap space of appropriate size from the operating system, and then the program manages this space by itself. This program is often the runtime library of the program.
3. There are two system calls for space allocation under Linux, one is brk() and the other is mmap().
brk() is to set the end address of the data segment (bss+data segment) and move the end address of the data segment to a higher address. Then the expanded part of the space is the heap space. glibc also has a function sbrk(), which is similar to brk, except that sbrk() uses an increment (can be positive or negative), and the return value is the end address of the incremented data segment, which is actually a wrapper for brk().
mmap() is similar to VirtualAlloc() under Windows. It applies for a virtual address space from the operating system and can map this space to a file. When it is not mapped to a file, we call it an anonymous name space.
void* mmap(
void* start,
size_t length,
int prot,
int flags,
int fd,
off_t offset);

glibc has two methods for users' space requests: for requests smaller than 128 KB, it will allocate space for it according to the heap algorithm within the existing space and return it; for requests larger than 128 KB, it will use mmap allocates an anonymous space for him, and then allocates space for the user in this anonymous space.
The maximum application size for heap space depends on many factors. For example, the kernel version is different, and there are system resource limitations, the sum of physical memory and swap space.
The WINDOWS process under WINDOWS allocates address space to various EXE, DLL files, heaps, and stacks. And because of the WINDOWS runtime library, a process has multiple threads and multiple stacks. After the allocation, the Windows process address space is already fragmented. When the program applies for space from the system, it is allocated from the remaining unoccupied addresses. When VirtualAlloc() applies for space, the space size must be an integer multiple of the page.
The algorithm implementation of heap allocation under Windows is located in the heap manager. The push manager provides a set of heap-related APIs to create, allocate, release and destroy heap space:
HeapCreate
HeapAlloc
HeapFree
HeapDestroy

3. Heap allocation algorithm
Free linked list:
At the beginning or end of each free space in the heap, there is a header structure that records the address of the previous or next free space.
The disadvantage is that once the meter is destroyed or the four bytes of the record length are destroyed, the entire heap cannot work properly.
Bitmap:
Divide the entire heap into a large number of blocks. Each block is the same size. When the user requests memory, an integer number of blocks is always allocated to the user. The first block is the header and the remaining blocks are the body. Therefore, only Use an integer array to record the usage of the meeting.
The advantage is that it is fast, stable and easy to manage without requiring additional information. The disadvantage is that fragmentation is easy to occur when allocating memory, and if the heap is large or the set one is small, then Rittal will be very large and the advantage of high cache hit rate may be lost.

Guess you like

Origin blog.csdn.net/weixin_45719581/article/details/123207766
Recommended