From 8cf2f36c3740e02e49c0a5aace44418e070c225e Mon Sep 17 00:00:00 2001 From: Pawel Dziepak Date: Thu, 12 Dec 2013 21:14:25 +0100 Subject: [PATCH] libroot: Fix Hoard when the CPU count is not a power of two --- src/system/libroot/posix/malloc/heap.cpp | 5 +++-- src/system/libroot/posix/malloc/heap.h | 6 ++++++ src/system/libroot/posix/malloc/processheap.cpp | 17 ++++++++--------- src/system/libroot/posix/malloc/processheap.h | 12 +++--------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/system/libroot/posix/malloc/heap.cpp b/src/system/libroot/posix/malloc/heap.cpp index 5ded94723e..99aba542d2 100644 --- a/src/system/libroot/posix/malloc/heap.cpp +++ b/src/system/libroot/posix/malloc/heap.cpp @@ -99,6 +99,7 @@ size_t hoardHeap::_threshold[hoardHeap::SIZE_CLASSES] = { #endif +int hoardHeap::fMaxThreadHeaps = 1; int hoardHeap::_numProcessors; int hoardHeap::_numProcessorsMask; @@ -454,7 +455,7 @@ hoardHeap::initNumProcs(void) else hoardHeap::_numProcessors = info.cpu_count; - hoardHeap::_numProcessorsMask = - (1 << (lg(hoardHeap::_numProcessors) + 1)) - 1; + fMaxThreadHeaps = 1 << (lg(_numProcessors) + 1); + _numProcessorsMask = fMaxThreadHeaps - 1; } diff --git a/src/system/libroot/posix/malloc/heap.h b/src/system/libroot/posix/malloc/heap.h index 84747c27c5..b2a9ed6ba3 100644 --- a/src/system/libroot/posix/malloc/heap.h +++ b/src/system/libroot/posix/malloc/heap.h @@ -205,6 +205,12 @@ class hoardHeap { static void initNumProcs(void); protected: + // The maximum number of thread heaps we allow. (NOT the maximum + // number of threads -- Hoard imposes no such limit.) This must be + // a power of two! NB: This number is twice the maximum number of + // PROCESSORS supported by Hoard. + static int fMaxThreadHeaps; + // number of CPUs, cached static int _numProcessors; static int _numProcessorsMask; diff --git a/src/system/libroot/posix/malloc/processheap.cpp b/src/system/libroot/posix/malloc/processheap.cpp index 808c42a24f..129cf4c3a0 100644 --- a/src/system/libroot/posix/malloc/processheap.cpp +++ b/src/system/libroot/posix/malloc/processheap.cpp @@ -37,8 +37,7 @@ using namespace BPrivate; processHeap::processHeap() : - kMaxThreadHeaps(_numProcessors * 2), - theap((HEAPTYPE*)hoardSbrk(sizeof(HEAPTYPE) * kMaxThreadHeaps)), + theap((HEAPTYPE*)hoardSbrk(sizeof(HEAPTYPE) * fMaxThreadHeaps)), #if HEAP_FRAG_STATS _currentAllocated(0), _currentRequested(0), @@ -48,32 +47,32 @@ processHeap::processHeap() #endif #if HEAP_LOG _log((Log*) - hoardSbrk(sizeof(Log) * (kMaxThreadHeaps + 1))), + hoardSbrk(sizeof(Log) * (fMaxThreadHeaps + 1))), #endif _buffer(NULL), _bufferCount(0) { if (theap == NULL) return; - new(theap) HEAPTYPE[kMaxThreadHeaps]; + new(theap) HEAPTYPE[fMaxThreadHeaps]; #if HEAP_LOG if (_log == NULL) return; - new(_log) Log[kMaxThreadHeaps + 1]; + new(_log) Log[fMaxThreadHeaps + 1]; #endif int i; // The process heap is heap 0. setIndex(0); - for (i = 0; i < kMaxThreadHeaps; i++) { + for (i = 0; i < fMaxThreadHeaps; i++) { // Set every thread's process heap to this one. theap[i].setpHeap(this); // Set every thread heap's index. theap[i].setIndex(i + 1); } #if HEAP_LOG - for (i = 0; i < kMaxThreadHeaps + 1; i++) { + for (i = 0; i < fMaxThreadHeaps + 1; i++) { char fname[255]; sprintf(fname, "log%d", i); unlink(fname); @@ -94,7 +93,7 @@ processHeap::stats(void) #if HEAP_STATS int umax = 0; int amax = 0; - for (int j = 0; j < kMaxThreadHeaps; j++) { + for (int j = 0; j < fMaxThreadHeaps; j++) { for (int i = 0; i < SIZE_CLASSES; i++) { amax += theap[j].maxAllocated(i) * sizeFromClass(i); umax += theap[j].maxInUse(i) * sizeFromClass(i); @@ -118,7 +117,7 @@ processHeap::stats(void) #if HEAP_LOG printf("closing logs.\n"); fflush(stdout); - for (int i = 0; i < kMaxThreadHeaps + 1; i++) { + for (int i = 0; i < fMaxThreadHeaps + 1; i++) { _log[i].close(); } #endif diff --git a/src/system/libroot/posix/malloc/processheap.h b/src/system/libroot/posix/malloc/processheap.h index 4aa17c7fe9..f2222897f2 100644 --- a/src/system/libroot/posix/malloc/processheap.h +++ b/src/system/libroot/posix/malloc/processheap.h @@ -132,12 +132,6 @@ class processHeap : public hoardHeap { processHeap(const processHeap &); const processHeap & operator=(const processHeap &); - // The maximum number of thread heaps we allow. (NOT the maximum - // number of threads -- Hoard imposes no such limit.) This must be - // a power of two! NB: This number is twice the maximum number of - // PROCESSORS supported by Hoard. - const int kMaxThreadHeaps; - // The per-thread heaps. HEAPTYPE* theap; @@ -174,7 +168,7 @@ processHeap::getHeap(int i) { assert(theap != NULL); assert(i >= 0); - assert(i < kMaxThreadHeaps); + assert(i < fMaxThreadHeaps); return theap[i]; } @@ -185,7 +179,7 @@ processHeap::getLog(int i) { assert(_log != NULL); assert(i >= 0); - assert(i < kMaxThreadHeaps + 1); + assert(i < fMaxThreadHeaps + 1); return _log[i]; } #endif @@ -200,7 +194,7 @@ processHeap::getHeapIndex(void) // In fact, for efficiency, we just round up to the highest power of two, // times two. int tid = find_thread(NULL) & _numProcessorsMask; - assert(tid < kMaxThreadHeaps); + assert(tid < fMaxThreadHeaps); return tid; }