2017-05-12 05:40:45 +08:00
|
|
|
//===-- scudo_allocator_combined.h ------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-12 05:40:45 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// Scudo Combined Allocator, dispatches allocation & deallocation requests to
|
|
|
|
/// the Primary or the Secondary backend allocators.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef SCUDO_ALLOCATOR_COMBINED_H_
|
|
|
|
#define SCUDO_ALLOCATOR_COMBINED_H_
|
|
|
|
|
|
|
|
#ifndef SCUDO_ALLOCATOR_H_
|
2018-07-20 23:07:17 +08:00
|
|
|
# error "This file must be included inside scudo_allocator.h."
|
2017-05-12 05:40:45 +08:00
|
|
|
#endif
|
|
|
|
|
2018-07-20 23:07:17 +08:00
|
|
|
class CombinedAllocator {
|
2017-05-12 05:40:45 +08:00
|
|
|
public:
|
2019-05-02 03:36:26 +08:00
|
|
|
using PrimaryAllocator = PrimaryT;
|
|
|
|
using SecondaryAllocator = SecondaryT;
|
2019-05-02 03:30:49 +08:00
|
|
|
using AllocatorCache = typename PrimaryAllocator::AllocatorCache;
|
2017-07-14 05:01:19 +08:00
|
|
|
void init(s32 ReleaseToOSIntervalMs) {
|
2017-05-12 05:40:45 +08:00
|
|
|
Primary.Init(ReleaseToOSIntervalMs);
|
2017-06-21 05:23:02 +08:00
|
|
|
Secondary.Init();
|
2017-05-12 05:40:45 +08:00
|
|
|
Stats.Init();
|
|
|
|
}
|
|
|
|
|
2017-07-14 05:01:19 +08:00
|
|
|
// Primary allocations are always MinAlignment aligned, and as such do not
|
|
|
|
// require an Alignment parameter.
|
2017-12-06 01:08:29 +08:00
|
|
|
void *allocatePrimary(AllocatorCache *Cache, uptr ClassId) {
|
|
|
|
return Cache->Allocate(&Primary, ClassId);
|
2017-07-14 05:01:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Secondary allocations do not require a Cache, but do require an Alignment
|
|
|
|
// parameter.
|
|
|
|
void *allocateSecondary(uptr Size, uptr Alignment) {
|
2017-05-12 05:40:45 +08:00
|
|
|
return Secondary.Allocate(&Stats, Size, Alignment);
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:08:29 +08:00
|
|
|
void deallocatePrimary(AllocatorCache *Cache, void *Ptr, uptr ClassId) {
|
|
|
|
Cache->Deallocate(&Primary, ClassId, Ptr);
|
2017-07-14 05:01:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void deallocateSecondary(void *Ptr) {
|
|
|
|
Secondary.Deallocate(&Stats, Ptr);
|
2017-05-12 05:40:45 +08:00
|
|
|
}
|
|
|
|
|
2017-07-14 05:01:19 +08:00
|
|
|
void initCache(AllocatorCache *Cache) {
|
2017-05-12 05:40:45 +08:00
|
|
|
Cache->Init(&Stats);
|
|
|
|
}
|
|
|
|
|
2017-07-14 05:01:19 +08:00
|
|
|
void destroyCache(AllocatorCache *Cache) {
|
2017-05-12 05:40:45 +08:00
|
|
|
Cache->Destroy(&Primary, &Stats);
|
|
|
|
}
|
|
|
|
|
2017-07-14 05:01:19 +08:00
|
|
|
void getStats(AllocatorStatCounters StatType) const {
|
2017-05-12 05:40:45 +08:00
|
|
|
Stats.Get(StatType);
|
|
|
|
}
|
|
|
|
|
2018-04-26 02:52:29 +08:00
|
|
|
void printStats() {
|
|
|
|
Primary.PrintStats();
|
|
|
|
Secondary.PrintStats();
|
|
|
|
}
|
|
|
|
|
2017-05-12 05:40:45 +08:00
|
|
|
private:
|
|
|
|
PrimaryAllocator Primary;
|
|
|
|
SecondaryAllocator Secondary;
|
|
|
|
AllocatorGlobalStats Stats;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SCUDO_ALLOCATOR_COMBINED_H_
|