]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/scudo/scudo_allocator_combined.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / scudo / scudo_allocator_combined.h
1 //===-- scudo_allocator_combined.h ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// Scudo Combined Allocator, dispatches allocation & deallocation requests to
11 /// the Primary or the Secondary backend allocators.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef SCUDO_ALLOCATOR_COMBINED_H_
16 #define SCUDO_ALLOCATOR_COMBINED_H_
17
18 #ifndef SCUDO_ALLOCATOR_H_
19 #error "This file must be included inside scudo_allocator.h."
20 #endif
21
22 template <class PrimaryAllocator, class AllocatorCache,
23     class SecondaryAllocator>
24 class ScudoCombinedAllocator {
25  public:
26   void Init(s32 ReleaseToOSIntervalMs) {
27     Primary.Init(ReleaseToOSIntervalMs);
28     Secondary.Init();
29     Stats.Init();
30   }
31
32   void *Allocate(AllocatorCache *Cache, uptr Size, uptr Alignment,
33                  bool FromPrimary) {
34     if (FromPrimary)
35       return Cache->Allocate(&Primary, Primary.ClassID(Size));
36     return Secondary.Allocate(&Stats, Size, Alignment);
37   }
38
39   void Deallocate(AllocatorCache *Cache, void *Ptr, bool FromPrimary) {
40     if (FromPrimary)
41       Cache->Deallocate(&Primary, Primary.GetSizeClass(Ptr), Ptr);
42     else
43       Secondary.Deallocate(&Stats, Ptr);
44   }
45
46   uptr GetActuallyAllocatedSize(void *Ptr, bool FromPrimary) {
47     if (FromPrimary)
48       return PrimaryAllocator::ClassIdToSize(Primary.GetSizeClass(Ptr));
49     return Secondary.GetActuallyAllocatedSize(Ptr);
50   }
51
52   void InitCache(AllocatorCache *Cache) {
53     Cache->Init(&Stats);
54   }
55
56   void DestroyCache(AllocatorCache *Cache) {
57     Cache->Destroy(&Primary, &Stats);
58   }
59
60   void GetStats(AllocatorStatCounters StatType) const {
61     Stats.Get(StatType);
62   }
63
64  private:
65   PrimaryAllocator Primary;
66   SecondaryAllocator Secondary;
67   AllocatorGlobalStats Stats;
68 };
69
70 #endif  // SCUDO_ALLOCATOR_COMBINED_H_