Memory pool is one of the pools we often use. The common pools are process pool, thread pool and connection pool. Today we will discuss memory pool first. Let's first look at the definition of pools.
Pool: A pool is a pool in which resources are applied for at the beginning, which is much larger than the resources used at the beginning. When used next, resources are obtained directly from the pool.
Memory pool: Apply for a certain amount of allocation at the beginning. The memory blocks of the same size are reserved for backup, and then allocated directly from the memory if necessary. Compared with the traditional new and melloc application space, this mechanism reduces the phenomenon of frequent application space from the kernel to generate a large number of memory space fragments and thus reduce performance.
The core of memory pool implementation consists of three steps: constructing memory pool nodes, applying for memory pool allocation, releasing memory and returning it to memory pool.
Next, we first understand the structure of a simple memory pool, which is actually an array managed in the form of a linked list, effectively avoiding the deletion of arrays and the movement of data during insertion.
#include<iostream> const int MEM_SIZE = 10; template<typename T> class MEM_Pool { public: static MEM_Pool<T>* getInstance() { if (mmp == NULL) { mmp = new MEM_Pool<T>(); } return mmp; } //Application space void* alloc(size_t size) { if (pool == NULL) //Memory pool is empty (no spare storage unit) { pool = (Node*)new char[(size + 4)*MEM_SIZE]();//Size is the size of the object, and 4 is the size of the pnext pointer field. Node* pCur = pool; //Domain NULL with the last pointer of the memory pool for (pCur; pCur < pool + MEM_SIZE - 1; pCur = pCur + 1) { pCur->pnext = pCur + 1; } pCur->pnext = NULL; } void* rt = pool; //Allocation space pool = pool->pnext;//Remove allocated space from the free list return rt; } //Release space and add space to the memory pool void dealloc(void* ptr) { if (ptr == NULL) { return; } Node* pptr = (Node*)ptr; //Temporary Save Address pptr->pnext = pool;//Join the memory pool pool = pptr;//Modify the pointer to the memory pool } private: MEM_Pool() { pool = NULL; } MEM_Pool(const MEM_Pool<int>&); class Node //Individual nodes of memory pool { public: Node(T val = T()) :mdata(val), pnext(NULL){} //Node constructor public: T mdata; Node* pnext; }; Node* pool; static MEM_Pool<T>* mmp; // Instantiate an object }; template<typename T> MEM_Pool<T>* MEM_Pool<T>::mmp = NULL; class CGoods { public: CGoods(std::string name, float price, int amount) :mname(name), mprice(price), mamount(amount) {} void* operator new(size_t size) { return pmm->alloc(size); } void operator delete(void* ptr) { pmm->dealloc(ptr); } private: std::string mname; float mprice; int mamount; static MEM_Pool<CGoods>* pmm; }; MEM_Pool<CGoods>* CGoods::pmm = MEM_Pool<CGoods>::getInstance(); int main() { CGoods* pgood1 = new CGoods("Bread", 4.5, 100); CGoods* pgood2 = new CGoods("Mineral water", 2.0,100); delete pgood1; return 0; }