blob: 44a649fcfb45d15c66ee971963c60fdbf5e5b283 [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#ifndef V8_COMPILER_ZONE_POOL_H_
6#define V8_COMPILER_ZONE_POOL_H_
7
8#include <map>
9#include <set>
10#include <vector>
11
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/zone.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013
14namespace v8 {
15namespace internal {
16namespace compiler {
17
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018class ZonePool final {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020 class Scope final {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040021 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022 explicit Scope(ZonePool* zone_pool)
23 : zone_pool_(zone_pool), zone_(nullptr) {}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040024 ~Scope() { Destroy(); }
25
26 Zone* zone() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027 if (zone_ == nullptr) zone_ = zone_pool_->NewEmptyZone();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028 return zone_;
29 }
30 void Destroy() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031 if (zone_ != nullptr) zone_pool_->ReturnZone(zone_);
32 zone_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040033 }
34
35 private:
36 ZonePool* const zone_pool_;
37 Zone* zone_;
38 DISALLOW_COPY_AND_ASSIGN(Scope);
39 };
40
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 class StatsScope final {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040042 public:
43 explicit StatsScope(ZonePool* zone_pool);
44 ~StatsScope();
45
46 size_t GetMaxAllocatedBytes();
47 size_t GetCurrentAllocatedBytes();
48 size_t GetTotalAllocatedBytes();
49
50 private:
51 friend class ZonePool;
52 void ZoneReturned(Zone* zone);
53
54 typedef std::map<Zone*, size_t> InitialValues;
55
56 ZonePool* const zone_pool_;
57 InitialValues initial_values_;
58 size_t total_allocated_bytes_at_start_;
59 size_t max_allocated_bytes_;
60
61 DISALLOW_COPY_AND_ASSIGN(StatsScope);
62 };
63
Ben Murdochda12d292016-06-02 14:46:10 +010064 explicit ZonePool(base::AccountingAllocator* allocator);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040065 ~ZonePool();
66
67 size_t GetMaxAllocatedBytes();
68 size_t GetTotalAllocatedBytes();
69 size_t GetCurrentAllocatedBytes();
70
71 private:
72 Zone* NewEmptyZone();
73 void ReturnZone(Zone* zone);
74
75 static const size_t kMaxUnusedSize = 3;
76 typedef std::vector<Zone*> Unused;
77 typedef std::vector<Zone*> Used;
78 typedef std::vector<StatsScope*> Stats;
79
Emily Bernierd0a1eb72015-03-24 16:35:39 -040080 Unused unused_;
81 Used used_;
82 Stats stats_;
83 size_t max_allocated_bytes_;
84 size_t total_deleted_bytes_;
Ben Murdochda12d292016-06-02 14:46:10 +010085 base::AccountingAllocator* allocator_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040086
87 DISALLOW_COPY_AND_ASSIGN(ZonePool);
88};
89
90} // namespace compiler
91} // namespace internal
92} // namespace v8
93
94#endif