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 #ifndef _SIZEHEAP_H_
00028 #define _SIZEHEAP_H_
00029
00035 #include <assert.h>
00036
00037 #include "addheap.h"
00038
00039 #if 1
00040
00047 namespace HL {
00048
00049 template <class Super>
00050 class UseSizeHeap : public Super {
00051 public:
00052
00053 inline UseSizeHeap (void) {}
00054
00055 inline static size_t getSize (const void * ptr) {
00056 return ((freeObject *) ptr - 1)->sz;
00057 }
00058
00059 protected:
00060 union freeObject {
00061 size_t sz;
00062 double _dummy;
00063 };
00064 };
00065
00071 template <class SuperHeap>
00072 class SizeHeap : public UseSizeHeap<SuperHeap> {
00073 typedef typename UseSizeHeap<SuperHeap>::freeObject freeObject;
00074 public:
00075 inline SizeHeap (void) {}
00076 inline void * malloc (const size_t sz) {
00077
00078 freeObject * ptr = (freeObject *)
00079 SuperHeap::malloc (sz + sizeof(freeObject));
00080
00081 ptr->sz = sz;
00082 return (void *) (ptr + 1);
00083 }
00084 inline void free (void * ptr) {
00085 SuperHeap::free ((freeObject *) ptr - 1);
00086 }
00087 };
00088
00089 };
00090
00091 #else
00092
00093 template <class Super>
00094 class SizeHeap : public Super {
00095 public:
00096
00097 inline void * malloc (size_t sz) {
00098
00099 assert (sizeof(size_t) <= sizeof(double));
00100 void * ptr = Super::malloc (sz + sizeof(double));
00101
00102 *((size_t *) ptr) = sz;
00103 return (void *) ((double *) ptr + 1);
00104 }
00105
00106 inline void free (void * ptr) {
00107 void * origPtr = (void *) ((double *) ptr - 1);
00108 Super::free (origPtr);
00109 }
00110
00111 inline static size_t getSize (void * ptr) {
00112 return *((size_t *) ((double *) ptr - 1));
00113 }
00114 };
00115
00116
00117
00118 template <class Super>
00119 class UseSizeHeap : public Super {
00120 public:
00121
00122 inline static size_t getSize (void * ptr) {
00123 return *((size_t *) ((double *) ptr - 1));
00124 }
00125 };
00126
00127 #endif
00128
00129 #endif