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 AccountingAllocator { | 20 class 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 pool or creates a new one. |
26 virtual void FreeSegment(Segment* memory); | 26 virtual Segment* GetSegment(size_t bytes); |
27 // Return non needed segments here to insert them into the pool | |
28 // or release them in case the pool is full. Operation may or may | |
29 // not be asynchronous, depending on memory pressure. | |
30 virtual void ReturnSegment(Segment* memory); | |
27 | 31 |
28 size_t GetCurrentMemoryUsage() const; | 32 size_t GetCurrentMemoryUsage() const; |
29 size_t GetMaxMemoryUsage() const; | 33 size_t GetMaxMemoryUsage() const; |
30 | 34 |
35 size_t GetCurrentPoolSize() const; | |
36 | |
37 void MemoryPressureNotification(MemoryPressureLevel level); | |
38 | |
31 private: | 39 private: |
40 static const uint8_t kMinSegmentSizePower = 13; | |
41 static const uint8_t kMaxSegmentSizePower = 18; | |
42 static const uint8_t kMaxSegmentsPerBucket = 5; | |
43 | |
44 STATIC_ASSERT(kMinSegmentSizePower <= kMaxSegmentSizePower); | |
45 | |
46 // Allocated a new segment. Returns nullptr on failed allocation. | |
47 Segment* AllocateSegment(size_t bytes); | |
48 // Frees a non needed segment. Blocks the current thread. | |
49 void FreeSegment(Segment* memory); | |
50 | |
51 // Returns a segment from the pool of at least the requested size. | |
52 Segment* GetSegmentFromPool(size_t requested_size); | |
53 // Trys to add a segment to the pool. Returns false if the pool is full. | |
54 bool AddSegmentToPool(Segment* segment); | |
55 | |
56 // Empties the pool and puts all its contents onto the garbage stack. | |
57 void ClearPool(); | |
58 | |
59 Segment** unused_segments_heads_ = | |
Toon Verwaest
2016/09/21 11:18:07
Segment* unused_segments_heads_[1+kMax-kMin], same
heimbuef
2016/09/21 11:39:23
Format checker does not like no spaces in between
| |
60 new Segment*[1 + kMaxSegmentSizePower - kMinSegmentSizePower]; | |
61 | |
62 size_t* unused_segments_sizes = | |
63 new size_t[1 + kMaxSegmentSizePower - kMinSegmentSizePower]; | |
64 | |
65 size_t unused_segments_size_ = 0; | |
66 | |
67 base::Mutex* unused_segments_mutex_ = new base::Mutex(); | |
68 | |
32 base::AtomicWord current_memory_usage_ = 0; | 69 base::AtomicWord current_memory_usage_ = 0; |
33 base::AtomicWord max_memory_usage_ = 0; | 70 base::AtomicWord max_memory_usage_ = 0; |
34 | 71 |
72 base::AtomicValue<MemoryPressureLevel> memory_pressure_level_; | |
73 | |
35 DISALLOW_COPY_AND_ASSIGN(AccountingAllocator); | 74 DISALLOW_COPY_AND_ASSIGN(AccountingAllocator); |
36 }; | 75 }; |
37 | 76 |
38 } // namespace internal | 77 } // namespace internal |
39 } // namespace v8 | 78 } // namespace v8 |
40 | 79 |
41 #endif // V8_ZONE_ACCOUNTING_ALLOCATOR_H_ | 80 #endif // V8_ZONE_ACCOUNTING_ALLOCATOR_H_ |
OLD | NEW |