Recycler.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //==- llvm/Support/Recycler.h - Recycling Allocator --------------*- 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. // This file defines the Recycler class template. See the doxygen comment for
  10. // Recycler for more details.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_RECYCLER_H
  14. #define LLVM_SUPPORT_RECYCLER_H
  15. #include "llvm/ADT/ilist.h"
  16. #include "llvm/Support/Allocator.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include <cassert>
  19. namespace llvm {
  20. /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
  21. /// printing statistics.
  22. ///
  23. void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
  24. /// Recycler - This class manages a linked-list of deallocated nodes
  25. /// and facilitates reusing deallocated memory in place of allocating
  26. /// new memory.
  27. ///
  28. template <class T, size_t Size = sizeof(T), size_t Align = alignof(T)>
  29. class Recycler {
  30. struct FreeNode {
  31. FreeNode *Next;
  32. };
  33. /// List of nodes that have deleted contents and are not in active use.
  34. FreeNode *FreeList = nullptr;
  35. FreeNode *pop_val() {
  36. auto *Val = FreeList;
  37. __asan_unpoison_memory_region(Val, Size);
  38. FreeList = FreeList->Next;
  39. __msan_allocated_memory(Val, Size);
  40. return Val;
  41. }
  42. void push(FreeNode *N) {
  43. N->Next = FreeList;
  44. FreeList = N;
  45. __asan_poison_memory_region(N, Size);
  46. }
  47. public:
  48. ~Recycler() {
  49. // If this fails, either the callee has lost track of some allocation,
  50. // or the callee isn't tracking allocations and should just call
  51. // clear() before deleting the Recycler.
  52. assert(!FreeList && "Non-empty recycler deleted!");
  53. }
  54. /// clear - Release all the tracked allocations to the allocator. The
  55. /// recycler must be free of any tracked allocations before being
  56. /// deleted; calling clear is one way to ensure this.
  57. template<class AllocatorType>
  58. void clear(AllocatorType &Allocator) {
  59. while (FreeList) {
  60. T *t = reinterpret_cast<T *>(pop_val());
  61. Allocator.Deallocate(t);
  62. }
  63. }
  64. /// Special case for BumpPtrAllocator which has an empty Deallocate()
  65. /// function.
  66. ///
  67. /// There is no need to traverse the free list, pulling all the objects into
  68. /// cache.
  69. void clear(BumpPtrAllocator &) { FreeList = nullptr; }
  70. template<class SubClass, class AllocatorType>
  71. SubClass *Allocate(AllocatorType &Allocator) {
  72. static_assert(alignof(SubClass) <= Align,
  73. "Recycler allocation alignment is less than object align!");
  74. static_assert(sizeof(SubClass) <= Size,
  75. "Recycler allocation size is less than object size!");
  76. return FreeList ? reinterpret_cast<SubClass *>(pop_val())
  77. : static_cast<SubClass *>(Allocator.Allocate(Size, Align));
  78. }
  79. template<class AllocatorType>
  80. T *Allocate(AllocatorType &Allocator) {
  81. return Allocate<T>(Allocator);
  82. }
  83. template<class SubClass, class AllocatorType>
  84. void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
  85. push(reinterpret_cast<FreeNode *>(Element));
  86. }
  87. void PrintStats();
  88. };
  89. template <class T, size_t Size, size_t Align>
  90. void Recycler<T, Size, Align>::PrintStats() {
  91. size_t S = 0;
  92. for (auto *I = FreeList; I; I = I->Next)
  93. ++S;
  94. PrintRecyclerStats(Size, Align, S);
  95. }
  96. }
  97. #endif