ArrayRecycler.h 4.6 KB

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