Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: src/zone/zone.h

Issue 2335343007: Pool implementation for zone segments (Closed)
Patch Set: Reaction to comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/zone/accounting-allocator.cc ('k') | src/zone/zone.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_ZONE_ZONE_H_ 5 #ifndef V8_ZONE_ZONE_H_
6 #define V8_ZONE_ZONE_H_ 6 #define V8_ZONE_ZONE_H_
7 7
8 #include <limits> 8 #include <limits>
9 9
10 #include "src/base/hashmap.h" 10 #include "src/base/hashmap.h"
11 #include "src/base/logging.h" 11 #include "src/base/logging.h"
12 #include "src/globals.h" 12 #include "src/globals.h"
13 #include "src/list.h" 13 #include "src/list.h"
14 #include "src/splay-tree.h" 14 #include "src/splay-tree.h"
15 #include "src/zone/accounting-allocator.h" 15 #include "src/zone/accounting-allocator.h"
16 16
17 namespace v8 { 17 namespace v8 {
18 namespace internal { 18 namespace internal {
19 19
20 // The Zone supports very fast allocation of small chunks of 20 // The Zone supports very fast allocation of small chunks of
21 // memory. The chunks cannot be deallocated individually, but instead 21 // memory. The chunks cannot be deallocated individually, but instead
22 // the Zone supports deallocating all chunks in one fast 22 // the Zone supports deallocating all chunks in one fast
23 // operation. The Zone is used to hold temporary data structures like 23 // operation. The Zone is used to hold temporary data structures like
24 // the abstract syntax tree, which is deallocated after compilation. 24 // the abstract syntax tree, which is deallocated after compilation.
25 // 25 //
26 // Note: There is no need to initialize the Zone; the first time an 26 // Note: There is no need to initialize the Zone; the first time an
27 // allocation is attempted, a segment of memory will be requested 27 // allocation is attempted, a segment of memory will be requested
28 // through a call to malloc(). 28 // through the allocator.
29 // 29 //
30 // Note: The implementation is inherently not thread safe. Do not use 30 // Note: The implementation is inherently not thread safe. Do not use
31 // from multi-threaded code. 31 // from multi-threaded code.
32 class V8_EXPORT_PRIVATE Zone final { 32 class V8_EXPORT_PRIVATE Zone final {
33 public: 33 public:
34 explicit Zone(AccountingAllocator* allocator); 34 explicit Zone(AccountingAllocator* allocator);
35 ~Zone(); 35 ~Zone();
36 36
37 // Allocate 'size' bytes of memory in the Zone; expands the Zone by 37 // Allocate 'size' bytes of memory in the Zone; expands the Zone by
38 // allocating new segments of memory on demand using malloc(). 38 // allocating new segments of memory on demand using malloc().
39 void* New(size_t size); 39 void* New(size_t size);
40 40
41 template <typename T> 41 template <typename T>
42 T* NewArray(size_t length) { 42 T* NewArray(size_t length) {
43 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T)); 43 DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T));
44 return static_cast<T*>(New(length * sizeof(T))); 44 return static_cast<T*>(New(length * sizeof(T)));
45 } 45 }
46 46
47 // Deletes all objects and free all memory allocated in the Zone. Keeps one 47 // Deletes all objects and free all memory allocated in the Zone.
48 // small (size <= kMaximumKeptSegmentSize) segment around if it finds one.
49 void DeleteAll(); 48 void DeleteAll();
50 49
51 // Deletes the last small segment kept around by DeleteAll(). You
52 // may no longer allocate in the Zone after a call to this method.
53 void DeleteKeptSegment();
54
55 // Returns true if more memory has been allocated in zones than 50 // Returns true if more memory has been allocated in zones than
56 // the limit allows. 51 // the limit allows.
57 bool excess_allocation() const { 52 bool excess_allocation() const {
58 return segment_bytes_allocated_ > kExcessLimit; 53 return segment_bytes_allocated_ > kExcessLimit;
59 } 54 }
60 55
61 size_t allocation_size() const { return allocation_size_; } 56 size_t allocation_size() const { return allocation_size_; }
62 57
63 AccountingAllocator* allocator() const { return allocator_; } 58 AccountingAllocator* allocator() const { return allocator_; }
64 59
65 private: 60 private:
66 // All pointers returned from New() have this alignment. In addition, if the 61 // All pointers returned from New() have this alignment. In addition, if the
67 // object being allocated has a size that is divisible by 8 then its alignment 62 // object being allocated has a size that is divisible by 8 then its alignment
68 // will be 8. ASan requires 8-byte alignment. 63 // will be 8. ASan requires 8-byte alignment.
69 #ifdef V8_USE_ADDRESS_SANITIZER 64 #ifdef V8_USE_ADDRESS_SANITIZER
70 static const size_t kAlignment = 8; 65 static const size_t kAlignment = 8;
71 STATIC_ASSERT(kPointerSize <= 8); 66 STATIC_ASSERT(kPointerSize <= 8);
72 #else 67 #else
73 static const size_t kAlignment = kPointerSize; 68 static const size_t kAlignment = kPointerSize;
74 #endif 69 #endif
75 70
76 // Never allocate segments smaller than this size in bytes. 71 // Never allocate segments smaller than this size in bytes.
77 static const size_t kMinimumSegmentSize = 8 * KB; 72 static const size_t kMinimumSegmentSize = 8 * KB;
78 73
79 // Never allocate segments larger than this size in bytes. 74 // Never allocate segments larger than this size in bytes.
80 static const size_t kMaximumSegmentSize = 1 * MB; 75 static const size_t kMaximumSegmentSize = 1 * MB;
81 76
82 // Never keep segments larger than this size in bytes around.
83 static const size_t kMaximumKeptSegmentSize = 64 * KB;
84
85 // Report zone excess when allocation exceeds this limit. 77 // Report zone excess when allocation exceeds this limit.
86 static const size_t kExcessLimit = 256 * MB; 78 static const size_t kExcessLimit = 256 * MB;
87 79
88 // The number of bytes allocated in this zone so far. 80 // The number of bytes allocated in this zone so far.
89 size_t allocation_size_; 81 size_t allocation_size_;
90 82
91 // The number of bytes allocated in segments. Note that this number 83 // The number of bytes allocated in segments. Note that this number
92 // includes memory allocated from the OS but not yet allocated from 84 // includes memory allocated from the OS but not yet allocated from
93 // the zone. 85 // the zone.
94 size_t segment_bytes_allocated_; 86 size_t segment_bytes_allocated_;
95 87
96 // Expand the Zone to hold at least 'size' more bytes and allocate 88 // Expand the Zone to hold at least 'size' more bytes and allocate
97 // the bytes. Returns the address of the newly allocated chunk of 89 // the bytes. Returns the address of the newly allocated chunk of
98 // memory in the Zone. Should only be called if there isn't enough 90 // memory in the Zone. Should only be called if there isn't enough
99 // room in the Zone already. 91 // room in the Zone already.
100 Address NewExpand(size_t size); 92 Address NewExpand(size_t size);
101 93
102 // Creates a new segment, sets it size, and pushes it to the front 94 // Creates a new segment, sets it size, and pushes it to the front
103 // of the segment chain. Returns the new segment. 95 // of the segment chain. Returns the new segment.
104 inline Segment* NewSegment(size_t size); 96 inline Segment* NewSegment(size_t size);
Jakob Kummerow 2016/10/06 13:53:39 nit: please apply the "requested_size" renaming he
105 97
106 // The free region in the current (front) segment is represented as 98 // The free region in the current (front) segment is represented as
107 // the half-open interval [position, limit). The 'position' variable 99 // the half-open interval [position, limit). The 'position' variable
108 // is guaranteed to be aligned as dictated by kAlignment. 100 // is guaranteed to be aligned as dictated by kAlignment.
109 Address position_; 101 Address position_;
110 Address limit_; 102 Address limit_;
111 103
112 AccountingAllocator* allocator_; 104 AccountingAllocator* allocator_;
113 105
114 Segment* segment_head_; 106 Segment* segment_head_;
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 226
235 typedef base::PointerTemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; 227 typedef base::PointerTemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap;
236 228
237 typedef base::CustomMatcherTemplateHashMapImpl<ZoneAllocationPolicy> 229 typedef base::CustomMatcherTemplateHashMapImpl<ZoneAllocationPolicy>
238 CustomMatcherZoneHashMap; 230 CustomMatcherZoneHashMap;
239 231
240 } // namespace internal 232 } // namespace internal
241 } // namespace v8 233 } // namespace v8
242 234
243 #endif // V8_ZONE_ZONE_H_ 235 #endif // V8_ZONE_ZONE_H_
OLDNEW
« no previous file with comments | « src/zone/accounting-allocator.cc ('k') | src/zone/zone.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698