Inner master-Memory Manager

Source: Internet
Author: User

Reprinted please indicate the source and the author contact: http://blog.csdn.net/absurd

Contact information of the author: Li xianjing <xianjimli at Hotmail dot com>

Last Updated:

 

As a CProgramEvery day, he is dealing with malloc/free/calloc/realloc series functions. Maybe they are too familiar with each other. Instead, they ignore their existence. Even with their friendship in year 35, they still have no idea about their implementation. On the contrary, some curious new users are interested in their implementations. At the beginning, it was a question from a new colleague that prompted me to study memory management.Algorithm.

 

Memory Management Algorithms are somewhat mysterious. We seldom want to implement our own memory management algorithms. It is no wonder that there are not many such demands. In fact, the implementation of the memory allocation algorithm is simple and complex. It may take a few weeks or even months to write a simple one, maybe half a day to get it done.

 

Malloc and free are two core functions, and calloc and realloc exist to improve efficiency. Otherwise, you can use a combination of malloc and free to simulate them.

 

Taking the implementation of the calloc function as an example, on a 32-bit machine, the memory manager ensures that the memory is at least 4 bytes aligned and its length can be extended to four bytes divisible, then the zeroth algorithm can be optimized. Four bytes can be cleared at a time, which greatly improves the clearing speed.

 

Taking the implementation of the realloc function as an example, if there is enough space behind the realloc pointer, the memory manager can directly expand its size without copying the original content. Of course, the new version is smaller than the old version, and the new version is not copied. On the contrary, when we use malloc and free to implement realloc, We need to copy the files in both cases, and the efficiency will naturally be much lower.

 

There are also two non-standard functions, but very common functions, also involve memory allocation: strdup and strndup. Both functions are supported in Linux and Win32, which is very convenient. This can be simulated using malloc without performance loss.

 

Here we mainly focus on the implementation of malloc and free functions, and take glibc 2.3.5 (32-bit Linux) as an example.

 

Target of the Memory Manager

Why is the memory manager hard to write? What factors should I consider when designing memory management algorithms? Memory Management is a functional requirement of the Memory Manager. Like designing other software, quality requirements are equally important. Before analyzing memory management algorithms, Let's first look at the quality requirements for memory management algorithms:

 

L maximum compatibility

To implement the Memory Manager, you must first define the interface function of the distributor. Interface functions do not need to be unconventional, but follow existing standards (such as POSIX or Win32), so that users can smoothly over to the new memory manager.

 

L maximum portability

Generally, the memory manager needs to apply for memory from the OS and then perform secondary allocation. Therefore, you need to expand the memory or Release excess memory when appropriate, which requires calling the functions provided by the OS. The functions provided by the operating system are platform-specific and abstract platform-related functions as much as possible.CodeTo ensure the portability of the Memory Manager.

 

L minimum waste of space

To manage the memory, the memory manager must use some of its own data structures, which also occupy the memory space. In the user's eyes, these memory spaces are undoubtedly wasted. It is obviously unacceptable if too much memory is wasted in the memory manager.

 

Memory fragments are also the culprit of a waste of space. If the memory manager has a large number of memory fragments, they are non-consecutive small pieces of memory. The total amount of memory may be large, but they cannot be used, this is unacceptable.

 

L the fastest speed

Memory Allocation/release is a common operation. According to the 2/8 principle, common operations are performance hotspots. The performance of hotspot functions is particularly important to the overall performance of the system.

 

L maximize totonality (to adapt to different situations)

The difficulty in designing Memory Management Algorithms lies in adapting to different situations. In fact, in the absence of application context, it is impossible to evaluate the quality of memory management algorithms. It can be said that under any circumstances, dedicated algorithms are superior to common algorithms in terms of time/space performance.

 

It is obviously inappropriate to write a set of Memory Management Algorithms for each scenario. We don't need to pursue the optimal algorithm, so the cost is too high and we can achieve sub-optimal performance. Design a general memory management algorithm and configure it with some parameters to make it outstanding in specific situations. This is tonality.

 

L locality)

We all know that using cache can increase the speed, but many may not know the real reason why the cache increases the speed of the program. The access speed of the cache inside the CPU may be an order of magnitude different from that of RAM. The speed difference between the two is important, but this is not a sufficient condition for improving the speed, but a necessary condition.

 

Another condition is the locality of the program's access to memory ). In most cases, the program accesses a memory near the memory, and adds the memory nearby to the cache first. The next time you access the data in the cache, the speed will increase. Otherwise, if the program accesses this area for a while and accesses another piece of memory 108,000 miles apart, this will only make the data move back and forth between the memory and the cache, not only to improve the speed, it will greatly reduce the speed of the program.

 

Therefore, the memory management algorithm should consider this factor to reduce cache miss and page fault.

 

L maximize debugging

As a C/C ++ programmer, memory errors can be said to be our nightmare. The last memory error must be remembered. The debugging function provided by the Memory Manager is powerful and easy to use. Especially for Embedded environments, the memory error detection tool is lacking, and the debugging function provided by the Memory Manager is even more indispensable.

 

L maximize Adaptability

As mentioned above, the maximum tonality can be applied to the memory manager in different situations. However, setting settings in different situations is undoubtedly too troublesome and non-user-friendly. Make the Memory Manager suitable for a wide range of situations, and tune the settings only in rare cases.

 

Design is a multi-objective optimization process, and competition exists between some objectives. Balancing these competencies is one of the difficulties in design. In different situations, these goals are of different importance, so there is no best memory allocation algorithm.

 

We plan to perform code-level analysis on the memory distributor of glibc. We will only talk about some interesting things:

1. glibc allocation algorithm Overview:

L smaller than or equal to 64 bytes: allocated using the pool algorithm.

L 64-512 bytes: it is suitable for optimal allocation by matching algorithm and pool algorithm allocation.

L greater than or equal to 512 bytes: allocated using the best matching algorithm.

L greater than or equal to 128 K: directly call the function provided by the OS (such as MMAP) for allocation.

 

2. glibc memory expansion method:

 

L int BRK (void * end_data_segment );

This function is used to expand the heap space. For the definition of heap space, see the memory model chapter. Use end_data_segment to specify the heap end address.

L void * sbrk (ptrdiff_t increment );

This function is used to expand the heap space (refer to the memory model chapter for the definition of the heap space). Use increment to specify the size to be increased.

L void * MMAP (void * Start, size_t length, int Prot, int flags, int FD, off_t offset );

This function is used to allocate a large amount of memory, as described in the previous section.

 

3. Empty pointer and zero-length memory

L will free (null) make the program crash? The answer is no. Standard C requires free to accept null pointers and then do nothing.

L will malloc (0) be allocated successfully? The answer is yes. It will return a minimum memory for you.

 

4. Alignment and rounding

L The Memory Manager will ensure that the allocated memory address is aligned, usually 4 or 8 bytes aligned.

L The Memory Manager will adjust the memory length so that the memory length can be divisible by 4 or 8.

5. allocated memory structure

 

If there is a valid memory block in front of it, the first size_t indicates the size of the previous memory block.

The second size_t indicates your own size, and also indicates whether you use MMAP to allocate (M), and whether there is an effective memory block (p) in front ). You may find it strange that on a 32-bit server, sizeof (size_t) is a 32-bit server. How can we leave two digits to save the mark? As we mentioned above, the memory length will be rounded up to ensure that the minimum value of 2 or 3 bits is 0, that is, idle.

 

6. Idle Memory Management

 

It can be seen that the minimum memory block length is 16 bytes:

Sizeof (size_t) +

Sizeof (size_t) +

Sizeof (void *) +

Sizeof (void *) +

0

This trick is very useful. The first time I saw it, I felt so clever. This eliminates the need for additional memory to manage idle blocks. You can use the idle block itself to forcibly convert the idle block into a two-way linked list.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zdl1016/archive/2009/10/21/4708902.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.