C + + Memory pool--C + + Memory pool__c++

Source: Internet
Author: User

This is my translation of the article, from Code Project,

Original Author: DanDanger2000.

Original link: http://www.codeproject.com/cpp/MemoryPool.asp

C + + memory pool

L Download Sample Engineering –105kb

L Download Source code –17.3KB

DirectoryLIntroductionLHow it worksLSampleLUse this CodeLBenefitsLAbout CodeLToDoLHistory   IntroductionThe memory allocations (via malloc or new) of C/C + + may take a lot of time. Worse, as time goes by, memory (memory) will become fragmented, so an application will run more slowly when it runs for a long time and/or performs a lot of memory allocation (release) operations. In particular, you often apply a small chunk of memory, and the heap (heap) becomes fragmented. Solution: One (possible) solution for your own memory pool is the memory pool (Memory pool). At boot time, a "Memory pool" (Memory pool) allocates a large chunk of memory, and the block is divided into smaller blocks (smaller chunks). Each time you request a memory space from a memory pool, it is obtained from the previously allocated block (chunks) rather than from the operating system. The biggest advantage is: l very few (few) heap fragments l more than the usual memory request/release (for example, through malloc, new, etc.) quickly in addition, you can get the following benefits: L Check whether any one pointer is in the memory pool L write a "heap dump" (heap-dump) "to your hard drive (useful for debugging afterwards) L Some kind of" memory leak Detection (Memory-leak detection) ": When you do not release all previously allocated memory, the memory pool (memory pool) throws an assertion (assertion).How it worksLet's take a look at the UML pattern diagram of the memory pool (Memory pool): This pattern diagram shows only a small portion of the class Cmemorypool, see the documentation generated by Doxygen for a detailed class description.A word about a memory block (memorychunks) you should see from the pattern diagram that the pool of memory (Memory pool) manages a pointer to a struct smemorychunk (M_ptrfirstchunk, M_ptrlastchunk, and M_ptrcursorchunk) pointer. These blocks (chunks) Create a list of memory blocks (memory chunks). Each point to the next block in the list (chunk). When allocated to a piece of memory from the operating system, it is completely managed by Smemorychunk S. Let's take a closer look at a block (chunk).

typedef struct SMEMORYCHUNK
{
TBYTe *data; The actual Data
std::size_t datasize; Size of the "Data"-block
std::size_t usedsize; Actual used Size
BOOL Isallocationchunk; True, when this memorychunks
Points to a "Data"-block
which can be deallocated via ' free () '
Smemorychunk *next; Pointer to the Next memorychunk
In the List (May is NULL)

} Smemorychunk;
Each block (chunk) holds a pointer to: L A small chunk of memory (Data), l The total size (datasize) of the available memory starting from the block (chunk), the actual size (usedsize), L, and a point in the list The pointer to the next block (chunk). Step one: Pre-Request memory (pre-allocating the memory) when you invoke the Cmemorypool constructor, the memory pool (memory pool) will request its first (large) chunk of memory (Memory-chunk) from the operating system/*co Nstructor
******************/
Cmemorypool::cmemorypool (const std::size_t & Sinitialmemorypoolsize,
Const std::size_t & Smemorychunksize,
Const std::size_t & Sminimalmemorysizetoallocate,
BOOL Bsetmemorydata)
{
M_ptrfirstchunk = NULL;
M_ptrlastchunk =

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.