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

Memory pool object

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


Description

These functions handle 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.h 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_H 28 #define __CUSTOMHEAP_H 29 30 31 // Use C naming convention. 32 #ifdef __cplusplus 33 extern "C" { 34 #endif //__cplusplus 35 36 37 // Heap block used for custom heap storage. 38 typedef struct _custom_heap_block 39 { 40 struct _custom_heap_block* m_pNext; 41 unsigned char* m_pBuffer; 42 unsigned int m_nUsed; 43 unsigned int m_nFree; 44 } CustomHeapBlock; 45 46 47 // Custom heap structure. 48 typedef struct 49 { 50 unsigned int m_nBlkSize; 51 CustomHeapBlock* m_pRoot; 52 } CustomHeap; 53 54 55 // Allocate a custom heap. I recommend a default block size of 0x20000 if you 56 // plan to be using the heap for a large amount of work. 57 extern CustomHeap* CustomHeap_Create(unsigned int nBlkSize); 58 59 // Destroy a custom heap. 60 extern void CustomHeap_Destroy(CustomHeap *heap); 61 62 // Free all memory allocated by the custom heap. 63 extern void CustomHeap_Empty(CustomHeap *heap); 64 65 // Allocate a block of memory from a custom heap. 66 extern void *CustomHeap_Malloc(CustomHeap *heap, unsigned int nSize); 67 68 // Allocate a string from a custom heap (similar to strdup). 69 extern char *CustomHeap_Strdup(CustomHeap *heap, char *src); 70 71 72 #ifdef __cplusplus 73 } 74 #endif //__cplusplus 75 76 #endif //__CUSTOMHEAP_H 77 // End of file. 78 79 //////////////////////////////////////////////////////////////////////////////// 80 // 81 // Filename: CustomHeap.c 82 // 83 // Version: 1.0 84 // 85 // Description: Memory allocation pool class. 86 // 87 // Author: Malcolm Nixon (MalcolmNixon@earthlink.net) 88 // 89 // This program is free software; you can redistribute it and/or modify 90 // it under the terms of the GNU General Public License as published by 91 // the Free Software Foundation; either version 2 of the License, or 92 // (at your option) any later version. 93 // 94 // This program is distributed in the hope that it will be useful, 95 // but WITHOUT ANY WARRANTY; without even the implied warranty of 96 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 97 // GNU General Public License for more details. 98 // 99 // You should have received a copy of the GNU General Public License 100 // along with this program; if not, write to the Free Software 101 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 102 // 103 // A copy of the GNU GPL can be found in gpl.txt or at http://www.gnu.org 104 105 #include "CustomHeap.h" 106 #include <stdlib.h> 107 #include <string.h> 108 109 110 // Allocate a custom heap. I recommend a default block size of 0x20000 if you 111 // plan to be using the heap for a large amount of work. 112 CustomHeap* CustomHeap_Create(unsigned int nBlkSize) 113 { 114 CustomHeap *heap; 115 116 // Allocate the heap object. 117 heap = (CustomHeap*)calloc(1, sizeof(CustomHeap)); 118 heap->m_nBlkSize = nBlkSize; 119 120 // Return a pointer to the heap. 121 return heap; 122 } 123 124 // Destroy a custom heap. 125 void CustomHeap_Destroy(CustomHeap *heap) 126 { 127 if (heap) 128 { 129 // Empty the custom heap. 130 CustomHeap_Empty(heap); 131 132 // Free the heap object. 133 free(heap); 134 } 135 } 136 137 // Free all memory allocated by the custom heap. 138 void CustomHeap_Empty(CustomHeap *heap) 139 { 140 // Loop over the blocks. 141 CustomHeapBlock *pBlock, *pNext; 142 143 pBlock = heap->m_pRoot; 144 while (pBlock != NULL) 145 { 146 // Save the next pointer. 147 pNext = pBlock->m_pNext; 148 149 // Destroy the block. 150 free(pBlock->m_pBuffer); 151 free(pBlock); 152 153 // Walk to the next block. 154 pBlock = pNext; 155 } 156 157 // Zero the members. 158 heap->m_pRoot = NULL; 159 heap->m_nBlkSize = 0L; 160 } 161 162 // Allocate a block of memory from a custom heap. 163 void* CustomHeap_Malloc(CustomHeap *heap, unsigned int size) 164 { 165 CustomHeapBlock *pNewBlock; 166 void *ret; 167 168 // If the heap object is invalid then return NULL. 169 if (!heap) 170 { 171 return NULL; 172 } 173 174 // Make sure the root block contains enough storage for this request 175 // (even if we have to build a new root block). 176 if (heap->m_pRoot == NULL || size > heap->m_pRoot->m_nFree) 177 { 178 // Calculate the new block size. 179 unsigned int blkSize = (size > heap->m_nBlkSize) ? size : heap->m_nBlkSize; 180 181 // Build the new block. 182 pNewBlock = (CustomHeapBlock*)calloc(1, sizeof(CustomHeapBlock)); 183 pNewBlock->m_pBuffer = (unsigned char*)malloc(blkSize); 184 pNewBlock->m_nUsed = 0; 185 pNewBlock->m_nFree = blkSize; 186 pNewBlock->m_pNext = heap->m_pRoot; 187 heap->m_pRoot = pNewBlock; 188 } 189 190 // Save the return pointer. 191 ret = heap->m_pRoot->m_pBuffer + heap->m_pRoot->m_nUsed; 192 193 // Adjust the root block to indicate the usage. 194 heap->m_pRoot->m_nUsed += size; 195 heap->m_pRoot->m_nFree -= size; 196 197 return ret; 198 } 199 200 // Allocate a string from a custom heap (similar to strdup). 201 char* CustomHeap_Strdup(CustomHeap *heap, char *src) 202 { 203 char *ret; 204 205 // Make sure src points to a valid string. 206 if (!heap || !src) 207 { 208 return NULL; 209 } 210 211 // Allocate a buffer large enough for the string an null terminator. 212 ret = (char*)CustomHeap_Malloc(heap, strlen(src) + 1); 213 214 // Copy the string contents into the new buffer. 215 strcpy(ret, src); 216 217 return ret; 218 } 219 220 /* Example use of CustomHeap objects. 221 void main(void) 222 { 223 CustomHeap *heap; 224 int i; 225 226 // Allocate a custom heap of 1K chunks. 227 heap = CustomHeap_Create(1024); 228 229 // Allocate 1000 copies of "Hello World". 230 for (i = 0; i < 1000; ++i) 231 { 232 CustomHeap_Strdup(heap, "Hello World"); 233 } 234 235 // Destroy the heap. 236 CustomHeap_Destroy(heap); 237 } 238 */ 239 240 // End of file. 241

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS