|
| 1 | +//===--- StackList.h - defines the StackList data structure -----*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_SIL_STACKLIST_H |
| 14 | +#define SWIFT_SIL_STACKLIST_H |
| 15 | + |
| 16 | +#include "swift/SIL/SILModule.h" |
| 17 | + |
| 18 | +namespace swift { |
| 19 | + |
| 20 | +/// A very efficient implementation of a stack, which can also be iterated over. |
| 21 | +/// |
| 22 | +/// A StackList is the best choice for things like worklists, etc., if no random |
| 23 | +/// access is needed. |
| 24 | +/// Regardless of how large a Stack gets, there is no memory allocation needed |
| 25 | +/// (except maybe for the first few uses in the compiler run). |
| 26 | +/// All operations have (almost) zero cost. |
| 27 | +template <typename Element> class StackList { |
| 28 | + /// The capacity of a single slab. |
| 29 | + static constexpr size_t slabCapacity = |
| 30 | + FixedSizeSlab::capacity / sizeof(Element); |
| 31 | + |
| 32 | + static_assert(slabCapacity > 0, "Element type to large for StackList"); |
| 33 | + static_assert(alignof(FixedSizeSlab) >= alignof(Element), |
| 34 | + "Element alignment to large for StackList"); |
| 35 | + |
| 36 | + /// Backlink to the module which manages the slab allocation. |
| 37 | + SILModule &module; |
| 38 | + |
| 39 | + /// The list of slabs. |
| 40 | + /// |
| 41 | + /// Invariant: there is always free space in the last slab to store at least |
| 42 | + /// one element. |
| 43 | + SILModule::SlabList slabs; |
| 44 | + |
| 45 | + /// The index of the next free element in endSlab. |
| 46 | + /// |
| 47 | + /// Invariant: endIndex < slabCapacity |
| 48 | + unsigned endIndex = 0; |
| 49 | + |
| 50 | + FixedSizeSlab *lastSlab() { return &*slabs.rbegin(); } |
| 51 | + const FixedSizeSlab *lastSlab() const { return &*slabs.rbegin(); } |
| 52 | + |
| 53 | + void allocSlab() { slabs.push_back(module.allocSlab()); } |
| 54 | + |
| 55 | + void growIfNeeded() { |
| 56 | + if (endIndex == slabCapacity) { |
| 57 | + allocSlab(); |
| 58 | + endIndex = 0; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | +public: |
| 64 | + /// The Stack's iterator. |
| 65 | + class iterator { |
| 66 | + friend StackList<Element>; |
| 67 | + const FixedSizeSlab *slab; |
| 68 | + unsigned index; |
| 69 | + |
| 70 | + iterator(const FixedSizeSlab *slab, unsigned index) |
| 71 | + : slab(slab), index(index) {} |
| 72 | + |
| 73 | + public: |
| 74 | + const Element &operator*() const { |
| 75 | + assert(index < slabCapacity); |
| 76 | + return slab->dataFor<Element>()[index]; |
| 77 | + } |
| 78 | + const Element &operator->() const { return *this; } |
| 79 | + |
| 80 | + iterator &operator++() { |
| 81 | + assert(index < slabCapacity); |
| 82 | + index++; |
| 83 | + if (index == slabCapacity) { |
| 84 | + slab = &*std::next(slab->getIterator()); |
| 85 | + index = 0; |
| 86 | + } |
| 87 | + return *this; |
| 88 | + } |
| 89 | + |
| 90 | + iterator operator++(int unused) { |
| 91 | + iterator copy = *this; |
| 92 | + ++*this; |
| 93 | + return copy; |
| 94 | + } |
| 95 | + |
| 96 | + friend bool operator==(iterator lhs, iterator rhs) { |
| 97 | + return lhs.slab == rhs.slab && lhs.index == rhs.index; |
| 98 | + } |
| 99 | + |
| 100 | + friend bool operator!=(iterator lhs, iterator rhs) { |
| 101 | + return !(lhs == rhs); |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + /// Constructor. |
| 106 | + StackList(SILFunction *function) : module(function->getModule()) { |
| 107 | + /// Allocate one slab so that there is free space for inserting the first |
| 108 | + /// element. |
| 109 | + allocSlab(); |
| 110 | + } |
| 111 | + |
| 112 | + ~StackList() { |
| 113 | + module.freeAllSlabs(slabs); |
| 114 | + } |
| 115 | + |
| 116 | + iterator begin() const { return iterator(&*slabs.begin(), 0); } |
| 117 | + iterator end() const { return iterator(lastSlab(), endIndex); } |
| 118 | + |
| 119 | + bool empty() const { |
| 120 | + return begin() == end(); |
| 121 | + } |
| 122 | + |
| 123 | + /// Adds a new element at the end. |
| 124 | + void push_back(const Element &newElement) { |
| 125 | + assert(endIndex < slabCapacity); |
| 126 | + lastSlab()->template dataFor<Element>()[endIndex++] = newElement; |
| 127 | + growIfNeeded(); |
| 128 | + } |
| 129 | + |
| 130 | + /// Adds a new element at the end. |
| 131 | + void push_back(Element &&newElement) { |
| 132 | + assert(endIndex < slabCapacity); |
| 133 | + lastSlab()->template dataFor<Element>()[endIndex++] = std::move(newElement); |
| 134 | + growIfNeeded(); |
| 135 | + } |
| 136 | + |
| 137 | + /// Removes the last element and returns it. |
| 138 | + Element pop_back_val() { |
| 139 | + FixedSizeSlab *slab = lastSlab(); |
| 140 | + if (endIndex > 0) |
| 141 | + return std::move(slab->dataFor<Element>()[--endIndex]); |
| 142 | + |
| 143 | + assert(!empty()); |
| 144 | + |
| 145 | + slabs.remove(slab); |
| 146 | + module.freeSlab(slab); |
| 147 | + assert(!slabs.empty()); |
| 148 | + endIndex = slabCapacity - 1; |
| 149 | + return std::move(lastSlab()->template dataFor<Element>()[endIndex]); |
| 150 | + } |
| 151 | +}; |
| 152 | + |
| 153 | +} // namespace swift |
| 154 | + |
| 155 | +#endif |
0 commit comments