blob: 8b43265c1a8ae24605de403c82642243568ecad7 [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
12#include "src/v8.h"
13
14namespace v8 {
15namespace internal {
16namespace compiler {
17
18class ZonePool FINAL {
19 public:
20 class Scope FINAL {
21 public:
22 explicit Scope(ZonePool* zone_pool) : zone_pool_(zone_pool), zone_(NULL) {}
23 ~Scope() { Destroy(); }
24
25 Zone* zone() {
26 if (zone_ == NULL) zone_ = zone_pool_->NewEmptyZone();
27 return zone_;
28 }
29 void Destroy() {
30 if (zone_ != NULL) zone_pool_->ReturnZone(zone_);
31 zone_ = NULL;
32 }
33
34 private:
35 ZonePool* const zone_pool_;
36 Zone* zone_;
37 DISALLOW_COPY_AND_ASSIGN(Scope);
38 };
39
40 class StatsScope FINAL {
41 public:
42 explicit StatsScope(ZonePool* zone_pool);
43 ~StatsScope();
44
45 size_t GetMaxAllocatedBytes();
46 size_t GetCurrentAllocatedBytes();
47 size_t GetTotalAllocatedBytes();
48
49 private:
50 friend class ZonePool;
51 void ZoneReturned(Zone* zone);
52
53 typedef std::map<Zone*, size_t> InitialValues;
54
55 ZonePool* const zone_pool_;
56 InitialValues initial_values_;
57 size_t total_allocated_bytes_at_start_;
58 size_t max_allocated_bytes_;
59
60 DISALLOW_COPY_AND_ASSIGN(StatsScope);
61 };
62
63 explicit ZonePool(Isolate* isolate);
64 ~ZonePool();
65
66 size_t GetMaxAllocatedBytes();
67 size_t GetTotalAllocatedBytes();
68 size_t GetCurrentAllocatedBytes();
69
70 private:
71 Zone* NewEmptyZone();
72 void ReturnZone(Zone* zone);
73
74 static const size_t kMaxUnusedSize = 3;
75 typedef std::vector<Zone*> Unused;
76 typedef std::vector<Zone*> Used;
77 typedef std::vector<StatsScope*> Stats;
78
79 Isolate* const isolate_;
80 Unused unused_;
81 Used used_;
82 Stats stats_;
83 size_t max_allocated_bytes_;
84 size_t total_deleted_bytes_;
85
86 DISALLOW_COPY_AND_ASSIGN(ZonePool);
87};
88
89} // namespace compiler
90} // namespace internal
91} // namespace v8
92
93#endif