ArrayRecycler.h 4.9 KB

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