| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
// A simple segmented vector class. |
| 8 |
// |
| 9 |
// This class should be used in preference to mozilla::Vector or nsTArray when |
| 10 |
// you are simply gathering items in order to later iterate over them. |
| 11 |
// |
| 12 |
// - In the case where you don't know the final size in advance, using |
| 13 |
// SegmentedVector avoids the need to repeatedly allocate increasingly large |
| 14 |
// buffers and copy the data into them. |
| 15 |
// |
| 16 |
// - In the case where you know the final size in advance and so can set the |
| 17 |
// capacity appropriately, using SegmentedVector still avoids the need for |
| 18 |
// large allocations (which can trigger OOMs). |
| 19 |
|
| 20 |
#ifndef mozilla_SegmentedVector_h |
| 21 |
#define mozilla_SegmentedVector_h |
| 22 |
|
| 23 |
#include <new> // for placement new |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
#include "mozilla/AllocPolicy.h" |
| 27 |
#include "mozilla/Array.h" |
| 28 |
#include "mozilla/Attributes.h" |
| 29 |
#include "mozilla/LinkedList.h" |
| 30 |
#include "mozilla/MemoryReporting.h" |
| 31 |
#include "mozilla/OperatorNewExtensions.h" |
| 32 |
|
| 33 |
#ifdef IMPL_LIBXUL |
| 34 |
# include "mozilla/Likely.h" |
| 35 |
# include "mozilla/mozalloc_oom.h" |
| 36 |
#endif // IMPL_LIBXUL |
| 37 |
|
| 38 |
namespace mozilla { |
| 39 |
|
| 40 |
// |IdealSegmentSize| specifies how big each segment will be in bytes (or as |
| 41 |
// close as is possible). Use the following guidelines to choose a size. |
| 42 |
// |
| 43 |
// - It should be a power-of-two, to avoid slop. |
| 44 |
// |
| 45 |
// - It should not be too small, so that segment allocations are infrequent, |
| 46 |
// and so that per-segment bookkeeping overhead is low. Typically each |
| 47 |
// segment should be able to hold hundreds of elements, at least. |
| 48 |
// |
| 49 |
// - It should not be too large, so that OOMs are unlikely when allocating |
| 50 |
// segments, and so that not too much space is wasted when the final segment |
| 51 |
// is not full. |
| 52 |
// |
| 53 |
// The ideal size depends on how the SegmentedVector is used and the size of |
| 54 |
// |T|, but reasonable sizes include 1024, 4096 (the default), 8192, and 16384. |
| 55 |
// |
| 56 |
template <typename T, size_t IdealSegmentSize = 4096, |
| 57 |
typename AllocPolicy = MallocAllocPolicy> |
| 58 |
class SegmentedVector : private AllocPolicy { |
| 59 |
template <size_t SegmentCapacity> |
| 60 |
struct SegmentImpl |
| 61 |
: public mozilla::LinkedListElement<SegmentImpl<SegmentCapacity>> { |
| 62 |
private: |
| 63 |
uint32_t mLength; |
| 64 |
alignas(T) MOZ_INIT_OUTSIDE_CTOR |
| 65 |
unsigned char mData[sizeof(T) * SegmentCapacity]; |
| 66 |
|
| 67 |
// Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a |
| 68 |
// -Werror compile error) to reinterpret_cast<> |mData| to |T*|, even |
| 69 |
// through |void*|. Placing the latter cast in these separate functions |
| 70 |
// breaks the chain such that affected GCC versions no longer warn/error. |
| 71 |
void* RawData() { return mData; } |
| 72 |
|
| 73 |
public: |
| 74 |
SegmentImpl() : mLength(0) {} |
| 75 |
|
| 76 |
~SegmentImpl() { |
| 77 |
for (uint32_t i = 0; i < mLength; i++) { |
| 78 |
(*this)[i].~T(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
uint32_t Length() const { return mLength; } |
| 83 |
|
| 84 |
T* Elems() { return reinterpret_cast<T*>(RawData()); } |
| 85 |
|
| 86 |
T& operator[](size_t aIndex) { |
| 87 |
MOZ_ASSERT(aIndex < mLength); |
| 88 |
return Elems()[aIndex]; |
| 89 |
} |
| 90 |
|
| 91 |
const T& operator[](size_t aIndex) const { |
| 92 |
MOZ_ASSERT(aIndex < mLength); |
| 93 |
return Elems()[aIndex]; |
| 94 |
} |
| 95 |
|
| 96 |
template <typename U> |
| 97 |
void Append(U&& aU) { |
| 98 |
MOZ_ASSERT(mLength < SegmentCapacity); |
| 99 |
// Pre-increment mLength so that the bounds-check in operator[] passes. |
| 100 |
mLength++; |
| 101 |
T* elem = &(*this)[mLength - 1]; |
| 102 |
new (KnownNotNull, elem) T(std::forward<U>(aU)); |
| 103 |
} |
| 104 |
|
| 105 |
void PopLast() { |
| 106 |
MOZ_ASSERT(mLength > 0); |
| 107 |
(*this)[mLength - 1].~T(); |
| 108 |
mLength--; |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
// See how many we elements we can fit in a segment of IdealSegmentSize. If |
| 113 |
// IdealSegmentSize is too small, it'll be just one. The +1 is because |
| 114 |
// kSingleElementSegmentSize already accounts for one element. |
| 115 |
static const size_t kSingleElementSegmentSize = sizeof(SegmentImpl<1>); |
| 116 |
static const size_t kSegmentCapacity = |
| 117 |
kSingleElementSegmentSize <= IdealSegmentSize |
| 118 |
? (IdealSegmentSize - kSingleElementSegmentSize) / sizeof(T) + 1 |
| 119 |
: 1; |
| 120 |
|
| 121 |
public: |
| 122 |
typedef SegmentImpl<kSegmentCapacity> Segment; |
| 123 |
|
| 124 |
// The |aIdealSegmentSize| is only for sanity checking. If it's specified, we |
| 125 |
// check that the actual segment size is as close as possible to it. This |
| 126 |
// serves as a sanity check for SegmentedVectorCapacity's capacity |
| 127 |
// computation. |
| 128 |
explicit SegmentedVector(size_t aIdealSegmentSize = 0) { |
| 129 |
// The difference between the actual segment size and the ideal segment |
| 130 |
// size should be less than the size of a single element... unless the |
| 131 |
// ideal size was too small, in which case the capacity should be one. |
| 132 |
MOZ_ASSERT_IF( |
| 133 |
aIdealSegmentSize != 0, |
| 134 |
(sizeof(Segment) > aIdealSegmentSize && kSegmentCapacity == 1) || |
| 135 |
aIdealSegmentSize - sizeof(Segment) < sizeof(T)); |
| 136 |
} |
| 137 |
|
| 138 |
SegmentedVector(SegmentedVector&& aOther) |
| 139 |
: mSegments(std::move(aOther.mSegments)) {} |
| 140 |
SegmentedVector& operator=(SegmentedVector&& aOther) { |
| 141 |
if (&aOther != this) { |
| 142 |
this->~SegmentedVector(); |
| 143 |
new (this) SegmentedVector(std::move(aOther)); |
| 144 |
} |
| 145 |
return *this; |
| 146 |
} |
| 147 |
|
| 148 |
~SegmentedVector() { Clear(); } |
| 149 |
|
| 150 |
bool IsEmpty() const { return !mSegments.getFirst(); } |
| 151 |
|
| 152 |
// Note that this is O(n) rather than O(1), but the constant factor is very |
| 153 |
// small because it only has to do one addition per segment. |
| 154 |
size_t Length() const { |
| 155 |
size_t n = 0; |
| 156 |
for (auto segment = mSegments.getFirst(); segment; |
| 157 |
segment = segment->getNext()) { |
| 158 |
n += segment->Length(); |
| 159 |
} |
| 160 |
return n; |
| 161 |
} |
| 162 |
|
| 163 |
// Returns false if the allocation failed. (If you are using an infallible |
| 164 |
// allocation policy, use InfallibleAppend() instead.) |
| 165 |
template <typename U> |
| 166 |
[[nodiscard]] bool Append(U&& aU) { |
| 167 |
Segment* last = mSegments.getLast(); |
| 168 |
if (!last || last->Length() == kSegmentCapacity) { |
| 169 |
last = this->template pod_malloc<Segment>(1); |
| 170 |
if (!last) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
new (KnownNotNull, last) Segment(); |
| 174 |
mSegments.insertBack(last); |
| 175 |
} |
| 176 |
last->Append(std::forward<U>(aU)); |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
// You should probably only use this instead of Append() if you are using an |
| 181 |
// infallible allocation policy. It will crash if the allocation fails. |
| 182 |
template <typename U> |
| 183 |
void InfallibleAppend(U&& aU) { |
| 184 |
bool ok = Append(std::forward<U>(aU)); |
| 185 |
|
| 186 |
#ifdef IMPL_LIBXUL |
| 187 |
if (MOZ_UNLIKELY(!ok)) { |
| 188 |
mozalloc_handle_oom(sizeof(Segment)); |
| 189 |
} |
| 190 |
#else |
| 191 |
MOZ_RELEASE_ASSERT(ok); |
| 192 |
#endif // MOZ_INTERNAL_API |
| 193 |
} |
| 194 |
|
| 195 |
void Clear() { |
| 196 |
Segment* segment; |
| 197 |
while ((segment = mSegments.popFirst())) { |
| 198 |
segment->~Segment(); |
| 199 |
this->free_(segment, 1); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
T& GetLast() { |
| 204 |
MOZ_ASSERT(!IsEmpty()); |
| 205 |
Segment* last = mSegments.getLast(); |
| 206 |
return (*last)[last->Length() - 1]; |
| 207 |
} |
| 208 |
|
| 209 |
const T& GetLast() const { |
| 210 |
MOZ_ASSERT(!IsEmpty()); |
| 211 |
Segment* last = mSegments.getLast(); |
| 212 |
return (*last)[last->Length() - 1]; |
| 213 |
} |
| 214 |
|
| 215 |
void PopLast() { |
| 216 |
MOZ_ASSERT(!IsEmpty()); |
| 217 |
Segment* last = mSegments.getLast(); |
| 218 |
last->PopLast(); |
| 219 |
if (!last->Length()) { |
| 220 |
mSegments.popLast(); |
| 221 |
last->~Segment(); |
| 222 |
this->free_(last, 1); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// Equivalent to calling |PopLast| |aNumElements| times, but potentially |
| 227 |
// more efficient. It is safe to call this even when aNumElements > Length(). |
| 228 |
void PopLastN(uint32_t aNumElements) { |
| 229 |
Segment* last; |
| 230 |
|
| 231 |
// Pop full segments for as long as we can. Note that this loop |
| 232 |
// cleanly handles the case when the initial last segment is not |
| 233 |
// full and we are popping more elements than said segment contains. |
| 234 |
do { |
| 235 |
last = mSegments.getLast(); |
| 236 |
|
| 237 |
// The list is empty. We're all done. |
| 238 |
if (!last) { |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
// Check to see if the list contains too many elements. Handle |
| 243 |
// that in the epilogue. |
| 244 |
uint32_t segmentLen = last->Length(); |
| 245 |
if (segmentLen > aNumElements) { |
| 246 |
break; |
| 247 |
} |
| 248 |
|
| 249 |
// Destroying the segment destroys all elements contained therein. |
| 250 |
mSegments.popLast(); |
| 251 |
last->~Segment(); |
| 252 |
this->free_(last, 1); |
| 253 |
|
| 254 |
MOZ_ASSERT(aNumElements >= segmentLen); |
| 255 |
aNumElements -= segmentLen; |
| 256 |
if (aNumElements == 0) { |
| 257 |
return; |
| 258 |
} |
| 259 |
} while (true); |
| 260 |
|
| 261 |
// Handle the case where the last segment contains more elements |
| 262 |
// than we want to pop. |
| 263 |
MOZ_ASSERT(last); |
| 264 |
MOZ_ASSERT(last == mSegments.getLast()); |
| 265 |
MOZ_ASSERT(aNumElements < last->Length()); |
| 266 |
for (uint32_t i = 0; i < aNumElements; ++i) { |
| 267 |
last->PopLast(); |
| 268 |
} |
| 269 |
MOZ_ASSERT(last->Length() != 0); |
| 270 |
} |
| 271 |
|
| 272 |
// Use this class to iterate over a SegmentedVector, like so: |
| 273 |
// |
| 274 |
// for (auto iter = v.Iter(); !iter.Done(); iter.Next()) { |
| 275 |
// MyElem& elem = iter.Get(); |
| 276 |
// f(elem); |
| 277 |
// } |
| 278 |
// |
| 279 |
// Note, adding new entries to the SegmentedVector while using iterators |
| 280 |
// is supported, but removing is not! |
| 281 |
// If an iterator has entered Done() state, adding more entries to the |
| 282 |
// vector doesn't affect it. |
| 283 |
class IterImpl { |
| 284 |
friend class SegmentedVector; |
| 285 |
|
| 286 |
Segment* mSegment; |
| 287 |
size_t mIndex; |
| 288 |
|
| 289 |
explicit IterImpl(SegmentedVector* aVector, bool aFromFirst) |
| 290 |
: mSegment(aFromFirst ? aVector->mSegments.getFirst() |
| 291 |
: aVector->mSegments.getLast()), |
| 292 |
mIndex(aFromFirst ? 0 : (mSegment ? mSegment->Length() - 1 : 0)) { |
| 293 |
MOZ_ASSERT_IF(mSegment, mSegment->Length() > 0); |
| 294 |
} |
| 295 |
|
| 296 |
public: |
| 297 |
bool Done() const { |
| 298 |
MOZ_ASSERT_IF(mSegment, mSegment->isInList()); |
| 299 |
MOZ_ASSERT_IF(mSegment, mIndex < mSegment->Length()); |
| 300 |
return !mSegment; |
| 301 |
} |
| 302 |
|
| 303 |
T& Get() { |
| 304 |
MOZ_ASSERT(!Done()); |
| 305 |
return (*mSegment)[mIndex]; |
| 306 |
} |
| 307 |
|
| 308 |
const T& Get() const { |
| 309 |
MOZ_ASSERT(!Done()); |
| 310 |
return (*mSegment)[mIndex]; |
| 311 |
} |
| 312 |
|
| 313 |
void Next() { |
| 314 |
MOZ_ASSERT(!Done()); |
| 315 |
mIndex++; |
| 316 |
if (mIndex == mSegment->Length()) { |
| 317 |
mSegment = mSegment->getNext(); |
| 318 |
mIndex = 0; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
void Prev() { |
| 323 |
MOZ_ASSERT(!Done()); |
| 324 |
if (mIndex == 0) { |
| 325 |
mSegment = mSegment->getPrevious(); |
| 326 |
if (mSegment) { |
| 327 |
mIndex = mSegment->Length() - 1; |
| 328 |
} |
| 329 |
} else { |
| 330 |
--mIndex; |
| 331 |
} |
| 332 |
} |
| 333 |
}; |
| 334 |
|
| 335 |
IterImpl Iter() { return IterImpl(this, true); } |
| 336 |
IterImpl IterFromLast() { return IterImpl(this, false); } |
| 337 |
|
| 338 |
// Measure the memory consumption of the vector excluding |this|. Note that |
| 339 |
// it only measures the vector itself. If the vector elements contain |
| 340 |
// pointers to other memory blocks, those blocks must be measured separately |
| 341 |
// during a subsequent iteration over the vector. |
| 342 |
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const { |
| 343 |
return mSegments.sizeOfExcludingThis(aMallocSizeOf); |
| 344 |
} |
| 345 |
|
| 346 |
// Like sizeOfExcludingThis(), but measures |this| as well. |
| 347 |
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const { |
| 348 |
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); |
| 349 |
} |
| 350 |
|
| 351 |
private: |
| 352 |
mozilla::LinkedList<Segment> mSegments; |
| 353 |
}; |
| 354 |
|
| 355 |
} // namespace mozilla |
| 356 |
|
| 357 |
#endif /* mozilla_SegmentedVector_h */ |