9#ifndef LLVM_LIB_DWARFLINKER_PARALLEL_ARRAYLIST_H
10#define LLVM_LIB_DWARFLINKER_PARALLEL_ARRAYLIST_H
24template <
typename T,
size_t ItemsGroupSize = 512>
class ArrayList {
43 CurItemsCount = CurGroup->
ItemsCount.fetch_add(1);
46 if (CurItemsCount < ItemsGroupSize)
57 CurGroup->
Items[CurItemsCount] = Item;
58 return CurGroup->
Items[CurItemsCount];
66 CurGroup = CurGroup->Next) {
67 for (
T &Item : *CurGroup)
82 bool Parallel =
false) {
86 if (SortedItems.
size()) {
90 std::sort(SortedItems.
begin(), SortedItems.
end(), Comparator);
92 size_t SortedItemIdx = 0;
93 forEach([&](
T &Item) { Item = SortedItems[SortedItemIdx++]; });
102 CurGroup = CurGroup->Next)
103 Result += CurGroup->getItemsCount();
110 using ArrayTy = std::array<T, ItemsGroupSize>;
116 std::atomic<ItemsGroup *>
Next =
nullptr;
125 return std::min(
ItemsCount.load(), ItemsGroupSize);
142 NewGroup->
Next =
nullptr;
145 if (AtomicGroup.compare_exchange_strong(CurGroup, NewGroup))
153 if (CurGroup->
Next.compare_exchange_weak(NextGroup, NewGroup))
157 CurGroup = NextGroup;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
void sort(function_ref< bool(const T &LHS, const T &RHS)> Comparator, bool Parallel=false)
T & add(const T &Item)
Add specified Item to the list.
std::atomic< ItemsGroup * > LastGroup
bool empty()
Check whether list is empty.
llvm::parallel::PerThreadBumpPtrAllocator * Allocator
bool allocateNewGroup(std::atomic< ItemsGroup * > &AtomicGroup)
void forEach(ItemHandlerTy Handler)
Enumerate all items and apply specified Handler to each.
std::atomic< ItemsGroup * > GroupsHead
function_ref< void(T &)> ItemHandlerTy
ArrayList(llvm::parallel::PerThreadBumpPtrAllocator *Allocator)
An efficient, type-erasing, non-owning reference to a callable.
PerThreadAllocator< BumpPtrAllocator > PerThreadBumpPtrAllocator
This is an optimization pass for GlobalISel generic memory operations.
void parallelSort(RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
std::array< T, ItemsGroupSize > ArrayTy
std::atomic< size_t > ItemsCount
std::atomic< ItemsGroup * > Next
ArrayTy::iterator begin()
size_t getItemsCount() const