OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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_ACCOUNTING_ALLOCATOR_H_ | 5 #ifndef V8_ZONE_ACCOUNTING_ALLOCATOR_H_ |
6 #define V8_ZONE_ACCOUNTING_ALLOCATOR_H_ | 6 #define V8_ZONE_ACCOUNTING_ALLOCATOR_H_ |
7 | 7 |
8 #include "include/v8-platform.h" | 8 #include "include/v8-platform.h" |
9 #include "src/base/atomic-utils.h" | 9 #include "src/base/atomic-utils.h" |
10 #include "src/base/atomicops.h" | 10 #include "src/base/atomicops.h" |
11 #include "src/base/macros.h" | 11 #include "src/base/macros.h" |
12 #include "src/base/platform/mutex.h" | 12 #include "src/base/platform/mutex.h" |
13 #include "src/base/platform/semaphore.h" | 13 #include "src/base/platform/semaphore.h" |
14 #include "src/base/platform/time.h" | 14 #include "src/base/platform/time.h" |
15 #include "src/zone/zone-segment.h" | 15 #include "src/zone/zone-segment.h" |
16 | 16 |
17 namespace v8 { | 17 namespace v8 { |
18 namespace internal { | 18 namespace internal { |
19 | 19 |
20 class V8_EXPORT_PRIVATE AccountingAllocator { | 20 class V8_EXPORT_PRIVATE AccountingAllocator { |
21 public: | 21 public: |
22 AccountingAllocator() = default; | 22 AccountingAllocator(); |
23 virtual ~AccountingAllocator() = default; | 23 virtual ~AccountingAllocator(); |
24 | 24 |
25 virtual Segment* AllocateSegment(size_t bytes); | 25 // Gets an empty segment from the pool or creates a new one. |
26 virtual void FreeSegment(Segment* memory); | 26 virtual Segment* GetSegment(size_t bytes); |
| 27 // Return unneeded segments to either insert them into the pool or release |
| 28 // them if the pool is already full or memory pressure is high. |
| 29 virtual void ReturnSegment(Segment* memory); |
27 | 30 |
28 size_t GetCurrentMemoryUsage() const; | 31 size_t GetCurrentMemoryUsage() const; |
29 size_t GetMaxMemoryUsage() const; | 32 size_t GetMaxMemoryUsage() const; |
30 | 33 |
| 34 size_t GetCurrentPoolSize() const; |
| 35 |
| 36 void MemoryPressureNotification(MemoryPressureLevel level); |
| 37 |
31 private: | 38 private: |
| 39 static const uint8_t kMinSegmentSizePower = 13; |
| 40 static const uint8_t kMaxSegmentSizePower = 18; |
| 41 static const uint8_t kMaxSegmentsPerBucket = 5; |
| 42 |
| 43 STATIC_ASSERT(kMinSegmentSizePower <= kMaxSegmentSizePower); |
| 44 |
| 45 // Allocates a new segment. Returns nullptr on failed allocation. |
| 46 Segment* AllocateSegment(size_t bytes); |
| 47 void FreeSegment(Segment* memory); |
| 48 |
| 49 // Returns a segment from the pool of at least the requested size. |
| 50 Segment* GetSegmentFromPool(size_t requested_size); |
| 51 // Trys to add a segment to the pool. Returns false if the pool is full. |
| 52 bool AddSegmentToPool(Segment* segment); |
| 53 |
| 54 // Empties the pool and puts all its contents onto the garbage stack. |
| 55 void ClearPool(); |
| 56 |
| 57 Segment* |
| 58 unused_segments_heads_[1 + kMaxSegmentSizePower - kMinSegmentSizePower]; |
| 59 |
| 60 size_t unused_segments_sizes[1 + kMaxSegmentSizePower - kMinSegmentSizePower]; |
| 61 |
| 62 size_t unused_segments_size_ = 0; |
| 63 |
| 64 base::Mutex unused_segments_mutex_; |
| 65 |
32 base::AtomicWord current_memory_usage_ = 0; | 66 base::AtomicWord current_memory_usage_ = 0; |
33 base::AtomicWord max_memory_usage_ = 0; | 67 base::AtomicWord max_memory_usage_ = 0; |
34 | 68 |
| 69 base::AtomicValue<MemoryPressureLevel> memory_pressure_level_; |
| 70 |
35 DISALLOW_COPY_AND_ASSIGN(AccountingAllocator); | 71 DISALLOW_COPY_AND_ASSIGN(AccountingAllocator); |
36 }; | 72 }; |
37 | 73 |
38 } // namespace internal | 74 } // namespace internal |
39 } // namespace v8 | 75 } // namespace v8 |
40 | 76 |
41 #endif // V8_ZONE_ACCOUNTING_ALLOCATOR_H_ | 77 #endif // V8_ZONE_ACCOUNTING_ALLOCATOR_H_ |
OLD | NEW |