]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/scudo/scudo_allocator_combined.h
Upgrade Unbound to 1.6.4. More to follow.
[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   // Primary allocations are always MinAlignment aligned, and as such do not
33   // require an Alignment parameter.
34   void *allocatePrimary(AllocatorCache *Cache, uptr ClassId) {
35     return Cache->Allocate(&Primary, ClassId);
36   }
37
38   // Secondary allocations do not require a Cache, but do require an Alignment
39   // parameter.
40   void *allocateSecondary(uptr Size, uptr Alignment) {
41     return Secondary.Allocate(&Stats, Size, Alignment);
42   }
43
44   void deallocatePrimary(AllocatorCache *Cache, void *Ptr, uptr ClassId) {
45     Cache->Deallocate(&Primary, ClassId, Ptr);
46   }
47
48   void deallocateSecondary(void *Ptr) {
49     Secondary.Deallocate(&Stats, Ptr);
50   }
51
52   uptr getActuallyAllocatedSize(void *Ptr, uptr ClassId) {
53     if (ClassId)
54       return PrimaryAllocator::ClassIdToSize(ClassId);
55     return Secondary.GetActuallyAllocatedSize(Ptr);
56   }
57
58   void initCache(AllocatorCache *Cache) {
59     Cache->Init(&Stats);
60   }
61
62   void destroyCache(AllocatorCache *Cache) {
63     Cache->Destroy(&Primary, &Stats);
64   }
65
66   void getStats(AllocatorStatCounters StatType) const {
67     Stats.Get(StatType);
68   }
69
70  private:
71   PrimaryAllocator Primary;
72   SecondaryAllocator Secondary;
73   AllocatorGlobalStats Stats;
74 };
75
76 #endif  // SCUDO_ALLOCATOR_COMBINED_H_