You're here: Snippet Directory » C/C++ (495)
Language:

Memory Pool container class

Language: English
Programming Language: C++
Published by: malcolmnixon [not registered]
Last Update: 5/15/2006
Views: 130


Description

This class handles a pool of memory for objects to be quickly allocated from. All objects can be freed by destroying the pool.

Code

1 //////////////////////////////////////////////////////////////////////////////// 2 // 3 // Filename: CustomHeap.hpp 4 // 5 // Version: 1.0 6 // 7 // Description: Memory allocation pool class. 8 // 9 // Author: Malcolm Nixon (MalcolmNixon@earthlink.net) 10 // 11 // This program is free software; you can redistribute it and/or modify 12 // it under the terms of the GNU General Public License as published by 13 // the Free Software Foundation; either version 2 of the License, or 14 // (at your option) any later version. 15 // 16 // This program is distributed in the hope that it will be useful, 17 // but WITHOUT ANY WARRANTY; without even the implied warranty of 18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 // GNU General Public License for more details. 20 // 21 // You should have received a copy of the GNU General Public License 22 // along with this program; if not, write to the Free Software 23 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 24 // 25 // A copy of the GNU GPL can be found in gpl.txt or at http://www.gnu.org 26 27 #ifndef __CUSTOMHEAP_HPP 28 #define __CUSTOMHEAP_HPP 29 30 // Standard checks. 31 #ifndef __cplusplus 32 #error __FILE__ requires a C++ compiler. 33 #endif //__cplusplus 34 35 // Heap block used for custom heap storage. 36 struct CustomHeapBlock 37 { 38 CustomHeapBlock* m_pNext; 39 unsigned char* m_pBuffer; 40 unsigned int m_nUsed; 41 unsigned int m_nFree; 42 }; 43 44 // Custom heap container object. 45 class CustomHeap 46 { 47 unsigned int m_nBlkSize; 48 CustomHeapBlock* m_pRoot; 49 50 public: 51 // Constructor / Destructor. 52 CustomHeap(); 53 virtual ~CustomHeap(); 54 55 // Create / Destroy the heap. 56 void CreateHeap(unsigned int nBlkSize = 0x20000); 57 void DestroyHeap(); 58 59 // Allocate from the heap. 60 void* Malloc(unsigned int nSize); 61 char* Strdup(const char *src); 62 }; 63 64 #endif //__CUSTOMHEAP_HPP 65 // End of file. 66 67 //////////////////////////////////////////////////////////////////////////////// 68 // 69 // Filename: CustomHeap.cpp 70 // 71 // Version: 1.0 72 // 73 // Description: Memory allocation pool class. 74 // 75 // Author: Malcolm Nixon (MalcolmNixon@earthlink.net) 76 // 77 // This program is free software; you can redistribute it and/or modify 78 // it under the terms of the GNU General Public License as published by 79 // the Free Software Foundation; either version 2 of the License, or 80 // (at your option) any later version. 81 // 82 // This program is distributed in the hope that it will be useful, 83 // but WITHOUT ANY WARRANTY; without even the implied warranty of 84 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 85 // GNU General Public License for more details. 86 // 87 // You should have received a copy of the GNU General Public License 88 // along with this program; if not, write to the Free Software 89 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 90 // 91 // A copy of the GNU GPL can be found in gpl.txt or at http://www.gnu.org 92 93 #include "CustomHeap.hpp" 94 #include <stdlib.h> 95 #include <string.h> 96 97 // Standard checks. 98 #ifndef __cplusplus 99 #error __FILE__ requires a C++ compiler. 100 #endif //__cplusplus 101 102 CustomHeap::CustomHeap() 103 { 104 // Zero the member variables. 105 m_nBlkSize = 0; 106 m_pRoot = NULL; 107 } 108 109 CustomHeap::~CustomHeap() 110 { 111 // Destroy any heap still created. 112 DestroyHeap(); 113 } 114 115 void CustomHeap::CreateHeap(unsigned int nBlkSize) 116 { 117 // Destroy any heap still in existence. 118 DestroyHeap(); 119 120 // Save off the default block size to use. 121 m_nBlkSize = nBlkSize; 122 } 123 124 void CustomHeap::DestroyHeap() 125 { 126 // Loop over the blocks. 127 CustomHeapBlock* pBlock = m_pRoot; 128 while (pBlock != NULL) 129 { 130 // Save the next pointer. 131 CustomHeapBlock* pNext = pBlock->m_pNext; 132 133 // Destroy the block. 134 free(pBlock->m_pBuffer); 135 delete pBlock; 136 137 // Walk to the next block. 138 pBlock = pNext; 139 } 140 141 // Zero the members. 142 m_pRoot = NULL; 143 m_nBlkSize = 0L; 144 } 145 146 void* CustomHeap::Malloc(unsigned int size) 147 { 148 // Make sure the root block contains enough storage for this request 149 // (even if we have to build a new root block). 150 if (m_pRoot == NULL || size > m_pRoot->m_nFree) 151 { 152 // Calculate the new block size. 153 unsigned int blkSize = (size > m_nBlkSize) ? size : m_nBlkSize; 154 155 // Build the new block. 156 CustomHeapBlock* pNewBlock = new CustomHeapBlock; 157 pNewBlock->m_pBuffer = (unsigned char*)malloc(blkSize); 158 pNewBlock->m_nUsed = 0; 159 pNewBlock->m_nFree = blkSize; 160 pNewBlock->m_pNext = m_pRoot; 161 m_pRoot = pNewBlock; 162 } 163 164 // Save the return pointer. 165 void* ret = m_pRoot->m_pBuffer + m_pRoot->m_nUsed; 166 167 // Adjust the root block to indicate the usage. 168 m_pRoot->m_nUsed += size; 169 m_pRoot->m_nFree -= size; 170 171 return ret; 172 } 173 174 // Duplicate the contents of the string in a block of custom heap memory. 175 char* CustomHeap::Strdup(const char *src) 176 { 177 // Make sure src points to a valid string. 178 if (!src) 179 { 180 return NULL; 181 } 182 183 // Allocate a buffer large enough for the string an null terminator. 184 char* ret = (char*)Malloc(strlen(src) + 1); 185 186 // Copy the string contents into the new buffer. 187 strcpy(ret, src); 188 189 return ret; 190 } 191 192 // End of file. 193

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS