00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020
00021 #include <limits.h>
00022 #include <string.h>
00023
00024 #include "config.h"
00025
00026 #include "hoardheap.h"
00027 #include "threadheap.h"
00028 #include "processheap.h"
00029
00030
00031 threadHeap::threadHeap (void)
00032 : _pHeap (0)
00033 {}
00034
00035
00036
00037
00038
00039
00040
00041
00042 void * threadHeap::malloc (const size_t size)
00043 {
00044 const int sizeclass = sizeClass (size);
00045 block * b = NULL;
00046
00047 lock();
00048
00049
00050
00051
00052
00053 superblock * sb = findAvailableSuperblock (sizeclass, b, _pHeap);
00054
00055 if (sb == NULL) {
00056
00057
00058
00059
00060 assert (_pHeap);
00061 sb = _pHeap->acquire ((int) sizeclass, this);
00062
00063
00064
00065 if (sb == NULL) {
00066 sb = superblock::makeSuperblock (sizeclass, _pHeap);
00067 if (sb == NULL) {
00068
00069 unlock ();
00070 return NULL;
00071 }
00072 #if HEAP_LOG
00073
00074 MemoryRequest m;
00075 m.allocate ((int) sb->getNumBlocks() * (int) sizeFromClass(sb->getBlockSizeClass()));
00076 _pHeap->getLog(getIndex()).append(m);
00077 #endif
00078 #if HEAP_FRAG_STATS
00079 _pHeap->setAllocated (0, sb->getNumBlocks() * sizeFromClass(sb->getBlockSizeClass()));
00080 #endif
00081 }
00082
00083
00084 b = sb->getBlock ();
00085 assert (b != NULL);
00086
00087
00088 insertSuperblock (sizeclass, sb, _pHeap);
00089 }
00090
00091 assert (b != NULL);
00092 assert (b->isValid());
00093 assert (sb->isValid());
00094
00095 b->markAllocated();
00096
00097 #if HEAP_LOG
00098 MemoryRequest m;
00099 m.malloc ((void *) (b + 1), align(size));
00100 _pHeap->getLog(getIndex()).append(m);
00101 #endif
00102 #if HEAP_FRAG_STATS
00103 b->setRequestedSize (align(size));
00104 _pHeap->setAllocated (align(size), 0);
00105 #endif
00106
00107 unlock();
00108
00109
00110 return (void *) (b + 1);
00111 }