Introduction
Memory allocation in C or C++ language can take lots of time, especially in some efficient software product. If a application always allocate memory, its performance decreases greatly when it is running over a long time. The heap becomes fragments.
the solution to improve the efficiency of memory allocation is using memory pool. It will allocate a big amount of memory on application startup and it will be separate into smaller memory buffer uints. Every time you request memory from the pool, it is taken from previously allocated memory buffer uints, and not from OS. The biggest advantages are
1. Faster than normal memory allocation like malloc/new operator in C,C++ language.
2. very litter heap-fragmention
On application startup, you will allocate a big amount of memory. The big memory block is made up of many buffer units. If the size of buffer that you allocate is smaller than the buffer unit size, the buffer pool will return you the free buffer unit point. One buffer unit is made up of the size of the buffer unit and the buffer memory that you can get.
Using the code
<p style="TEXT-ALIGN: left" align="left"> size_t initSize = 1024;
</p>
<p style="TEXT-ALIGN: left" align="left">
</p>
<p style="TEXT-ALIGN: left" align="left"> BufferPool bfA(initSize,20);
</p>
<p style="TEXT-ALIGN: left" align="left">
</p>
<p style="TEXT-ALIGN: left" align="left"> char *p = bfA.Allocate(100);
</p>
<p style="TEXT-ALIGN: left" align="left"> if(p)
</p>
<p style="TEXT-ALIGN: left" align="left"> {
</p>
<p style="TEXT-ALIGN: left" align="left">
</p>
<p style="TEXT-ALIGN: left" align="left">
</p>
<p style="TEXT-ALIGN: left" align="left">
</p>
<p style="TEXT-ALIGN: left" align="left"> bfA.Release(p);
}
</p>