blob: 179988d418f15a469b775a26a2d72065f2a4ad72 [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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
7namespace v8 {
8namespace internal {
9namespace compiler {
10
11ZonePool::StatsScope::StatsScope(ZonePool* zone_pool)
12 : zone_pool_(zone_pool),
13 total_allocated_bytes_at_start_(zone_pool->GetTotalAllocatedBytes()),
14 max_allocated_bytes_(0) {
15 zone_pool_->stats_.push_back(this);
16 for (auto zone : zone_pool_->used_) {
17 size_t size = static_cast<size_t>(zone->allocation_size());
18 std::pair<InitialValues::iterator, bool> res =
19 initial_values_.insert(std::make_pair(zone, size));
20 USE(res);
21 DCHECK(res.second);
22 }
23}
24
25
26ZonePool::StatsScope::~StatsScope() {
27 DCHECK_EQ(zone_pool_->stats_.back(), this);
28 zone_pool_->stats_.pop_back();
29}
30
31
32size_t ZonePool::StatsScope::GetMaxAllocatedBytes() {
33 return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes());
34}
35
36
37size_t ZonePool::StatsScope::GetCurrentAllocatedBytes() {
38 size_t total = 0;
39 for (Zone* zone : zone_pool_->used_) {
40 total += static_cast<size_t>(zone->allocation_size());
41 // Adjust for initial values.
42 InitialValues::iterator it = initial_values_.find(zone);
43 if (it != initial_values_.end()) {
44 total -= it->second;
45 }
46 }
47 return total;
48}
49
50
51size_t ZonePool::StatsScope::GetTotalAllocatedBytes() {
52 return zone_pool_->GetTotalAllocatedBytes() - total_allocated_bytes_at_start_;
53}
54
55
56void ZonePool::StatsScope::ZoneReturned(Zone* zone) {
57 size_t current_total = GetCurrentAllocatedBytes();
58 // Update max.
59 max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total);
60 // Drop zone from initial value map.
61 InitialValues::iterator it = initial_values_.find(zone);
62 if (it != initial_values_.end()) {
63 initial_values_.erase(it);
64 }
65}
66
67
68ZonePool::ZonePool(Isolate* isolate)
69 : isolate_(isolate), max_allocated_bytes_(0), total_deleted_bytes_(0) {}
70
71
72ZonePool::~ZonePool() {
73 DCHECK(used_.empty());
74 DCHECK(stats_.empty());
75 for (Zone* zone : unused_) {
76 delete zone;
77 }
78}
79
80
81size_t ZonePool::GetMaxAllocatedBytes() {
82 return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes());
83}
84
85
86size_t ZonePool::GetCurrentAllocatedBytes() {
87 size_t total = 0;
88 for (Zone* zone : used_) {
89 total += static_cast<size_t>(zone->allocation_size());
90 }
91 return total;
92}
93
94
95size_t ZonePool::GetTotalAllocatedBytes() {
96 return total_deleted_bytes_ + GetCurrentAllocatedBytes();
97}
98
99
100Zone* ZonePool::NewEmptyZone() {
101 Zone* zone;
102 // Grab a zone from pool if possible.
103 if (!unused_.empty()) {
104 zone = unused_.back();
105 unused_.pop_back();
106 } else {
107 zone = new Zone(isolate_);
108 }
109 used_.push_back(zone);
110 DCHECK_EQ(0, zone->allocation_size());
111 return zone;
112}
113
114
115void ZonePool::ReturnZone(Zone* zone) {
116 size_t current_total = GetCurrentAllocatedBytes();
117 // Update max.
118 max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total);
119 // Update stats.
120 for (auto stat_scope : stats_) {
121 stat_scope->ZoneReturned(zone);
122 }
123 // Remove from used.
124 Used::iterator it = std::find(used_.begin(), used_.end(), zone);
125 DCHECK(it != used_.end());
126 used_.erase(it);
127 total_deleted_bytes_ += static_cast<size_t>(zone->allocation_size());
128 // Delete zone or clear and stash on unused_.
129 if (unused_.size() >= kMaxUnusedSize) {
130 delete zone;
131 } else {
132 zone->DeleteAll();
133 DCHECK_EQ(0, zone->allocation_size());
134 unused_.push_back(zone);
135 }
136}
137
138} // namespace compiler
139} // namespace internal
140} // namespace v8