ArrayRecycler.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //==- llvm/Support/ArrayRecycler.h - Recycling of Arrays ---------*- 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 ArrayRecycler class template which can recycle small
  10. // arrays allocated from one of the allocators in Allocator.h
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_ARRAYRECYCLER_H
  14. #define LLVM_SUPPORT_ARRAYRECYCLER_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/Support/Allocator.h"
  17. #include "llvm/Support/MathExtras.h"
  18. namespace llvm {
  19. /// Recycle small arrays allocated from a BumpPtrAllocator.
  20. ///
  21. /// Arrays are allocated in a small number of fixed sizes. For each supported
  22. /// array size, the ArrayRecycler keeps a free list of available arrays.
  23. ///
  24. template <class T, size_t Align = alignof(T)> class ArrayRecycler {
  25. // The free list for a given array size is a simple singly linked list.
  26. // We can't use iplist or Recycler here since those classes can't be copied.
  27. struct FreeList {
  28. FreeList *Next;
  29. };
  30. static_assert(Align >= alignof(FreeList), "Object underaligned");
  31. static_assert(sizeof(T) >= sizeof(FreeList), "Objects are too small");
  32. // Keep a free list for each array size.
  33. SmallVector<FreeList*, 8> Bucket;
  34. // Remove an entry from the free list in Bucket[Idx] and return it.
  35. // Return NULL if no entries are available.
  36. T *pop(unsigned Idx) {
  37. if (Idx >= Bucket.size())
  38. return nullptr;
  39. FreeList *Entry = Bucket[Idx];
  40. if (!Entry)
  41. return nullptr;
  42. __asan_unpoison_memory_region(Entry, Capacity::get(Idx).getSize());
  43. Bucket[Idx] = Entry->Next;
  44. __msan_allocated_memory(Entry, Capacity::get(Idx).getSize());
  45. return reinterpret_cast<T*>(Entry);
  46. }
  47. // Add an entry to the free list at Bucket[Idx].
  48. void push(unsigned Idx, T *Ptr) {
  49. assert(Ptr && "Cannot recycle NULL pointer");
  50. FreeList *Entry = reinterpret_cast<FreeList*>(Ptr);
  51. if (Idx >= Bucket.size())
  52. Bucket.resize(size_t(Idx) + 1);
  53. Entry->Next = Bucket[Idx];
  54. Bucket[Idx] = Entry;
  55. __asan_poison_memory_region(Ptr, Capacity::get(Idx).getSize());
  56. }
  57. public:
  58. /// The size of an allocated array is represented by a Capacity instance.
  59. ///
  60. /// This class is much smaller than a size_t, and it provides methods to work
  61. /// with the set of legal array capacities.
  62. class Capacity {
  63. uint8_t Index;
  64. explicit Capacity(uint8_t idx) : Index(idx) {}
  65. public:
  66. Capacity() : Index(0) {}
  67. /// Get the capacity of an array that can hold at least N elements.
  68. static Capacity get(size_t N) {
  69. return Capacity(N ? Log2_64_Ceil(N) : 0);
  70. }
  71. /// Get the number of elements in an array with this capacity.
  72. size_t getSize() const { return size_t(1u) << Index; }
  73. /// Get the bucket number for this capacity.
  74. unsigned getBucket() const { return Index; }
  75. /// Get the next larger capacity. Large capacities grow exponentially, so
  76. /// this function can be used to reallocate incrementally growing vectors
  77. /// in amortized linear time.
  78. Capacity getNext() const { return Capacity(Index + 1); }
  79. };
  80. ~ArrayRecycler() {
  81. // The client should always call clear() so recycled arrays can be returned
  82. // to the allocator.
  83. assert(Bucket.empty() && "Non-empty ArrayRecycler deleted!");
  84. }
  85. /// Release all the tracked allocations to the allocator. The recycler must
  86. /// be free of any tracked allocations before being deleted.
  87. template<class AllocatorType>
  88. void clear(AllocatorType &Allocator) {
  89. for (; !Bucket.empty(); Bucket.pop_back())
  90. while (T *Ptr = pop(Bucket.size() - 1))
  91. Allocator.Deallocate(Ptr);
  92. }
  93. /// Special case for BumpPtrAllocator which has an empty Deallocate()
  94. /// function.
  95. ///
  96. /// There is no need to traverse the free lists, pulling all the objects into
  97. /// cache.
  98. void clear(BumpPtrAllocator&) {
  99. Bucket.clear();
  100. }
  101. /// Allocate an array of at least the requested capacity.
  102. ///
  103. /// Return an existing recycled array, or allocate one from Allocator if
  104. /// none are available for recycling.
  105. ///
  106. template<class AllocatorType>
  107. T *allocate(Capacity Cap, AllocatorType &Allocator) {
  108. // Try to recycle an existing array.
  109. if (T *Ptr = pop(Cap.getBucket()))
  110. return Ptr;
  111. // Nope, get more memory.
  112. return static_cast<T*>(Allocator.Allocate(sizeof(T)*Cap.getSize(), Align));
  113. }
  114. /// Deallocate an array with the specified Capacity.
  115. ///
  116. /// Cap must be the same capacity that was given to allocate().
  117. ///
  118. void deallocate(Capacity Cap, T *Ptr) {
  119. push(Cap.getBucket(), Ptr);
  120. }
  121. };
  122. } // end llvm namespace
  123. #endif