. h file
/********************** Description ************************** This is the implementation of the Mpool memory pool, he has the following features: * 1. The memory block in the pool is the same size as * 2. Defined by the macro _mp_no_serialize determines whether multi-thread synchronization is required * 3. He uses the Windows heap memory API for memory allocation * 4. He could not replace the CRT with malloc and free* 5. He's not a general-purpose memory pool * 6. For specific application environments (high-frequency applications that release memory, such as network servers), the application environment affects the size of the memory blocks in the pool, and whether multi-threaded synchronization is required * * Write such a memory pool based on these several reasons * 1. Reduce memory fragmentation (allocating equal chunks of memory each time to help reduce memory fragmentation, memory block size also consider memory alignment) * 2. Increased memory allocation efficiency (non-serialized memory allocations can significantly increase efficiency) * 3. Many general-purpose memory pools on the network, I think the general-purpose memory pool should be the operating system itself, the memory management API, the RING3 layer does not make a common memory pool, and may even play a counter-effect. The simplest way to solve this problem is to increase the capacity of physical memory. Only memory pools that are written for a particular application environment are meaningful. */#ifndef mpool_h#define mpool_h#include <Windows.h> #include <vector>using std::vector;class mpool{ Public:mpool (size_t block_size); ~mpool (); void *mpalloc (); void Mpfree (void *p);p rivate:mpool (const mpool&); mpool& operator= (const mpool&);p rivate:rtl_critical_section *m_pool_lock; Vector<void*> m_block_list; size_t M_block_size; size_t M_total_size; size_t M_block_count; size_t M_reserve_count; HANDLE M_heap; DWORD M_heap_oPtion;}; #endif
. cpp Files
#include "mpool.h" #ifdef _mp_no_serialize#define mplock () #define Mpunlock () #else # define Mp_lock () EnterCriticalSection (M_pool_lock), #define Mp_unlock () leavecriticalsection (M_pool_lock); #endifMPool:: Mpool (Size_ T block_size) {m_block_size = block_size; size_t m = m_block_size% 8; if (M! = 0) {m_block_size + = (8-m); } _system_info INFO; memset (&info,0,sizeof (_system_info)); GetSystemInfo (&info); m_heap_option = 0; #ifdef _mp_no_serialize m_heap_option = heap_no_serialize; #else m_pool_lock = new Rtl_critical_sec tion; InitializeCriticalSectionAndSpinCount (m_pool_lock,4000); #endif m_heap = HeapCreate (M_heap_option,info.dwpagesize, 0); M_block_list.reserve (1024);} Mpool::~mpool () {while (!m_block_list.empty ()) {HeapFree (M_heap,m_heap_option,m_block_list.back ()); M_block_list.pop_back (); } #ifndef _mp_no_serialize deletecriticalsection (m_pool_lock); Delete m_pool_lock; #endif}_inline void * MPOol::mpalloc () {void *ret; Mp_lock (); if (!m_block_list.empty ()) {ret = M_block_list.back (); M_block_list.pop_back (); } else {ret = HeapAlloc (m_heap,m_heap_option,m_block_size); } mp_unlock (); return ret; }_inline void Mpool::mpfree (void *p) {if (p) {mp_lock (); M_block_list.push_back (P); Mp_unlock (); }}