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

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

Issue 2335343007: Pool implementation for zone segments (Closed)
Patch Set: Fixed windows build 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. MIPS also requires 8-byte 63 // will be 8. ASan requires 8-byte alignment. MIPS also requires 8-byte
69 // alignment. 64 // alignment.
70 #if defined(V8_USE_ADDRESS_SANITIZER) || defined(V8_TARGET_ARCH_MIPS) 65 #if defined(V8_USE_ADDRESS_SANITIZER) || defined(V8_TARGET_ARCH_MIPS)
71 static const size_t kAlignment = 8; 66 static const size_t kAlignment = 8;
72 STATIC_ASSERT(kPointerSize <= 8); 67 STATIC_ASSERT(kPointerSize <= 8);
73 #else 68 #else
74 static const size_t kAlignment = kPointerSize; 69 static const size_t kAlignment = kPointerSize;
75 #endif 70 #endif
76 71
77 // Never allocate segments smaller than this size in bytes. 72 // Never allocate segments smaller than this size in bytes.
78 static const size_t kMinimumSegmentSize = 8 * KB; 73 static const size_t kMinimumSegmentSize = 8 * KB;
79 74
80 // Never allocate segments larger than this size in bytes. 75 // Never allocate segments larger than this size in bytes.
81 static const size_t kMaximumSegmentSize = 1 * MB; 76 static const size_t kMaximumSegmentSize = 1 * MB;
82 77
83 // Never keep segments larger than this size in bytes around.
84 static const size_t kMaximumKeptSegmentSize = 64 * KB;
85
86 // Report zone excess when allocation exceeds this limit. 78 // Report zone excess when allocation exceeds this limit.
87 static const size_t kExcessLimit = 256 * MB; 79 static const size_t kExcessLimit = 256 * MB;
88 80
89 // The number of bytes allocated in this zone so far. 81 // The number of bytes allocated in this zone so far.
90 size_t allocation_size_; 82 size_t allocation_size_;
91 83
92 // The number of bytes allocated in segments. Note that this number 84 // The number of bytes allocated in segments. Note that this number
93 // includes memory allocated from the OS but not yet allocated from 85 // includes memory allocated from the OS but not yet allocated from
94 // the zone. 86 // the zone.
95 size_t segment_bytes_allocated_; 87 size_t segment_bytes_allocated_;
96 88
97 // Expand the Zone to hold at least 'size' more bytes and allocate 89 // Expand the Zone to hold at least 'size' more bytes and allocate
98 // the bytes. Returns the address of the newly allocated chunk of 90 // the bytes. Returns the address of the newly allocated chunk of
99 // memory in the Zone. Should only be called if there isn't enough 91 // memory in the Zone. Should only be called if there isn't enough
100 // room in the Zone already. 92 // room in the Zone already.
101 Address NewExpand(size_t size); 93 Address NewExpand(size_t size);
102 94
103 // Creates a new segment, sets it size, and pushes it to the front 95 // Creates a new segment, sets it size, and pushes it to the front
104 // of the segment chain. Returns the new segment. 96 // of the segment chain. Returns the new segment.
105 inline Segment* NewSegment(size_t size); 97 inline Segment* NewSegment(size_t requested_size);
106 98
107 // The free region in the current (front) segment is represented as 99 // The free region in the current (front) segment is represented as
108 // the half-open interval [position, limit). The 'position' variable 100 // the half-open interval [position, limit). The 'position' variable
109 // is guaranteed to be aligned as dictated by kAlignment. 101 // is guaranteed to be aligned as dictated by kAlignment.
110 Address position_; 102 Address position_;
111 Address limit_; 103 Address limit_;
112 104
113 AccountingAllocator* allocator_; 105 AccountingAllocator* allocator_;
114 106
115 Segment* segment_head_; 107 Segment* segment_head_;
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 227
236 typedef base::PointerTemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap; 228 typedef base::PointerTemplateHashMapImpl<ZoneAllocationPolicy> ZoneHashMap;
237 229
238 typedef base::CustomMatcherTemplateHashMapImpl<ZoneAllocationPolicy> 230 typedef base::CustomMatcherTemplateHashMapImpl<ZoneAllocationPolicy>
239 CustomMatcherZoneHashMap; 231 CustomMatcherZoneHashMap;
240 232
241 } // namespace internal 233 } // namespace internal
242 } // namespace v8 234 } // namespace v8
243 235
244 #endif // V8_ZONE_ZONE_H_ 236 #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