00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020
00021 #ifndef _THREADHEAP_H_
00022 #define _THREADHEAP_H_
00023
00024 #include "config.h"
00025
00026
00027 #include "hoardheap.h"
00028
00029 class processHeap;
00030 class persistentHeap;
00031
00032
00033
00034
00035
00036 class threadHeap : public hoardHeap {
00037
00038 public:
00039
00040 threadHeap (void);
00041
00042
00043 void * malloc (const size_t sz);
00044 inline void * memalign (size_t alignment, size_t sz);
00045
00046
00047 inline static size_t objectSize (void * ptr);
00048
00049
00050 inline void setpHeap (processHeap * p);
00051
00052
00053 inline void setPersistentHeap (persistentHeap * p);
00054 private:
00055
00056
00057 threadHeap (const threadHeap&);
00058 const threadHeap& operator= (const threadHeap&);
00059
00060
00061 processHeap * _pHeap;
00062
00063
00064 persistentHeap * _persistentHeap;
00065
00066
00067
00068
00069 double _pad[CACHE_LINE / sizeof(double)];
00070 };
00071
00072
00073 void * threadHeap::memalign (size_t alignment,
00074 size_t size)
00075 {
00076
00077
00078
00079 size_t newSize;
00080
00081
00082
00083 if (alignment <= ALIGNMENT) {
00084 return this->malloc (size);
00085 }
00086
00087 if (alignment < sizeof(block)) {
00088 alignment = sizeof(block);
00089 }
00090
00091
00092 assert ((alignment & (alignment - 1)) == 0);
00093
00094
00095 newSize = size + sizeof(block) + alignment;
00096
00097
00098
00099
00100 void * ptr = this->malloc (newSize);
00101 if ((((unsigned long) ptr) & -((long) alignment)) == 0) {
00102
00103 assert (((unsigned long) ptr % alignment) == 0);
00104 return ptr;
00105
00106 } else {
00107
00108
00109 char * newptr = (char *)
00110 (((unsigned long) ptr + alignment - 1) & -((long) alignment));
00111
00112
00113
00114 if ((unsigned long) newptr - (unsigned long) ptr < sizeof(block)) {
00115 newptr += alignment;
00116 }
00117 assert (((unsigned long) newptr % alignment) == 0);
00118
00119
00120 block * b = ((block *) ptr - 1);
00121
00122 assert (b->isValid());
00123 assert (b->getSuperblock()->isValid());
00124
00125
00126 assert (((unsigned long) newptr - (unsigned long) ptr) >= sizeof(block));
00127
00128 block * p = ((block *) newptr - 1);
00129
00130
00131 assert (((unsigned long) p - sizeof(block)) >= (unsigned long) b);
00132
00133 if (p != b) {
00134 assert ((unsigned long) newptr > (unsigned long) ptr);
00135
00136 *p = *b;
00137 assert (p->isValid());
00138 assert (p->getSuperblock()->isValid());
00139
00140
00141
00142 p->setNext ((block *) ((unsigned long) b | 1));
00143
00144 } else {
00145 assert (ptr != newptr);
00146 }
00147
00148 assert (((unsigned long) ptr + newSize) >= ((unsigned long) newptr + size));
00149 return newptr;
00150 }
00151 }
00152
00153
00154 size_t threadHeap::objectSize (void * ptr)
00155 {
00156
00157
00158 block * b = ((block *) ptr - 1);
00159 assert (b->isValid());
00160 superblock * sb = b->getSuperblock ();
00161 assert (sb);
00162
00163
00164 return sizeFromClass (sb->getBlockSizeClass());
00165 }
00166
00167
00168 void threadHeap::setpHeap (processHeap * p)
00169 {
00170 _pHeap = p;
00171 }
00172
00173 void threadHeap::setPersistentHeap (persistentHeap * p)
00174 {
00175 _persistentHeap = p;
00176 }
00177
00178
00179 #endif // _THREADHEAP_H_
00180