Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_ZONE_ACCOUNTING_ALLOCATOR_H_
6 : #define V8_ZONE_ACCOUNTING_ALLOCATOR_H_
7 :
8 : #include "include/v8-platform.h"
9 : #include "src/base/atomic-utils.h"
10 : #include "src/base/atomicops.h"
11 : #include "src/base/macros.h"
12 : #include "src/base/platform/mutex.h"
13 : #include "src/base/platform/semaphore.h"
14 : #include "src/base/platform/time.h"
15 : #include "src/zone/zone-segment.h"
16 : #include "testing/gtest/include/gtest/gtest_prod.h" // nogncheck
17 :
18 : namespace v8 {
19 : namespace internal {
20 :
21 : class V8_EXPORT_PRIVATE AccountingAllocator {
22 : public:
23 : static const size_t kMaxPoolSize = 8ul * KB;
24 :
25 : AccountingAllocator();
26 : virtual ~AccountingAllocator();
27 :
28 : // Gets an empty segment from the pool or creates a new one.
29 : virtual Segment* GetSegment(size_t bytes);
30 : // Return unneeded segments to either insert them into the pool or release
31 : // them if the pool is already full or memory pressure is high.
32 : virtual void ReturnSegment(Segment* memory);
33 :
34 : size_t GetCurrentMemoryUsage() const;
35 : size_t GetMaxMemoryUsage() const;
36 :
37 : size_t GetCurrentPoolSize() const;
38 :
39 : void MemoryPressureNotification(MemoryPressureLevel level);
40 : // Configures the zone segment pool size limits so the pool does not
41 : // grow bigger than max_pool_size.
42 : // TODO(heimbuef): Do not accept segments to pool that are larger than
43 : // their size class requires. Sometimes the zones generate weird segments.
44 : void ConfigureSegmentPool(const size_t max_pool_size);
45 :
46 55633118 : virtual void ZoneCreation(const Zone* zone) {}
47 55616098 : virtual void ZoneDestruction(const Zone* zone) {}
48 :
49 : private:
50 : FRIEND_TEST(Zone, SegmentPoolConstraints);
51 :
52 : static const size_t kMinSegmentSizePower = 13;
53 : static const size_t kMaxSegmentSizePower = 18;
54 :
55 : STATIC_ASSERT(kMinSegmentSizePower <= kMaxSegmentSizePower);
56 :
57 : static const size_t kNumberBuckets =
58 : 1 + kMaxSegmentSizePower - kMinSegmentSizePower;
59 :
60 : // Allocates a new segment. Returns nullptr on failed allocation.
61 : Segment* AllocateSegment(size_t bytes);
62 : void FreeSegment(Segment* memory);
63 :
64 : // Returns a segment from the pool of at least the requested size.
65 : Segment* GetSegmentFromPool(size_t requested_size);
66 : // Trys to add a segment to the pool. Returns false if the pool is full.
67 : bool AddSegmentToPool(Segment* segment);
68 :
69 : // Empties the pool and puts all its contents onto the garbage stack.
70 : void ClearPool();
71 :
72 : Segment* unused_segments_heads_[kNumberBuckets];
73 :
74 : size_t unused_segments_sizes_[kNumberBuckets];
75 : size_t unused_segments_max_sizes_[kNumberBuckets];
76 :
77 : base::Mutex unused_segments_mutex_;
78 :
79 : base::AtomicWord current_memory_usage_ = 0;
80 : base::AtomicWord max_memory_usage_ = 0;
81 : base::AtomicWord current_pool_size_ = 0;
82 :
83 : base::AtomicValue<MemoryPressureLevel> memory_pressure_level_;
84 :
85 : DISALLOW_COPY_AND_ASSIGN(AccountingAllocator);
86 : };
87 :
88 : } // namespace internal
89 : } // namespace v8
90 :
91 : #endif // V8_ZONE_ACCOUNTING_ALLOCATOR_H_
|