1 //==- llvm/Support/ArrayRecycler.h - Recycling of Arrays ---------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ArrayRecycler class template which can recycle small
11 // arrays allocated from one of the allocators in Allocator.h
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_ARRAYRECYCLER_H
16 #define LLVM_SUPPORT_ARRAYRECYCLER_H
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/MathExtras.h"
24 /// Recycle small arrays allocated from a BumpPtrAllocator.
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.
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.
36 static_assert(Align >= alignof(FreeList), "Object underaligned");
37 static_assert(sizeof(T) >= sizeof(FreeList), "Objects are too small");
39 // Keep a free list for each array size.
40 SmallVector<FreeList*, 8> Bucket;
42 // Remove an entry from the free list in Bucket[Idx] and return it.
43 // Return NULL if no entries are available.
44 T *pop(unsigned Idx) {
45 if (Idx >= Bucket.size())
47 FreeList *Entry = Bucket[Idx];
50 __asan_unpoison_memory_region(Entry, Capacity::get(Idx).getSize());
51 Bucket[Idx] = Entry->Next;
52 __msan_allocated_memory(Entry, Capacity::get(Idx).getSize());
53 return reinterpret_cast<T*>(Entry);
56 // Add an entry to the free list at Bucket[Idx].
57 void push(unsigned Idx, T *Ptr) {
58 assert(Ptr && "Cannot recycle NULL pointer");
59 FreeList *Entry = reinterpret_cast<FreeList*>(Ptr);
60 if (Idx >= Bucket.size())
61 Bucket.resize(size_t(Idx) + 1);
62 Entry->Next = Bucket[Idx];
64 __asan_poison_memory_region(Ptr, Capacity::get(Idx).getSize());
68 /// The size of an allocated array is represented by a Capacity instance.
70 /// This class is much smaller than a size_t, and it provides methods to work
71 /// with the set of legal array capacities.
74 explicit Capacity(uint8_t idx) : Index(idx) {}
77 Capacity() : Index(0) {}
79 /// Get the capacity of an array that can hold at least N elements.
80 static Capacity get(size_t N) {
81 return Capacity(N ? Log2_64_Ceil(N) : 0);
84 /// Get the number of elements in an array with this capacity.
85 size_t getSize() const { return size_t(1u) << Index; }
87 /// Get the bucket number for this capacity.
88 unsigned getBucket() const { return Index; }
90 /// Get the next larger capacity. Large capacities grow exponentially, so
91 /// this function can be used to reallocate incrementally growing vectors
92 /// in amortized linear time.
93 Capacity getNext() const { return Capacity(Index + 1); }
97 // The client should always call clear() so recycled arrays can be returned
99 assert(Bucket.empty() && "Non-empty ArrayRecycler deleted!");
102 /// Release all the tracked allocations to the allocator. The recycler must
103 /// be free of any tracked allocations before being deleted.
104 template<class AllocatorType>
105 void clear(AllocatorType &Allocator) {
106 for (; !Bucket.empty(); Bucket.pop_back())
107 while (T *Ptr = pop(Bucket.size() - 1))
108 Allocator.Deallocate(Ptr);
111 /// Special case for BumpPtrAllocator which has an empty Deallocate()
114 /// There is no need to traverse the free lists, pulling all the objects into
116 void clear(BumpPtrAllocator&) {
120 /// Allocate an array of at least the requested capacity.
122 /// Return an existing recycled array, or allocate one from Allocator if
123 /// none are available for recycling.
125 template<class AllocatorType>
126 T *allocate(Capacity Cap, AllocatorType &Allocator) {
127 // Try to recycle an existing array.
128 if (T *Ptr = pop(Cap.getBucket()))
130 // Nope, get more memory.
131 return static_cast<T*>(Allocator.Allocate(sizeof(T)*Cap.getSize(), Align));
134 /// Deallocate an array with the specified Capacity.
136 /// Cap must be the same capacity that was given to allocate().
138 void deallocate(Capacity Cap, T *Ptr) {
139 push(Cap.getBucket(), Ptr);
143 } // end llvm namespace