OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/compiler/zone-pool.h" |
| 6 |
| 7 namespace v8 { |
| 8 namespace internal { |
| 9 namespace compiler { |
| 10 |
| 11 ZonePool::StatsScope::StatsScope(ZonePool* zone_pool) |
| 12 : zone_pool_(zone_pool), max_allocated_bytes_(0) { |
| 13 zone_pool_->stats_.push_back(this); |
| 14 for (auto zone : zone_pool_->used_) { |
| 15 size_t size = static_cast<size_t>(zone->allocation_size()); |
| 16 std::pair<InitialValues::iterator, bool> res = |
| 17 initial_values_.insert(std::make_pair(zone, size)); |
| 18 USE(res); |
| 19 DCHECK(res.second); |
| 20 } |
| 21 } |
| 22 |
| 23 |
| 24 ZonePool::StatsScope::~StatsScope() { |
| 25 DCHECK_EQ(zone_pool_->stats_.back(), this); |
| 26 zone_pool_->stats_.pop_back(); |
| 27 } |
| 28 |
| 29 |
| 30 size_t ZonePool::StatsScope::GetMaxAllocatedBytes() { |
| 31 return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes()); |
| 32 } |
| 33 |
| 34 |
| 35 size_t ZonePool::StatsScope::GetCurrentAllocatedBytes() { |
| 36 size_t total = 0; |
| 37 for (Zone* zone : zone_pool_->used_) { |
| 38 total += static_cast<size_t>(zone->allocation_size()); |
| 39 // Adjust for initial values. |
| 40 InitialValues::iterator it = initial_values_.find(zone); |
| 41 if (it != initial_values_.end()) { |
| 42 total -= it->second; |
| 43 } |
| 44 } |
| 45 return total; |
| 46 } |
| 47 |
| 48 |
| 49 void ZonePool::StatsScope::ZoneReturned(Zone* zone) { |
| 50 size_t current_total = GetCurrentAllocatedBytes(); |
| 51 // Update max. |
| 52 max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total); |
| 53 // Drop zone from initial value map. |
| 54 InitialValues::iterator it = initial_values_.find(zone); |
| 55 if (it != initial_values_.end()) { |
| 56 initial_values_.erase(it); |
| 57 } |
| 58 } |
| 59 |
| 60 |
| 61 ZonePool::ZonePool(Isolate* isolate) |
| 62 : isolate_(isolate), max_allocated_bytes_(0), total_deleted_bytes_(0) {} |
| 63 |
| 64 |
| 65 ZonePool::~ZonePool() { |
| 66 DCHECK(used_.empty()); |
| 67 DCHECK(stats_.empty()); |
| 68 for (Zone* zone : unused_) { |
| 69 delete zone; |
| 70 } |
| 71 } |
| 72 |
| 73 |
| 74 size_t ZonePool::GetMaxAllocatedBytes() { |
| 75 return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes()); |
| 76 } |
| 77 |
| 78 |
| 79 size_t ZonePool::GetCurrentAllocatedBytes() { |
| 80 size_t total = 0; |
| 81 for (Zone* zone : used_) { |
| 82 total += static_cast<size_t>(zone->allocation_size()); |
| 83 } |
| 84 return total; |
| 85 } |
| 86 |
| 87 |
| 88 size_t ZonePool::GetTotalAllocatedBytes() { |
| 89 return total_deleted_bytes_ + GetCurrentAllocatedBytes(); |
| 90 } |
| 91 |
| 92 |
| 93 Zone* ZonePool::NewEmptyZone() { |
| 94 Zone* zone; |
| 95 // Grab a zone from pool if possible. |
| 96 if (!unused_.empty()) { |
| 97 zone = unused_.back(); |
| 98 unused_.pop_back(); |
| 99 } else { |
| 100 zone = new Zone(isolate_); |
| 101 } |
| 102 used_.push_back(zone); |
| 103 DCHECK_EQ(0, zone->allocation_size()); |
| 104 return zone; |
| 105 } |
| 106 |
| 107 |
| 108 void ZonePool::ReturnZone(Zone* zone) { |
| 109 size_t current_total = GetCurrentAllocatedBytes(); |
| 110 // Update max. |
| 111 max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total); |
| 112 // Update stats. |
| 113 for (auto stat_scope : stats_) { |
| 114 stat_scope->ZoneReturned(zone); |
| 115 } |
| 116 // Remove from used. |
| 117 Used::iterator it = std::find(used_.begin(), used_.end(), zone); |
| 118 DCHECK(it != used_.end()); |
| 119 used_.erase(it); |
| 120 total_deleted_bytes_ += static_cast<size_t>(zone->allocation_size()); |
| 121 // Delete zone or clear and stash on unused_. |
| 122 if (unused_.size() >= kMaxUnusedSize) { |
| 123 delete zone; |
| 124 } else { |
| 125 zone->DeleteAll(); |
| 126 DCHECK_EQ(0, zone->allocation_size()); |
| 127 unused_.push_back(zone); |
| 128 } |
| 129 } |
| 130 |
| 131 } // namespace compiler |
| 132 } // namespace internal |
| 133 } // namespace v8 |
OLD | NEW |