Recycler.h 3.6 KB

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