Eneboo - Documentación para desarrolladores
|
00001 // -*- C++ -*- 00002 00003 /* 00004 00005 Heap Layers: An Extensible Memory Allocation Infrastructure 00006 00007 Copyright (C) 2000-2003 by Emery Berger 00008 http://www.cs.umass.edu/~emery 00009 emery@cs.umass.edu 00010 00011 This program is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or 00014 (at your option) any later version. 00015 00016 This program is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 GNU General Public License for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with this program; if not, write to the Free Software 00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00024 00025 */ 00026 00027 00028 #ifndef _SLOPHEAP_H_ 00029 #define _SLOPHEAP_H_ 00030 00042 namespace HL { 00043 00044 template <class SuperHeap, int SLOP = 16> 00045 class SlopHeap : public SuperHeap { 00046 public: 00047 SlopHeap (void) 00048 : remaining (0), 00049 ptr (NULL) 00050 {} 00051 00052 inline void * malloc (const size_t nbytes) { 00053 00054 // Put the usual case up front. 00055 if (nbytes <= remaining) { 00056 remaining -= nbytes; 00057 char * p = ptr; 00058 ptr += nbytes; 00059 return (void *) p; 00060 } 00061 00062 // 00063 // We don't have enough space to satisfy the current 00064 // request, so get more memory. 00065 // 00066 00067 return getMoreMemory(nbytes); 00068 } 00069 00070 inline void clear (void) { 00071 ptr = NULL; 00072 remaining = 0; 00073 SuperHeap::clear (); 00074 } 00075 00076 inline void free (void *) {} 00077 00078 private: 00079 00080 // Disabled. 00081 inline int remove (void *); 00082 00083 void * getMoreMemory (size_t nbytes) { 00084 char * newptr = (char *) SuperHeap::malloc (nbytes + SLOP); 00085 00086 if (newptr == NULL) { 00087 return NULL; 00088 } 00089 00090 // 00091 // If this new memory is contiguous with the previous one, 00092 // reclaim the "slop". 00093 // 00094 00095 if ((ptr != NULL) && (ptr + remaining + SLOP == newptr)) { 00096 remaining += SLOP; 00097 } else { 00098 ptr = newptr; 00099 remaining = 0; 00100 } 00101 char * p = ptr; 00102 ptr += nbytes; 00103 00104 return (void *) p; 00105 } 00106 00107 char * ptr; 00108 size_t remaining; 00109 00110 }; 00111 00112 } 00113 00114 #endif