scudo_allocator_combined.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifndef SCUDO_ALLOCATOR_COMBINED_H_
  14. #define SCUDO_ALLOCATOR_COMBINED_H_
  15. #ifndef SCUDO_ALLOCATOR_H_
  16. # error "This file must be included inside scudo_allocator.h."
  17. #endif
  18. class CombinedAllocator {
  19. public:
  20. using PrimaryAllocator = PrimaryT;
  21. using SecondaryAllocator = SecondaryT;
  22. using AllocatorCache = typename PrimaryAllocator::AllocatorCache;
  23. void init(s32 ReleaseToOSIntervalMs) {
  24. Primary.Init(ReleaseToOSIntervalMs);
  25. Secondary.Init();
  26. Stats.Init();
  27. }
  28. // Primary allocations are always MinAlignment aligned, and as such do not
  29. // require an Alignment parameter.
  30. void *allocatePrimary(AllocatorCache *Cache, uptr ClassId) {
  31. return Cache->Allocate(&Primary, ClassId);
  32. }
  33. // Secondary allocations do not require a Cache, but do require an Alignment
  34. // parameter.
  35. void *allocateSecondary(uptr Size, uptr Alignment) {
  36. return Secondary.Allocate(&Stats, Size, Alignment);
  37. }
  38. void deallocatePrimary(AllocatorCache *Cache, void *Ptr, uptr ClassId) {
  39. Cache->Deallocate(&Primary, ClassId, Ptr);
  40. }
  41. void deallocateSecondary(void *Ptr) {
  42. Secondary.Deallocate(&Stats, Ptr);
  43. }
  44. void initCache(AllocatorCache *Cache) {
  45. Cache->Init(&Stats);
  46. }
  47. void destroyCache(AllocatorCache *Cache) {
  48. Cache->Destroy(&Primary, &Stats);
  49. }
  50. void getStats(AllocatorStatCounters StatType) const {
  51. Stats.Get(StatType);
  52. }
  53. void printStats() {
  54. Primary.PrintStats();
  55. Secondary.PrintStats();
  56. }
  57. private:
  58. PrimaryAllocator Primary;
  59. SecondaryAllocator Secondary;
  60. AllocatorGlobalStats Stats;
  61. };
  62. #endif // SCUDO_ALLOCATOR_COMBINED_H_