zone-pool.cc 3.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/zone-pool.h"

namespace v8 {
namespace internal {
namespace compiler {

ZonePool::StatsScope::StatsScope(ZonePool* zone_pool)
12 13 14
    : zone_pool_(zone_pool),
      total_allocated_bytes_at_start_(zone_pool->GetTotalAllocatedBytes()),
      max_allocated_bytes_(0) {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  zone_pool_->stats_.push_back(this);
  for (auto zone : zone_pool_->used_) {
    size_t size = static_cast<size_t>(zone->allocation_size());
    std::pair<InitialValues::iterator, bool> res =
        initial_values_.insert(std::make_pair(zone, size));
    USE(res);
    DCHECK(res.second);
  }
}


ZonePool::StatsScope::~StatsScope() {
  DCHECK_EQ(zone_pool_->stats_.back(), this);
  zone_pool_->stats_.pop_back();
}


size_t ZonePool::StatsScope::GetMaxAllocatedBytes() {
  return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes());
}


size_t ZonePool::StatsScope::GetCurrentAllocatedBytes() {
  size_t total = 0;
  for (Zone* zone : zone_pool_->used_) {
    total += static_cast<size_t>(zone->allocation_size());
    // Adjust for initial values.
    InitialValues::iterator it = initial_values_.find(zone);
    if (it != initial_values_.end()) {
      total -= it->second;
    }
  }
  return total;
}


51 52 53 54 55
size_t ZonePool::StatsScope::GetTotalAllocatedBytes() {
  return zone_pool_->GetTotalAllocatedBytes() - total_allocated_bytes_at_start_;
}


56 57 58 59 60 61 62 63 64 65 66 67
void ZonePool::StatsScope::ZoneReturned(Zone* zone) {
  size_t current_total = GetCurrentAllocatedBytes();
  // Update max.
  max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total);
  // Drop zone from initial value map.
  InitialValues::iterator it = initial_values_.find(zone);
  if (it != initial_values_.end()) {
    initial_values_.erase(it);
  }
}


68
ZonePool::ZonePool() : max_allocated_bytes_(0), total_deleted_bytes_(0) {}
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105


ZonePool::~ZonePool() {
  DCHECK(used_.empty());
  DCHECK(stats_.empty());
  for (Zone* zone : unused_) {
    delete zone;
  }
}


size_t ZonePool::GetMaxAllocatedBytes() {
  return std::max(max_allocated_bytes_, GetCurrentAllocatedBytes());
}


size_t ZonePool::GetCurrentAllocatedBytes() {
  size_t total = 0;
  for (Zone* zone : used_) {
    total += static_cast<size_t>(zone->allocation_size());
  }
  return total;
}


size_t ZonePool::GetTotalAllocatedBytes() {
  return total_deleted_bytes_ + GetCurrentAllocatedBytes();
}


Zone* ZonePool::NewEmptyZone() {
  Zone* zone;
  // Grab a zone from pool if possible.
  if (!unused_.empty()) {
    zone = unused_.back();
    unused_.pop_back();
  } else {
106
    zone = new Zone();
107 108
  }
  used_.push_back(zone);
109
  DCHECK_EQ(0u, zone->allocation_size());
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  return zone;
}


void ZonePool::ReturnZone(Zone* zone) {
  size_t current_total = GetCurrentAllocatedBytes();
  // Update max.
  max_allocated_bytes_ = std::max(max_allocated_bytes_, current_total);
  // Update stats.
  for (auto stat_scope : stats_) {
    stat_scope->ZoneReturned(zone);
  }
  // Remove from used.
  Used::iterator it = std::find(used_.begin(), used_.end(), zone);
  DCHECK(it != used_.end());
  used_.erase(it);
  total_deleted_bytes_ += static_cast<size_t>(zone->allocation_size());
  // Delete zone or clear and stash on unused_.
  if (unused_.size() >= kMaxUnusedSize) {
    delete zone;
  } else {
    zone->DeleteAll();
132
    DCHECK_EQ(0u, zone->allocation_size());
133 134 135 136 137 138 139
    unused_.push_back(zone);
  }
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8