00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00055 if (nbytes <= remaining) {
00056 remaining -= nbytes;
00057 char * p = ptr;
00058 ptr += nbytes;
00059 return (void *) p;
00060 }
00061
00062
00063
00064
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
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
00092
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