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/MathExtras.h"
23 class BumpPtrAllocator;
25 /// Recycle small arrays allocated from a BumpPtrAllocator.
27 /// Arrays are allocated in a small number of fixed sizes. For each supported
28 /// array size, the ArrayRecycler keeps a free list of available arrays.
30 template<class T, size_t Align = AlignOf<T>::Alignment>
32 // The free list for a given array size is a simple singly linked list.
33 // We can't use iplist or Recycler here since those classes can't be copied.
38 static_assert(Align >= AlignOf<FreeList>::Alignment, "Object underaligned");
39 static_assert(sizeof(T) >= sizeof(FreeList), "Objects are too small");
41 // Keep a free list for each array size.
42 SmallVector<FreeList*, 8> Bucket;
44 // Remove an entry from the free list in Bucket[Idx] and return it.
45 // Return NULL if no entries are available.
46 T *pop(unsigned Idx) {
47 if (Idx >= Bucket.size())
49 FreeList *Entry = Bucket[Idx];
52 Bucket[Idx] = Entry->Next;
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];
67 /// The size of an allocated array is represented by a Capacity instance.
69 /// This class is much smaller than a size_t, and it provides methods to work
70 /// with the set of legal array capacities.
73 explicit Capacity(uint8_t idx) : Index(idx) {}
76 Capacity() : Index(0) {}
78 /// Get the capacity of an array that can hold at least N elements.
79 static Capacity get(size_t N) {
80 return Capacity(N ? Log2_64_Ceil(N) : 0);
83 /// Get the number of elements in an array with this capacity.
84 size_t getSize() const { return size_t(1u) << Index; }
86 /// Get the bucket number for this capacity.
87 unsigned getBucket() const { return Index; }
89 /// Get the next larger capacity. Large capacities grow exponentially, so
90 /// this function can be used to reallocate incrementally growing vectors
91 /// in amortized linear time.
92 Capacity getNext() const { return Capacity(Index + 1); }
96 // The client should always call clear() so recycled arrays can be returned
98 assert(Bucket.empty() && "Non-empty ArrayRecycler deleted!");
101 /// Release all the tracked allocations to the allocator. The recycler must
102 /// be free of any tracked allocations before being deleted.
103 template<class AllocatorType>
104 void clear(AllocatorType &Allocator) {
105 for (; !Bucket.empty(); Bucket.pop_back())
106 while (T *Ptr = pop(Bucket.size() - 1))
107 Allocator.Deallocate(Ptr);
110 /// Special case for BumpPtrAllocator which has an empty Deallocate()
113 /// There is no need to traverse the free lists, pulling all the objects into
115 void clear(BumpPtrAllocator&) {
119 /// Allocate an array of at least the requested capacity.
121 /// Return an existing recycled array, or allocate one from Allocator if
122 /// none are available for recycling.
124 template<class AllocatorType>
125 T *allocate(Capacity Cap, AllocatorType &Allocator) {
126 // Try to recycle an existing array.
127 if (T *Ptr = pop(Cap.getBucket()))
129 // Nope, get more memory.
130 return static_cast<T*>(Allocator.Allocate(sizeof(T)*Cap.getSize(), Align));
133 /// Deallocate an array with the specified Capacity.
135 /// Cap must be the same capacity that was given to allocate().
137 void deallocate(Capacity Cap, T *Ptr) {
138 push(Cap.getBucket(), Ptr);
142 } // end llvm namespace