1 //===-- scudo_allocator_combined.h ------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// Scudo Combined Allocator, dispatches allocation & deallocation requests to
10 /// the Primary or the Secondary backend allocators.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef SCUDO_ALLOCATOR_COMBINED_H_
15 #define SCUDO_ALLOCATOR_COMBINED_H_
16 
17 #ifndef SCUDO_ALLOCATOR_H_
18 # error "This file must be included inside scudo_allocator.h."
19 #endif
20 
21 class CombinedAllocator {
22  public:
23   using PrimaryAllocator = PrimaryT;
24   using SecondaryAllocator = SecondaryT;
25   using AllocatorCache = typename PrimaryAllocator::AllocatorCache;
init(s32 ReleaseToOSIntervalMs)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.
allocatePrimary(AllocatorCache * Cache,uptr ClassId)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.
allocateSecondary(uptr Size,uptr Alignment)40   void *allocateSecondary(uptr Size, uptr Alignment) {
41     return Secondary.Allocate(&Stats, Size, Alignment);
42   }
43 
deallocatePrimary(AllocatorCache * Cache,void * Ptr,uptr ClassId)44   void deallocatePrimary(AllocatorCache *Cache, void *Ptr, uptr ClassId) {
45     Cache->Deallocate(&Primary, ClassId, Ptr);
46   }
47 
deallocateSecondary(void * Ptr)48   void deallocateSecondary(void *Ptr) {
49     Secondary.Deallocate(&Stats, Ptr);
50   }
51 
initCache(AllocatorCache * Cache)52   void initCache(AllocatorCache *Cache) {
53     Cache->Init(&Stats);
54   }
55 
destroyCache(AllocatorCache * Cache)56   void destroyCache(AllocatorCache *Cache) {
57     Cache->Destroy(&Primary, &Stats);
58   }
59 
getStats(AllocatorStatCounters StatType)60   void getStats(AllocatorStatCounters StatType) const {
61     Stats.Get(StatType);
62   }
63 
printStats()64   void printStats() {
65     Primary.PrintStats();
66     Secondary.PrintStats();
67   }
68 
69  private:
70   PrimaryAllocator Primary;
71   SecondaryAllocator Secondary;
72   AllocatorGlobalStats Stats;
73 };
74 
75 #endif  // SCUDO_ALLOCATOR_COMBINED_H_
76