Recently in learning C + + program performance optimization, read to the Memory Pool section. Write one by yourself, a small test should be no problem.
Memory block Memoryblock declaration file
MemoryBlock.h
#pragma once
#define USHORT unsigned short
#define ULONG unsigned long
#include <iostream>
using namespace Std;
Memory block
struct Memoryblock
{
USHORT m_nsize;//Total Allocated memory size
USHORT m_nfree;//number of allocated memory units
USHORT m_nfirst;//The first available memory unit location
memoryblock* m_pnext;//points to the next memory block
Char m_data[1];
void* operator new (size_t,const ushort& sum,const ushort&)
{
Return:: operator new (sizeof (Memoryblock) +sum*unit_size);/Request a memory block space
}
void operator Delete (void* del,size_t)
{
:: operator delete (DEL);//delete memory block space
}
Memoryblock (const ushort& sum,const ushort& unit_size)
: M_nsize (Sum*unit_size), M_nfree (sum-1), M_nfirst (1), M_pnext (0)
{
char* Pdata=m_data;
for (int i=1;i<sum;i++)//initialization 1 to Sum-1 point
{
*reinterpret_cast<ushort*> (PData) =i;
Pdata+=unit_size;
}
}
~memoryblock () {}
};