Chromium Code Reviews| Index: src/vm/immutable_heap.cc |
| diff --git a/src/vm/immutable_heap.cc b/src/vm/immutable_heap.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3c2e77a23bbc71d674e17fdedb6f5c0d638748fe |
| --- /dev/null |
| +++ b/src/vm/immutable_heap.cc |
| @@ -0,0 +1,102 @@ |
| +// Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE.md file. |
| + |
| +#include "src/vm/immutable_heap.h" |
| +#include "src/vm/object_memory.h" |
| + |
| +namespace fletch { |
| + |
| +ImmutableHeap::ImmutableHeap() |
| + : number_of_hw_threads_(Platform::GetNumberOfHardwareThreads()), |
| + heap_mutex_(Platform::CreateMutex()), |
| + heap_(new Space(), reinterpret_cast<WeakPointer*>(NULL)), |
| + outstanding_parts_(0), |
| + cached_parts_(), |
| + ticks_(0) { |
| +} |
| + |
| +ImmutableHeap::~ImmutableHeap() { |
| + delete heap_mutex_; |
| + |
| + while (HasCachedParts()) { |
| + Heap* heap = RemoveCachedPart(); |
| + delete heap; |
| + } |
| +} |
| + |
| +void ImmutableHeap::AddCachedPart(Heap* heap) { |
| + CachedHeapPart* cached_part = new CachedHeapPart(); |
|
Mads Ager (google)
2015/08/05 10:19:02
This could be a one-liner?
cached_parts_ = new Ca
kustermann
2015/08/05 11:11:46
Made it a two liner, see other reason why I don't
|
| + cached_part->heap_part_ = heap; |
| + cached_part->next_ = cached_parts_; |
| + cached_parts_ = cached_part; |
| +} |
| + |
| +Heap* ImmutableHeap::RemoveCachedPart() { |
| + CachedHeapPart* part = cached_parts_; |
| + if (part == NULL) return NULL; |
| + |
| + Heap* heap = part->heap_part_; |
| + cached_parts_ = part->next_; |
| + delete part; |
| + return heap; |
| +} |
| + |
| +void ImmutableHeap::MergeParts() { |
| + ScopedLock locker(heap_mutex_); |
| + |
| + ASSERT(outstanding_parts_ == 0); |
| + while (HasCachedParts()) { |
| + Heap* heap_part = RemoveCachedPart(); |
| + heap_.MergeInOtherHeap(heap_part); |
| + delete heap_part; |
| + } |
| + |
| + ticks_ = 0; |
| +} |
| + |
| +void ImmutableHeap::IterateProgramPointers(PointerVisitor* visitor) { |
| + ASSERT(outstanding_parts_ == 0 && cached_parts_ == NULL); |
| + |
| + HeapObjectPointerVisitor heap_pointer_visitor(visitor); |
| + heap_.IterateObjects(&heap_pointer_visitor); |
| +} |
| + |
| +Heap* ImmutableHeap::AcquirePart() { |
| + ScopedLock locker(heap_mutex_); |
| + |
| + // Calculate new allocation budget we want to give out. |
| + Space* merged_space = heap_.space(); |
| + int size = 0; |
| + if (merged_space != NULL && !merged_space->is_empty()) { |
| + // We allow each thread to get the amount of live memory per |
| + // thread (i.e. a "2x of live memory" heap size strategy). |
| + size = merged_space->Used() / number_of_hw_threads_; |
| + } |
| + int budget = Utils::Maximum(size, Space::kDefaultChunkSize); |
| + |
| + Heap* part; |
| + if (HasCachedParts()) { |
| + part = RemoveCachedPart(); |
| + part->space()->SetAllocationBudget(budget); |
| + } else { |
| + part = new Heap(NULL, budget); |
| + } |
| + |
| + outstanding_parts_++; |
| + return part; |
| +} |
| + |
| +bool ImmutableHeap::ReleasePart(Heap* part) { |
| + ScopedLock locker(heap_mutex_); |
| + ASSERT(outstanding_parts_ > 0); |
| + outstanding_parts_--; |
| + |
| + part->Flush(); |
| + part->AdjustAllocationBudget(); |
| + AddCachedPart(part); |
| + |
| + return ticks_++ == number_of_hw_threads_; |
| +} |
| + |
| +} // namespace fletch |