Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.5 KiB

  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/MathExtras.h"
  18. namespace llvm {
  19. class BumpPtrAllocator;
  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. // 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 0;
  39. FreeList *Entry = Bucket[Idx];
  40. if (!Entry)
  41. return 0;
  42. Bucket[Idx] = Entry->Next;
  43. return reinterpret_cast<T*>(Entry);
  44. }
  45. // Add an entry to the free list at Bucket[Idx].
  46. void push(unsigned Idx, T *Ptr) {
  47. assert(Ptr && "Cannot recycle NULL pointer");
  48. assert(sizeof(T) >= sizeof(FreeList) && "Objects are too small");
  49. assert(Align >= AlignOf<FreeList>::Alignment && "Object underaligned");
  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