Implementation of the memory pool

Source: Internet
Author: User

turn from: http://www.cnblogs.com/bangerlee/archive/2011/08/31/2161421.html

Introduction

C + + memory management is a problem for nearly every programmer, allocating enough memory, tracking the allocation of memory, freeing up memory when it is not needed-a task that is quite complex. and direct use of system call Malloc/free, New/delete memory allocation and release, there are the following drawbacks: Call Malloc/new, the system needs to be based on "first match", "best Match" or other algorithms in the memory free block table to find a piece of free memory, call the free/ Delete, the system may need to merge free memory blocks, which can incur additional overhead
Frequent use of a large amount of memory fragmentation, which reduces the efficiency of the program can easily lead to memory leaks


Memory pool (memory pool) is in lieu of direct call Malloc/free, New/delete for memory management of the common method, when we apply for memory space, first to our memory pool to find the appropriate block of memory, rather than directly to the operating system to apply, the advantage is: Faster or less heap fragmentation to avoid memory leaks than Malloc/free for memory requests/releases


Memory Pool Design

See the memory pool benefits so much, is not hate can not immediately abandon Malloc/free, the embrace of the memory pool. Wait a minute, before we do our own implementation of the memory pool, we need to be clear about the following issues: How to get the memory pool space. Whether to allocate a large chunk of space when the program is started or to allocate it on demand in the running of the program. Memory pool for incoming memory request, there is no size limit. If so, what is the maximum size of the memory block that can be applied?
How to design the structure of memory block to facilitate the application, tracking and release of memory.
The more space the memory pool occupies, the less memory the other program can use, and whether to set the upper limit of the memory pool space. How appropriate to set it.

With the above questions, let's look at one of the following memory pool design scenarios.


Memory Pool Implementation solution I

Download the source code for this memory pool from here.

First, the overall structure of the scheme is given, as follows:

Figure 1. Memory Pool architecture Diagram

The structure consists of block, list and pool three structures, which contain pointers to the actual memory space, and forward and back pointers allow block to form a two-way list; the free pointer in the list structure points to a list of spare memory blocks, The used pointer points to a list of memory blocks that the program uses, the size value is the memory block, the list is composed of one-way lists, and the pool structure records the head and tail of list lists.


Memory Tracking Policy

In this scenario, when allocating memory, more than 12 bytes will be requested, that is, the actual requested memory size is +12 of the required memory size. In the 12 bytes of multiple requests, the corresponding list pointer (4 bytes), The used pointer (4 bytes), and the checksum Code (4 bytes) are stored respectively. By setting this up, it is easy to get the block and list where the memory is located, and the check code can be used to make a cursory inspection of the error. The structure is illustrated as follows:

Fig. 2. Memory block application diagram

The position indicated by the arrow in the figure is where the memory block really starts.


memory request and release policies

Application: According to the size of the requested memory, traverse the list of lists to see if there is a matching size;

There is a matching size: null when viewing free

Free null: Use Malloc/new to request memory and place it at the end of the linked list used

Free is NOT NULL: Removes the head node of the linked list, and places the end of the linked list in the used

No matching size: Create a new list, use Malloc/new to request memory, and place it at the end of the list's used linked list

Returns the memory space pointer

Release: Retrieves the list and used pointers from the linked list indicated by the used pointer according to the memory trace policy, and places them in the linked list that the free pointer points to


analysis of the programme one

In contrast to the issues raised in the "Memory Pool Design" section, our proposal has the following characteristics: After the program starts the memory pool does not have the memory block, when the program really carries on the memory request and the release time only then takes over the memory block management; The memory pool is not limited to the size of the application, It creates a linked list for each size value for memory management; This scenario does not provide the ability to limit the size of the memory pool


Combining analysis, it can be concluded that the application scenario of the program is as follows: the size of the memory block requested by the application is relatively fixed (for example, only apply/release 1024bytes or 2048bytes of memory), the frequency of application and release is basically consistent (due to the number of applications to release a small amount of memory,


This article explains the basic knowledge of memory management, with a simple memory pool implementation example as a stepping stone, leading everyone to know the memory pool, the next one for the memory pool advanced article on the Apache server memory pool implementation methods.


Reference: "Memory pool Design Research and application" by Freeeyes

-----------------------------------------------------------------------------

implementation of the memory pool (ii)

In the memory pool implementation (i), this paper introduces the reasons for using the memory pool, designs the problems that should be considered in the memory pool, and finally gives a simple memory pool implementation example. Using the memory pool implementation described in the previous article, under certain constraints, let's look at a more general memory pool implementation of the--apache server's memory pool.


The Apache server developer collates the portable parts of the code into the Apache Portable runtime (apacheportable run-timelibraries), or APR, which can be downloaded from here, This contains the implementation code for the memory pool to be covered here. The Apache server memory pool is referred to as the APR memory pool below.


APR memory Pool structure 1. Memory allocation node

Before understanding the entire memory pool architecture, let's take a look at the most basic unit in the APR memory pool-The memory allocation node. Memory allocation nodes are used to describe each allocated block of memory, the corresponding structure named apr_memnode_t, defined in the file Apr_allocator.h, which is defined as follows:

/* Basic memory nodestructure*/struct  apr_memnode_t {apr_memnode_t*next;     & nbsp;    /**< Next Memnode * *      apr_memnode_t**ref;           /**< reference to self */     apr_uint32_t  index;             /**< Size */      apr_uint32_t  free_index;      /**< how much free * *   & nbsp;  char          *first_avail;          /**< Pointer to the memory */     char   & nbsp;      *endp;                 /**< Pointer to end of the free memory */};

Even though each field in the structure is commented on in the source file, it is difficult to understand the purpose of each field. Here we give you a brief explanation of each field: Next: A pointer to the next node; Ref: To the next node on the previous node, next to the last node points to this node, so **ref is the node itself; Index: both indicates the size of the node, At the same time, it indicates the index subscript value of the linked list of the node; Free_index: The unused space in the memory block described (here and above the index and their literal meaning, to be aware of); First_avail: A pointer to the starting position of the available space; ENDP: A pointer to the end position of the free space.

The schematic diagram of the node is as follows:

2. Memory Allocator

In the ARP memory pool, the memory allocation node is managed using a memory allocator, which is defined in APR_POOLS.C as follows:

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.