00001
00002
00003 #ifndef _FREELISTHEAP_H_
00004 #define _FREELISTHEAP_H_
00005
00015 #include "freesllist.h"
00016 #include <assert.h>
00017
00018 #ifndef NULL
00019 #define NULL 0
00020 #endif
00021
00022 namespace HL {
00023
00024 template <class SuperHeap>
00025 class FreelistHeap : public SuperHeap {
00026 public:
00027
00028 inline void * malloc (size_t sz) {
00029
00030 void * ptr = _freelist.get();
00031
00032
00033 if (ptr == 0) {
00034 ptr = SuperHeap::malloc (sz);
00035 }
00036 return ptr;
00037 }
00038
00039 inline void free (void * ptr) {
00040 if (ptr == 0) {
00041 return;
00042 }
00043 _freelist.insert (ptr);
00044 }
00045
00046 inline void clear (void) {
00047 void * ptr;
00048 while (ptr = _freelist.get()) {
00049 SuperHeap::free (ptr);
00050 }
00051 }
00052
00053 private:
00054
00055 FreeSLList _freelist;
00056
00057 };
00058
00059 }
00060
00061 #endif