Analysis of memory management methods based on scattered Learning Algorithms

Source: Internet
Author: User

When talking about memory allocation, we will immediately think of interfaces for applying for memory, such as malloc () and calloc (). Speaking of the memory allocation algorithm, we will think of distribution algorithms such as Buddy and Slab. Have you ever thought about how to manage the applied memory? What are the management methods? This is what we will discuss in this article. This article introduces two memory management methods: the linked list method and bit method. In section 0, the problem initialization assumes that there is a batch of memory of M, And we divide it into N blocks, then the size of each block is BLOCK_SIZE = M/N, as shown in: in the first section, the linked list method is called the Linked List Method Memory Management, which means that a lump of memory has been divided into many small pieces, and then each small piece is chained in the form of a linked list. For example, we can see that a linked list node named link is added to each small block, which indicates: [cpp] struct list_head {struct list_head * next, * prev;}; note: in the kernel, the linked list is a two-way linked list. However, for simplicity, I will represent it as a single-chain table. So we can use a header node named head to concatenate these small pieces. When we use it, we only need to extract one from the linked list and return it after use. Using linked lists to manage memory has the following advantages: 1. out-of-the-box, out-of-the-box (convenient and fast): that is to say, when we need to use one or several small pieces of memory in this batch of applied memory, we can use it as needed, remove one or more items from the linked list and return them to the Linked List directly after use. 2. management stream and data stream integration and separation (space saving): When we use the linked list method to manage memory, we only need to know the link of each block, in this way, we can use link to manage each block. We say they are integrated. When you want to use a block, you can remove it from the linked list, at this time, we will use this block for data flow operations. We do not care about link. In this perspective, we say they are separated. Regardless of integration and separation, we did not use extra space during operations, but solved the management flow and data flow within each block. Well, this is the memory management of the linked list method. At the end of this section, I still provide the code for the linked list method: [cpp]/*** linked list memory management: memory initialization * addr: indicates the starting address of the block * block_size: indicates the size of each block * block_number: indicates the number of blocks */void MemoryInit (void * addr, unsigned long block_size, unsigned int block_number) {int I = 0; struct list_head * link = (struct list_head *) addr; for (; I <block_number; I ++) {list_add_tail (link, head ); // Add addr + = block_size; link = (struct list_head *) addr;} [cpp]/*** linked list memory management: memory block allocation * /Void * alloc_blkmem (void) {struct list_head * link = NULL; if (! List_empty (head) // The linked list is not empty {link = head-> next; list_del_init (link); // remove from the linked list} return link ;} [cpp]/*** Linked List Method Memory Management: memory block release ** addr: indicates the starting address of the block */void free_blkmem (void * addr) {struct list_head * link = (struct list_head *) addr; list_add_tail (link, head); // return to the linked list}. The second section of the bitwise method is compared with the linked list method, bit method is simpler. The bit concept is very simple, that is, when a block is not used, we make the bit at the corresponding position of the Block 0. When the block is used, the bit location is 1. For example:
Bit-level method is indeed intuitive, which is an advantage. However, compared with the linked list method, bit-level method is inferior to the linked list method in terms of time and space. Let's take a look: in terms of time: the linked list method is an out-of-the-box method, while the bit rule is not, it must traverse the bit table and use it when the bit is 0; in space: the linked list method does not require additional space, while the bit rule takes additional space to store bitmap tables. If there is a block, it must correspond to a bit. When there are many data blocks, this is also an overhead. Although bitwise method is not ideal in terms of time and space, it is intuitive and easy to understand. It is also very common! Well, this is the bit method, because it is very intuitive, so I will finish it in a few simple words. At the end of this section, we still use the old method to give the code of bitwise, even though it is very simple! [Cpp]/** set bitmap to indicate positioning ** src: Indicates bitmap * index: Indicates positioning */void bitmap_set_bit (void * src, unsigned int index) {unsigned short * data_bitmap = (unsigned short *) src; data_bitmap [index] | = 1 <index ;} www.2cto.com [cpp]/** indicates the location of clearing bitmap ** src: Indicates bitmap * index: indicates the location */void bitmap_clear_bit (void * src, unsigned int index) {unsigned short * data_bitmap = (unsigned short *) src; data_bitmap [index] & = ~ (1 <index);} conclusion in Section 3: Think about, write, and draw ......

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.