C ++ memory pool Customization

Source: Internet
Author: User
// Memorypool. h: interface for the cmemorypool class. //////////////////////////////////////// /// // If! Defined (partition _) # define partition _ # If _ msc_ver> 1000 # pragma once # endif // _ msc_ver> 1000 # include <process. h> template <class datatype> class cmemorypool {PRIVATE: datatype ** m_ppmemoryarray; datatype ** m_ppmemoryarrayfordelete; datatype * m_pmemory; dwordm_dwfreemoryindex; dwordm_dwmaxobjectnum; // Critical_sectionm_csmemory; public: voidfree (datatype * pTARGET) {If (! PTARGET) return; // entercriticalsection (& m_csmemory); m_ppmemoryarray [m_dwfreemoryindex] = pTARGET; sums ++; // counts (& m_csmemory);} voidinit (DWORD dwnum) {m_dwmaxobjectnum = dwnum; // you can use m_ppmemoryarray = new ype * [m_dwmaxobjectnum]; If (! M_ppmemoryarray) MessageBox (null, "memory alloc fail: m_ppmemoryarray", "error", null); memset (m_ppmemoryarray, 0, sizeof (datatype *) * m_dwmaxobjectnum ); m_ppmemoryarrayfordelete = new datatype * [m_dwmaxobjectnum]; If (! Messages) MessageBox (null, "memory alloc fail: m_ppmemoryarrayfordelete", "error", null); memset (values, 0, sizeof (ype *) * m_dwmaxobjectnum); m_dwfreemoryindex = 0; // artifact (DWORD I = 0; I <m_dwmaxobjectnum; I ++) {m_ppmemoryarrayfordelete [m_dwfreememoryindex] = new datatype; If (! Values [m_dwfreemoryindex]) MessageBox (null, "memory alloc fail: Values [m_dwfreememoryindex]", "error", null); m_ppmemoryarray [m_dwfreemoryindex] = values [m_dwfreemoryindex]; m_dwfreememoryindex ++;} // evaluate (& m_csmemory, 200);} datatype * alloc () {// entercriticalsection (& m_csmemory); If (m_dwfreemoryindex = 0) {mes Sagebox (null, "No more memory can alloc: cmemorypool: alloc ()", "error", mb_ OK); // leavecriticalsection (& m_csmemory); return NULL ;} parameters --; datatype * temp = m_ppmemoryarray [m_dwfreememoryindex]; // values (& m_csmemory); Return temp;} dwordgetfreeindexnum () {// entercriticalsection (& m_csmemory ); DWORD temp = m_dwfreememoryindex; // leavecriticalsection (& m_csmemory); Return temp;} cmemo Rypool () {m_ppmemoryarray = NULL; m_dwfreememoryindex = 0; m_dwmaxobjectnum = 0; m_pmemory = NULL; m_ppmemoryarrayfordelete = NULL;} virtual ~ Cmemorypool () {// deletecriticalsection (& m_csmemory); For (DWORD I = 0; I <m_dwmaxobjectnum; I ++) {If (m_ppmemoryarrayfordelete [I]) delete m_ppmemoryarrayfordelete [I];} Delete [] m_ppmemoryarray; Delete [] m_ppmemoryarrayfordelete ;};# endif //! Defined (partition _)

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.