2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-10 03:33:29 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* A type/length-parametrized vector class. */
|
|
|
|
|
|
|
|
#ifndef mozilla_Vector_h
|
|
|
|
#define mozilla_Vector_h
|
|
|
|
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include <new> // for placement new
|
2023-04-17 16:50:11 +03:00
|
|
|
#include <type_traits>
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2013-08-14 11:00:52 +04:00
|
|
|
#include "mozilla/Alignment.h"
|
2013-07-10 03:33:29 +04:00
|
|
|
#include "mozilla/AllocPolicy.h"
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h" // for PointerRangeSize
|
2013-07-10 03:33:29 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/MathAlgorithms.h"
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-08-13 05:43:49 +03:00
|
|
|
#include "mozilla/OperatorNewExtensions.h"
|
2013-07-10 03:33:29 +04:00
|
|
|
#include "mozilla/ReentrancyGuard.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "mozilla/Span.h"
|
2013-07-10 03:33:29 +04:00
|
|
|
#include "mozilla/TemplateLib.h"
|
2013-07-26 07:01:42 +04:00
|
|
|
|
2013-07-10 03:33:29 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AllocPolicy>
|
|
|
|
class Vector;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that the given capacity wastes the minimal amount of space if
|
2022-07-28 01:59:51 +03:00
|
|
|
* allocated on the heap. This means that aCapacity*EltSize is as close to a
|
2014-07-11 06:10:17 +04:00
|
|
|
* power-of-two as possible. growStorageBy() is responsible for ensuring this.
|
2013-07-10 03:33:29 +04:00
|
|
|
*/
|
2022-07-28 01:59:51 +03:00
|
|
|
template <size_t EltSize>
|
2014-07-11 06:10:17 +04:00
|
|
|
static bool CapacityHasExcessSpace(size_t aCapacity) {
|
2022-07-28 01:59:51 +03:00
|
|
|
size_t size = aCapacity * EltSize;
|
|
|
|
return RoundUpPow2(size) - size >= EltSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AllocPolicy can optionally provide a `computeGrowth<T>(size_t aOldElts,
|
|
|
|
* size_t aIncr)` method that returns the new number of elements to allocate
|
|
|
|
* when the current capacity is `aOldElts` and `aIncr` more are being
|
|
|
|
* requested. If the AllocPolicy does not have such a method, a fallback
|
|
|
|
* will be used that mostly will just round the new requested capacity up to
|
|
|
|
* the next power of two, which results in doubling capacity for the most part.
|
|
|
|
*
|
|
|
|
* If the new size would overflow some limit, `computeGrowth` returns 0.
|
|
|
|
*
|
|
|
|
* A simpler way would be to make computeGrowth() part of the API for all
|
|
|
|
* AllocPolicy classes, but this turns out to be rather complex because
|
|
|
|
* mozalloc.h defines a very widely-used InfallibleAllocPolicy, and yet it
|
|
|
|
* can only be compiled in limited contexts, eg within `extern "C"` and with
|
|
|
|
* -std=c++11 rather than a later version. That makes the headers that are
|
|
|
|
* necessary for the computation unavailable (eg mfbt/MathAlgorithms.h).
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Fallback version.
|
|
|
|
template <size_t EltSize>
|
|
|
|
inline size_t GrowEltsByDoubling(size_t aOldElts, size_t aIncr) {
|
|
|
|
/*
|
|
|
|
* When choosing a new capacity, its size in bytes should is as close to 2**N
|
|
|
|
* bytes as possible. 2**N-sized requests are best because they are unlikely
|
|
|
|
* to be rounded up by the allocator. Asking for a 2**N number of elements
|
|
|
|
* isn't as good, because if EltSize is not a power-of-two that would
|
|
|
|
* result in a non-2**N request size.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (aIncr == 1) {
|
|
|
|
if (aOldElts == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This case occurs in ~15--20% of the calls to Vector::growStorageBy. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Will aOldSize * 4 * sizeof(T) overflow? This condition limits a
|
|
|
|
* collection to 1GB of memory on a 32-bit system, which is a reasonable
|
|
|
|
* limit. It also ensures that
|
|
|
|
*
|
|
|
|
* static_cast<char*>(end()) - static_cast<char*>(begin())
|
|
|
|
*
|
|
|
|
* for a Vector doesn't overflow ptrdiff_t (see bug 510319).
|
|
|
|
*/
|
|
|
|
if (MOZ_UNLIKELY(aOldElts &
|
|
|
|
mozilla::tl::MulOverflowMask<4 * EltSize>::value)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we reach here, the existing capacity will have a size that is already
|
|
|
|
* as close to 2^N as sizeof(T) will allow. Just double the capacity, and
|
|
|
|
* then there might be space for one more element.
|
|
|
|
*/
|
|
|
|
size_t newElts = aOldElts * 2;
|
|
|
|
if (CapacityHasExcessSpace<EltSize>(newElts)) {
|
|
|
|
newElts += 1;
|
|
|
|
}
|
|
|
|
return newElts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This case occurs in ~2% of the calls to Vector::growStorageBy. */
|
|
|
|
size_t newMinCap = aOldElts + aIncr;
|
|
|
|
|
2022-08-09 01:59:15 +03:00
|
|
|
/* Did aOldElts + aIncr overflow? Will newMinCap * EltSize rounded up to the
|
|
|
|
* next power of two overflow PTRDIFF_MAX? */
|
2022-07-28 01:59:51 +03:00
|
|
|
if (MOZ_UNLIKELY(newMinCap < aOldElts ||
|
2022-08-09 01:59:15 +03:00
|
|
|
newMinCap & tl::MulOverflowMask<4 * EltSize>::value)) {
|
2022-07-28 01:59:51 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t newMinSize = newMinCap * EltSize;
|
|
|
|
size_t newSize = RoundUpPow2(newMinSize);
|
|
|
|
return newSize / EltSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fallback version.
|
|
|
|
template <typename AP, size_t EltSize>
|
|
|
|
static size_t ComputeGrowth(size_t aOldElts, size_t aIncr, int) {
|
|
|
|
return GrowEltsByDoubling<EltSize>(aOldElts, aIncr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the AllocPolicy provides its own computeGrowth<EltSize> implementation,
|
|
|
|
// use that.
|
|
|
|
template <typename AP, size_t EltSize>
|
|
|
|
static size_t ComputeGrowth(
|
|
|
|
size_t aOldElts, size_t aIncr,
|
|
|
|
decltype(std::declval<AP>().template computeGrowth<EltSize>(0, 0),
|
|
|
|
bool()) aOverloadSelector) {
|
|
|
|
size_t newElts = AP::template computeGrowth<EltSize>(aOldElts, aIncr);
|
|
|
|
MOZ_ASSERT(newElts <= PTRDIFF_MAX && newElts * EltSize <= PTRDIFF_MAX,
|
|
|
|
"invalid Vector size (see bug 510319)");
|
|
|
|
return newElts;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This template class provides a default implementation for vector operations
|
|
|
|
* when the element type is not known to be a POD, as judged by IsPod.
|
|
|
|
*/
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP, bool IsPod>
|
2013-07-10 03:33:29 +04:00
|
|
|
struct VectorImpl {
|
2015-02-12 19:37:01 +03:00
|
|
|
/*
|
2016-04-27 02:26:17 +03:00
|
|
|
* Constructs an object in the uninitialized memory at *aDst with aArgs.
|
2015-02-12 19:37:01 +03:00
|
|
|
*/
|
2016-04-27 02:26:17 +03:00
|
|
|
template <typename... Args>
|
2015-02-12 19:37:01 +03:00
|
|
|
MOZ_NONNULL(1)
|
2016-04-27 02:26:17 +03:00
|
|
|
static inline void new_(T* aDst, Args&&... aArgs) {
|
2018-06-01 19:30:30 +03:00
|
|
|
new (KnownNotNull, aDst) T(std::forward<Args>(aArgs)...);
|
2015-02-12 19:37:01 +03:00
|
|
|
}
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Destroys constructed objects in the range [aBegin, aEnd). */
|
|
|
|
static inline void destroy(T* aBegin, T* aEnd) {
|
|
|
|
MOZ_ASSERT(aBegin <= aEnd);
|
|
|
|
for (T* p = aBegin; p < aEnd; ++p) {
|
|
|
|
p->~T();
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Constructs objects in the uninitialized range [aBegin, aEnd). */
|
|
|
|
static inline void initialize(T* aBegin, T* aEnd) {
|
|
|
|
MOZ_ASSERT(aBegin <= aEnd);
|
|
|
|
for (T* p = aBegin; p < aEnd; ++p) {
|
2015-02-12 19:37:01 +03:00
|
|
|
new_(p);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* Copy-constructs objects in the uninitialized range
|
|
|
|
* [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
|
|
|
|
*/
|
|
|
|
template <typename U>
|
|
|
|
static inline void copyConstruct(T* aDst, const U* aSrcStart,
|
|
|
|
const U* aSrcEnd) {
|
|
|
|
MOZ_ASSERT(aSrcStart <= aSrcEnd);
|
|
|
|
for (const U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
|
2015-02-12 19:37:01 +03:00
|
|
|
new_(aDst, *p);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* Move-constructs objects in the uninitialized range
|
|
|
|
* [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
|
|
|
|
*/
|
|
|
|
template <typename U>
|
|
|
|
static inline void moveConstruct(T* aDst, U* aSrcStart, U* aSrcEnd) {
|
|
|
|
MOZ_ASSERT(aSrcStart <= aSrcEnd);
|
|
|
|
for (U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
|
2018-05-30 22:15:35 +03:00
|
|
|
new_(aDst, std::move(*p));
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* Copy-constructs objects in the uninitialized range [aDst, aDst+aN) from
|
|
|
|
* the same object aU.
|
|
|
|
*/
|
|
|
|
template <typename U>
|
|
|
|
static inline void copyConstructN(T* aDst, size_t aN, const U& aU) {
|
|
|
|
for (T* end = aDst + aN; aDst < end; ++aDst) {
|
2015-02-12 19:37:01 +03:00
|
|
|
new_(aDst, aU);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* Grows the given buffer to have capacity aNewCap, preserving the objects
|
|
|
|
* constructed in the range [begin, end) and updating aV. Assumes that (1)
|
|
|
|
* aNewCap has not overflowed, and (2) multiplying aNewCap by sizeof(T) will
|
|
|
|
* not overflow.
|
|
|
|
*/
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] static inline bool growTo(Vector<T, N, AP>& aV,
|
|
|
|
size_t aNewCap) {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(!aV.usingInlineStorage());
|
2022-07-28 01:59:51 +03:00
|
|
|
MOZ_ASSERT(!CapacityHasExcessSpace<sizeof(T)>(aNewCap));
|
2014-08-09 00:22:39 +04:00
|
|
|
T* newbuf = aV.template pod_malloc<T>(aNewCap);
|
2015-02-12 19:37:01 +03:00
|
|
|
if (MOZ_UNLIKELY(!newbuf)) {
|
2014-07-11 06:10:17 +04:00
|
|
|
return false;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
T* dst = newbuf;
|
|
|
|
T* src = aV.beginNoCheck();
|
|
|
|
for (; src < aV.endNoCheck(); ++dst, ++src) {
|
2018-05-30 22:15:35 +03:00
|
|
|
new_(dst, std::move(*src));
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
|
|
|
VectorImpl::destroy(aV.beginNoCheck(), aV.endNoCheck());
|
2018-07-21 02:58:34 +03:00
|
|
|
aV.free_(aV.mBegin, aV.mTail.mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
aV.mBegin = newbuf;
|
|
|
|
/* aV.mLength is unchanged. */
|
2017-01-31 02:56:05 +03:00
|
|
|
aV.mTail.mCapacity = aNewCap;
|
2014-07-11 06:10:17 +04:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This partial template specialization provides a default implementation for
|
|
|
|
* vector operations when the element type is known to be a POD, as judged by
|
|
|
|
* IsPod.
|
|
|
|
*/
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
struct VectorImpl<T, N, AP, true> {
|
2016-04-27 02:26:17 +03:00
|
|
|
template <typename... Args>
|
|
|
|
MOZ_NONNULL(1)
|
|
|
|
static inline void new_(T* aDst, Args&&... aArgs) {
|
2016-06-02 22:38:02 +03:00
|
|
|
// Explicitly construct a local object instead of using a temporary since
|
|
|
|
// T(args...) will be treated like a C-style cast in the unary case and
|
|
|
|
// allow unsafe conversions. Both forms should be equivalent to an
|
|
|
|
// optimizing compiler.
|
2018-06-01 19:30:30 +03:00
|
|
|
T temp(std::forward<Args>(aArgs)...);
|
2016-06-02 22:38:02 +03:00
|
|
|
*aDst = temp;
|
2015-02-12 19:37:01 +03:00
|
|
|
}
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
static inline void destroy(T*, T*) {}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
static inline void initialize(T* aBegin, T* aEnd) {
|
|
|
|
/*
|
|
|
|
* You would think that memset would be a big win (or even break even)
|
|
|
|
* when we know T is a POD. But currently it's not. This is probably
|
|
|
|
* because |append| tends to be given small ranges and memset requires
|
|
|
|
* a function call that doesn't get inlined.
|
|
|
|
*
|
|
|
|
* memset(aBegin, 0, sizeof(T) * (aEnd - aBegin));
|
|
|
|
*/
|
|
|
|
MOZ_ASSERT(aBegin <= aEnd);
|
|
|
|
for (T* p = aBegin; p < aEnd; ++p) {
|
2015-02-12 19:37:01 +03:00
|
|
|
new_(p);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
template <typename U>
|
|
|
|
static inline void copyConstruct(T* aDst, const U* aSrcStart,
|
|
|
|
const U* aSrcEnd) {
|
|
|
|
/*
|
|
|
|
* See above memset comment. Also, notice that copyConstruct is
|
|
|
|
* currently templated (T != U), so memcpy won't work without
|
|
|
|
* requiring T == U.
|
|
|
|
*
|
|
|
|
* memcpy(aDst, aSrcStart, sizeof(T) * (aSrcEnd - aSrcStart));
|
|
|
|
*/
|
|
|
|
MOZ_ASSERT(aSrcStart <= aSrcEnd);
|
|
|
|
for (const U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
|
2015-02-12 19:37:01 +03:00
|
|
|
new_(aDst, *p);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
static inline void moveConstruct(T* aDst, const U* aSrcStart,
|
|
|
|
const U* aSrcEnd) {
|
|
|
|
copyConstruct(aDst, aSrcStart, aSrcEnd);
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
static inline void copyConstructN(T* aDst, size_t aN, const T& aT) {
|
|
|
|
for (T* end = aDst + aN; aDst < end; ++aDst) {
|
2015-02-12 19:37:01 +03:00
|
|
|
new_(aDst, aT);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] static inline bool growTo(Vector<T, N, AP>& aV,
|
|
|
|
size_t aNewCap) {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(!aV.usingInlineStorage());
|
2022-07-28 01:59:51 +03:00
|
|
|
MOZ_ASSERT(!CapacityHasExcessSpace<sizeof(T)>(aNewCap));
|
2017-01-31 02:56:05 +03:00
|
|
|
T* newbuf =
|
|
|
|
aV.template pod_realloc<T>(aV.mBegin, aV.mTail.mCapacity, aNewCap);
|
2015-02-12 19:37:01 +03:00
|
|
|
if (MOZ_UNLIKELY(!newbuf)) {
|
2014-07-11 06:10:17 +04:00
|
|
|
return false;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
aV.mBegin = newbuf;
|
|
|
|
/* aV.mLength is unchanged. */
|
2017-01-31 02:56:05 +03:00
|
|
|
aV.mTail.mCapacity = aNewCap;
|
2014-07-11 06:10:17 +04:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
};
|
|
|
|
|
2014-11-27 00:01:19 +03:00
|
|
|
// A struct for TestVector.cpp to access private internal fields.
|
|
|
|
// DO NOT DEFINE IN YOUR OWN CODE.
|
|
|
|
struct VectorTesting;
|
|
|
|
|
2013-07-10 03:33:29 +04:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/*
|
2015-07-13 22:42:52 +03:00
|
|
|
* STL-like container providing a short-lived, dynamic buffer. Vector calls the
|
|
|
|
* constructors/destructors of all elements stored in its internal buffer, so
|
|
|
|
* non-PODs may be safely used. Additionally, Vector will store the first N
|
|
|
|
* elements in-place before resorting to dynamic allocation.
|
|
|
|
*
|
|
|
|
* T requirements:
|
|
|
|
* - default and copy constructible, assignable, destructible
|
|
|
|
* - operations do not throw
|
|
|
|
* MinInlineCapacity requirements:
|
|
|
|
* - any value, however, MinInlineCapacity is clamped to min/max values
|
|
|
|
* AllocPolicy:
|
|
|
|
* - see "Allocation policies" in AllocPolicy.h (defaults to
|
|
|
|
* mozilla::MallocAllocPolicy)
|
2013-07-10 03:33:29 +04:00
|
|
|
*
|
2015-07-13 22:42:52 +03:00
|
|
|
* Vector is not reentrant: T member functions called during Vector member
|
|
|
|
* functions must not call back into the same object!
|
2013-07-10 03:33:29 +04:00
|
|
|
*/
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t MinInlineCapacity = 0,
|
|
|
|
class AllocPolicy = MallocAllocPolicy>
|
2017-01-31 02:56:05 +03:00
|
|
|
class MOZ_NON_PARAM Vector final : private AllocPolicy {
|
2014-07-11 06:10:17 +04:00
|
|
|
/* utilities */
|
2023-04-17 16:50:11 +03:00
|
|
|
static constexpr bool kElemIsPod =
|
2023-04-20 01:51:46 +03:00
|
|
|
std::is_trivial_v<T> && std::is_standard_layout_v<T>;
|
2015-07-13 22:42:52 +03:00
|
|
|
typedef detail::VectorImpl<T, MinInlineCapacity, AllocPolicy, kElemIsPod>
|
|
|
|
Impl;
|
|
|
|
friend struct detail::VectorImpl<T, MinInlineCapacity, AllocPolicy,
|
|
|
|
kElemIsPod>;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-11-27 00:01:19 +03:00
|
|
|
friend struct detail::VectorTesting;
|
|
|
|
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool growStorageBy(size_t aIncr);
|
|
|
|
[[nodiscard]] bool convertToHeapStorage(size_t aNewCap);
|
|
|
|
[[nodiscard]] bool maybeCheckSimulatedOOM(size_t aRequestedSize);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* magic constants */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
/**
|
|
|
|
* The maximum space allocated for inline element storage.
|
|
|
|
*
|
|
|
|
* We reduce space by what the AllocPolicy base class and prior Vector member
|
|
|
|
* fields likely consume to attempt to play well with binary size classes.
|
|
|
|
*/
|
|
|
|
static constexpr size_t kMaxInlineBytes =
|
|
|
|
1024 -
|
|
|
|
(sizeof(AllocPolicy) + sizeof(T*) + sizeof(size_t) + sizeof(size_t));
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
/**
|
|
|
|
* The number of T elements of inline capacity built into this Vector. This
|
|
|
|
* is usually |MinInlineCapacity|, but it may be less (or zero!) for large T.
|
2014-07-11 06:10:17 +04:00
|
|
|
*
|
2017-01-31 02:56:05 +03:00
|
|
|
* We use a partially-specialized template (not explicit specialization, which
|
|
|
|
* is only allowed at namespace scope) to compute this value. The benefit is
|
|
|
|
* that |sizeof(T)| need not be computed, and |T| doesn't have to be fully
|
|
|
|
* defined at the time |Vector<T>| appears, if no inline storage is requested.
|
2014-07-11 06:10:17 +04:00
|
|
|
*/
|
2017-01-31 02:56:05 +03:00
|
|
|
template <size_t MinimumInlineCapacity, size_t Dummy>
|
|
|
|
struct ComputeCapacity {
|
|
|
|
static constexpr size_t value =
|
|
|
|
tl::Min<MinimumInlineCapacity, kMaxInlineBytes / sizeof(T)>::value;
|
2014-07-11 06:10:17 +04:00
|
|
|
};
|
2017-01-31 02:56:05 +03:00
|
|
|
|
|
|
|
template <size_t Dummy>
|
|
|
|
struct ComputeCapacity<0, Dummy> {
|
|
|
|
static constexpr size_t value = 0;
|
2014-07-11 06:10:17 +04:00
|
|
|
};
|
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
/** The actual inline capacity in number of elements T. This may be zero! */
|
|
|
|
static constexpr size_t kInlineCapacity =
|
|
|
|
ComputeCapacity<MinInlineCapacity, 0>::value;
|
2014-07-11 06:10:17 +04:00
|
|
|
|
|
|
|
/* member data */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* Pointer to the buffer, be it inline or heap-allocated. Only [mBegin,
|
|
|
|
* mBegin + mLength) hold valid constructed T objects. The range [mBegin +
|
|
|
|
* mLength, mBegin + mCapacity) holds uninitialized memory. The range
|
|
|
|
* [mBegin + mLength, mBegin + mReserved) also holds uninitialized memory
|
|
|
|
* previously allocated by a call to reserve().
|
|
|
|
*/
|
|
|
|
T* mBegin;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Number of elements in the vector. */
|
|
|
|
size_t mLength;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
/*
|
2017-01-31 02:56:05 +03:00
|
|
|
* Memory used to store capacity, reserved element count (debug builds only),
|
|
|
|
* and inline storage. The simple "answer" is:
|
|
|
|
*
|
|
|
|
* size_t mCapacity;
|
|
|
|
* #ifdef DEBUG
|
|
|
|
* size_t mReserved;
|
|
|
|
* #endif
|
|
|
|
* alignas(T) unsigned char mBytes[kInlineCapacity * sizeof(T)];
|
2017-01-31 02:56:05 +03:00
|
|
|
*
|
2017-01-31 02:56:05 +03:00
|
|
|
* but there are complications. First, C++ forbids zero-sized arrays that
|
|
|
|
* might result. Second, we don't want zero capacity to affect Vector's size
|
|
|
|
* (even empty classes take up a byte, unless they're base classes).
|
2017-01-31 02:56:05 +03:00
|
|
|
*
|
2017-01-31 02:56:05 +03:00
|
|
|
* Yet again, we eliminate the zero-sized array using partial specialization.
|
|
|
|
* And we eliminate potential size hit by putting capacity/reserved in one
|
|
|
|
* struct, then putting the array (if any) in a derived struct. If no array
|
|
|
|
* is needed, the derived struct won't consume extra space.
|
2017-01-31 02:56:05 +03:00
|
|
|
*/
|
2017-01-31 02:56:05 +03:00
|
|
|
struct CapacityAndReserved {
|
|
|
|
explicit CapacityAndReserved(size_t aCapacity, size_t aReserved)
|
|
|
|
: mCapacity(aCapacity)
|
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mReserved(aReserved)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
|
|
|
CapacityAndReserved() = default;
|
|
|
|
|
|
|
|
/* Max number of elements storable in the vector without resizing. */
|
|
|
|
size_t mCapacity;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* Max elements of reserved or used space in this vector. */
|
|
|
|
size_t mReserved;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// Silence warnings about this struct possibly being padded dued to the
|
|
|
|
// alignas() in it -- there's nothing we can do to avoid it.
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning(push)
|
|
|
|
# pragma warning(disable : 4324)
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
template <size_t Capacity, size_t Dummy>
|
2017-01-31 02:56:05 +03:00
|
|
|
struct CRAndStorage : CapacityAndReserved {
|
|
|
|
explicit CRAndStorage(size_t aCapacity, size_t aReserved)
|
|
|
|
: CapacityAndReserved(aCapacity, aReserved) {}
|
|
|
|
CRAndStorage() = default;
|
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
alignas(T) unsigned char mBytes[Capacity * sizeof(T)];
|
|
|
|
|
|
|
|
// GCC fails due to -Werror=strict-aliasing if |mBytes| is directly cast to
|
|
|
|
// T*. Indirecting through this function addresses the problem.
|
|
|
|
void* data() { return mBytes; }
|
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
T* storage() { return static_cast<T*>(data()); }
|
2017-01-31 02:56:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t Dummy>
|
2017-01-31 02:56:05 +03:00
|
|
|
struct CRAndStorage<0, Dummy> : CapacityAndReserved {
|
|
|
|
explicit CRAndStorage(size_t aCapacity, size_t aReserved)
|
|
|
|
: CapacityAndReserved(aCapacity, aReserved) {}
|
|
|
|
CRAndStorage() = default;
|
|
|
|
|
2019-03-01 08:30:49 +03:00
|
|
|
T* storage() {
|
|
|
|
// If this returns |nullptr|, functions like |Vector::begin()| would too,
|
|
|
|
// breaking callers that pass a vector's elements as pointer/length to
|
|
|
|
// code that bounds its operation by length but (even just as a sanity
|
|
|
|
// check) always wants a non-null pointer. Fake up an aligned, non-null
|
|
|
|
// pointer to support these callers.
|
|
|
|
return reinterpret_cast<T*>(sizeof(T));
|
|
|
|
}
|
2017-01-31 02:56:05 +03:00
|
|
|
};
|
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
CRAndStorage<kInlineCapacity, 0> mTail;
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning(pop)
|
|
|
|
#endif // _MSC_VER
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
friend class ReentrancyGuard;
|
|
|
|
bool mEntered;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* private accessors */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
bool usingInlineStorage() const {
|
2015-07-13 22:42:52 +03:00
|
|
|
return mBegin == const_cast<Vector*>(this)->inlineStorage();
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
T* inlineStorage() { return mTail.storage(); }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* beginNoCheck() const { return mBegin; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* endNoCheck() { return mBegin + mLength; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T* endNoCheck() const { return mBegin + mLength; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-11-27 00:01:19 +03:00
|
|
|
/**
|
|
|
|
* The amount of explicitly allocated space in this vector that is immediately
|
|
|
|
* available to be filled by appending additional elements. This value is
|
|
|
|
* always greater than or equal to |length()| -- the vector's actual elements
|
|
|
|
* are implicitly reserved. This value is always less than or equal to
|
|
|
|
* |capacity()|. It may be explicitly increased using the |reserve()| method.
|
|
|
|
*/
|
2014-07-11 06:10:17 +04:00
|
|
|
size_t reserved() const {
|
2017-01-31 02:56:05 +03:00
|
|
|
MOZ_ASSERT(mLength <= mTail.mReserved);
|
|
|
|
MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
|
|
|
|
return mTail.mReserved;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
|
2020-10-29 22:41:43 +03:00
|
|
|
bool internalEnsureCapacity(size_t aNeeded);
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Append operations guaranteed to succeed due to pre-reserved space. */
|
|
|
|
template <typename U>
|
|
|
|
void internalAppend(U&& aU);
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename U, size_t O, class BP>
|
|
|
|
void internalAppendAll(const Vector<U, O, BP>& aU);
|
2014-07-11 06:10:17 +04:00
|
|
|
void internalAppendN(const T& aT, size_t aN);
|
|
|
|
template <typename U>
|
|
|
|
void internalAppend(const U* aBegin, size_t aLength);
|
2020-10-29 22:41:43 +03:00
|
|
|
template <typename U>
|
|
|
|
void internalMoveAppend(U* aBegin, size_t aLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
2015-07-13 22:42:52 +03:00
|
|
|
static const size_t sMaxInlineStorage = MinInlineCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
typedef T ElementType;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2023-04-20 01:51:46 +03:00
|
|
|
explicit Vector(AllocPolicy);
|
|
|
|
Vector() : Vector(AllocPolicy()) {}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
Vector(Vector&&); /* Move constructor. */
|
|
|
|
Vector& operator=(Vector&&); /* Move assignment. */
|
|
|
|
~Vector();
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* accessors */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const AllocPolicy& allocPolicy() const { return *this; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
AllocPolicy& allocPolicy() { return *this; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
enum { InlineLength = MinInlineCapacity };
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
size_t length() const { return mLength; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
bool empty() const { return mLength == 0; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2017-01-31 02:56:05 +03:00
|
|
|
size_t capacity() const { return mTail.mCapacity; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* begin() {
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
return mBegin;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T* begin() const {
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
return mBegin;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* end() {
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
return mBegin + mLength;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T* end() const {
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
return mBegin + mLength;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T& operator[](size_t aIndex) {
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
MOZ_ASSERT(aIndex < mLength);
|
|
|
|
return begin()[aIndex];
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T& operator[](size_t aIndex) const {
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
MOZ_ASSERT(aIndex < mLength);
|
|
|
|
return begin()[aIndex];
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T& back() {
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
return *(end() - 1);
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T& back() const {
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
return *(end() - 1);
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2019-09-26 15:45:00 +03:00
|
|
|
operator mozilla::Span<const T>() const {
|
2020-08-07 10:42:50 +03:00
|
|
|
// Explicitly specify template argument here to avoid instantiating Span<T>
|
|
|
|
// first and then implicitly converting to Span<const T>
|
|
|
|
return mozilla::Span<const T>{mBegin, mLength};
|
2019-09-26 15:45:00 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 10:42:50 +03:00
|
|
|
operator mozilla::Span<T>() { return mozilla::Span{mBegin, mLength}; }
|
2019-09-26 15:45:00 +03:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
class Range {
|
2015-07-13 22:42:52 +03:00
|
|
|
friend class Vector;
|
2014-07-11 06:10:17 +04:00
|
|
|
T* mCur;
|
|
|
|
T* mEnd;
|
|
|
|
Range(T* aCur, T* aEnd) : mCur(aCur), mEnd(aEnd) {
|
|
|
|
MOZ_ASSERT(aCur <= aEnd);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
|
|
|
bool empty() const { return mCur == mEnd; }
|
|
|
|
size_t remain() const { return PointerRangeSize(mCur, mEnd); }
|
|
|
|
T& front() const {
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
return *mCur;
|
|
|
|
}
|
|
|
|
void popFront() {
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
++mCur;
|
|
|
|
}
|
|
|
|
T popCopyFront() {
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
return *mCur++;
|
|
|
|
}
|
|
|
|
};
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2015-03-06 23:37:00 +03:00
|
|
|
class ConstRange {
|
2015-07-13 22:42:52 +03:00
|
|
|
friend class Vector;
|
2015-03-06 23:37:00 +03:00
|
|
|
const T* mCur;
|
|
|
|
const T* mEnd;
|
|
|
|
ConstRange(const T* aCur, const T* aEnd) : mCur(aCur), mEnd(aEnd) {
|
|
|
|
MOZ_ASSERT(aCur <= aEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool empty() const { return mCur == mEnd; }
|
|
|
|
size_t remain() const { return PointerRangeSize(mCur, mEnd); }
|
|
|
|
const T& front() const {
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
return *mCur;
|
|
|
|
}
|
|
|
|
void popFront() {
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
++mCur;
|
|
|
|
}
|
|
|
|
T popCopyFront() {
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
return *mCur++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
Range all() { return Range(begin(), end()); }
|
2015-03-06 23:37:00 +03:00
|
|
|
ConstRange all() const { return ConstRange(begin(), end()); }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* mutators */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2016-02-11 10:33:00 +03:00
|
|
|
/**
|
|
|
|
* Reverse the order of the elements in the vector in place.
|
|
|
|
*/
|
|
|
|
void reverse();
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
2016-06-10 21:28:04 +03:00
|
|
|
* Given that the vector is empty, grow the internal capacity to |aRequest|,
|
|
|
|
* keeping the length 0.
|
2014-07-11 06:10:17 +04:00
|
|
|
*/
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool initCapacity(size_t aRequest);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2016-06-10 21:28:04 +03:00
|
|
|
/**
|
|
|
|
* Given that the vector is empty, grow the internal capacity and length to
|
|
|
|
* |aRequest| leaving the elements' memory completely uninitialized (with all
|
|
|
|
* the associated hazards and caveats). This avoids the usual allocation-size
|
|
|
|
* rounding that happens in resize and overhead of initialization for elements
|
|
|
|
* that are about to be overwritten.
|
|
|
|
*/
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool initLengthUninitialized(size_t aRequest);
|
2016-06-10 21:28:04 +03:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
2014-11-27 00:01:19 +03:00
|
|
|
* If reserve(aRequest) succeeds and |aRequest >= length()|, then appending
|
|
|
|
* |aRequest - length()| elements, in any sequence of append/appendAll calls,
|
|
|
|
* is guaranteed to succeed.
|
|
|
|
*
|
|
|
|
* A request to reserve an amount less than the current length does not affect
|
|
|
|
* reserved space.
|
2014-07-11 06:10:17 +04:00
|
|
|
*/
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool reserve(size_t aRequest);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Destroy elements in the range [end() - aIncr, end()). Does not deallocate
|
|
|
|
* or unreserve storage for those elements.
|
|
|
|
*/
|
|
|
|
void shrinkBy(size_t aIncr);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2016-01-06 23:06:25 +03:00
|
|
|
/**
|
|
|
|
* Destroy elements in the range [aNewLength, end()). Does not deallocate
|
|
|
|
* or unreserve storage for those elements.
|
|
|
|
*/
|
|
|
|
void shrinkTo(size_t aNewLength);
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Grow the vector by aIncr elements. */
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool growBy(size_t aIncr);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Call shrinkBy or growBy based on whether newSize > length(). */
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool resize(size_t aNewLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Increase the length of the vector, but don't initialize the new elements
|
|
|
|
* -- leave them as uninitialized memory.
|
|
|
|
*/
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool growByUninitialized(size_t aIncr);
|
2014-10-09 02:04:12 +04:00
|
|
|
void infallibleGrowByUninitialized(size_t aIncr);
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool resizeUninitialized(size_t aNewLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Shorthand for shrinkBy(length()). */
|
|
|
|
void clear();
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Clears and releases any heap-allocated storage. */
|
|
|
|
void clearAndFree();
|
2013-11-19 21:05:36 +04:00
|
|
|
|
2016-05-09 16:59:10 +03:00
|
|
|
/**
|
2020-02-03 20:32:12 +03:00
|
|
|
* Shrinks the storage to drop excess capacity if possible.
|
|
|
|
*
|
|
|
|
* The return value indicates whether the operation succeeded, otherwise, it
|
|
|
|
* represents an OOM. The bool can be safely ignored unless you want to
|
|
|
|
* provide the guarantee that `length() == capacity()`.
|
|
|
|
*
|
|
|
|
* For PODs, it calls the AllocPolicy's pod_realloc. For non-PODs, it moves
|
|
|
|
* the elements into the new storage.
|
2016-05-09 16:59:10 +03:00
|
|
|
*/
|
2020-02-03 20:32:12 +03:00
|
|
|
bool shrinkStorageToFit();
|
2016-05-09 16:59:10 +03:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* If true, appending |aNeeded| elements won't reallocate elements storage.
|
|
|
|
* This *doesn't* mean that infallibleAppend may be used! You still must
|
|
|
|
* reserve the extra space, even if this method indicates that appends won't
|
|
|
|
* need to reallocate elements storage.
|
|
|
|
*/
|
|
|
|
bool canAppendWithoutRealloc(size_t aNeeded) const;
|
2013-11-19 21:05:36 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Potentially fallible append operations. */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* This can take either a T& or a T&&. Given a T&&, it moves |aU| into the
|
|
|
|
* vector, instead of copying it. If it fails, |aU| is left unmoved. ("We are
|
|
|
|
* not amused.")
|
|
|
|
*/
|
2016-04-27 07:16:50 +03:00
|
|
|
template <typename U>
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool append(U&& aU);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2015-07-20 21:13:28 +03:00
|
|
|
/**
|
|
|
|
* Construct a T in-place as a new entry at the end of this vector.
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool emplaceBack(Args&&... aArgs) {
|
2015-07-20 21:13:28 +03:00
|
|
|
if (!growByUninitialized(1)) return false;
|
2018-06-01 19:30:30 +03:00
|
|
|
Impl::new_(&back(), std::forward<Args>(aArgs)...);
|
2015-07-20 21:13:28 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename U, size_t O, class BP>
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool appendAll(const Vector<U, O, BP>& aU);
|
2020-10-29 22:41:43 +03:00
|
|
|
template <typename U, size_t O, class BP>
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool appendAll(Vector<U, O, BP>&& aU);
|
|
|
|
[[nodiscard]] bool appendN(const T& aT, size_t aN);
|
2016-04-27 07:16:50 +03:00
|
|
|
template <typename U>
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool append(const U* aBegin, const U* aEnd);
|
2016-04-27 07:16:50 +03:00
|
|
|
template <typename U>
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool append(const U* aBegin, size_t aLength);
|
2020-10-29 22:41:43 +03:00
|
|
|
template <typename U>
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool moveAppend(U* aBegin, U* aEnd);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* Guaranteed-infallible append operations for use upon vectors whose
|
|
|
|
* memory has been pre-reserved. Don't use this if you haven't reserved the
|
|
|
|
* memory!
|
|
|
|
*/
|
|
|
|
template <typename U>
|
|
|
|
void infallibleAppend(U&& aU) {
|
2018-06-01 19:30:30 +03:00
|
|
|
internalAppend(std::forward<U>(aU));
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
|
|
|
void infallibleAppendN(const T& aT, size_t aN) { internalAppendN(aT, aN); }
|
|
|
|
template <typename U>
|
|
|
|
void infallibleAppend(const U* aBegin, const U* aEnd) {
|
|
|
|
internalAppend(aBegin, PointerRangeSize(aBegin, aEnd));
|
|
|
|
}
|
|
|
|
template <typename U>
|
|
|
|
void infallibleAppend(const U* aBegin, size_t aLength) {
|
|
|
|
internalAppend(aBegin, aLength);
|
|
|
|
}
|
2015-10-06 16:50:49 +03:00
|
|
|
template <typename... Args>
|
|
|
|
void infallibleEmplaceBack(Args&&... aArgs) {
|
|
|
|
infallibleGrowByUninitialized(1);
|
2018-06-01 19:30:30 +03:00
|
|
|
Impl::new_(&back(), std::forward<Args>(aArgs)...);
|
2015-10-06 16:50:49 +03:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
void popBack();
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T popCopy();
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
2016-04-25 15:41:25 +03:00
|
|
|
* If elements are stored in-place, return nullptr and leave this vector
|
|
|
|
* unmodified.
|
|
|
|
*
|
|
|
|
* Otherwise return this vector's elements buffer, and clear this vector as if
|
|
|
|
* by clearAndFree(). The caller now owns the buffer and is responsible for
|
|
|
|
* deallocating it consistent with this vector's AllocPolicy.
|
2014-07-11 06:10:17 +04:00
|
|
|
*
|
|
|
|
* N.B. Although a T*, only the range [0, length()) is constructed.
|
|
|
|
*/
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] T* extractRawBuffer();
|
2016-04-25 15:41:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If elements are stored in-place, allocate a new buffer, move this vector's
|
|
|
|
* elements into it, and return that buffer.
|
|
|
|
*
|
|
|
|
* Otherwise return this vector's elements buffer. The caller now owns the
|
|
|
|
* buffer and is responsible for deallocating it consistent with this vector's
|
|
|
|
* AllocPolicy.
|
|
|
|
*
|
|
|
|
* This vector is cleared, as if by clearAndFree(), when this method
|
|
|
|
* succeeds. This method fails and returns nullptr only if new elements buffer
|
|
|
|
* allocation fails.
|
|
|
|
*
|
|
|
|
* N.B. Only the range [0, length()) of the returned buffer is constructed.
|
|
|
|
* If any of these elements are uninitialized (as growByUninitialized
|
|
|
|
* enables), behavior is undefined.
|
|
|
|
*/
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] T* extractOrCopyRawBuffer();
|
2014-07-11 06:10:17 +04:00
|
|
|
|
2017-09-22 18:34:24 +03:00
|
|
|
/**
|
|
|
|
* Transfer ownership of an array of objects into the vector. The caller
|
|
|
|
* must have allocated the array in accordance with this vector's
|
|
|
|
* AllocPolicy.
|
|
|
|
*
|
|
|
|
* N.B. This call assumes that there are no uninitialized elements in the
|
|
|
|
* passed range [aP, aP + aLength). The range [aP + aLength, aP +
|
|
|
|
* aCapacity) must be allocated uninitialized memory.
|
|
|
|
*/
|
|
|
|
void replaceRawBuffer(T* aP, size_t aLength, size_t aCapacity);
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Transfer ownership of an array of objects into the vector. The caller
|
|
|
|
* must have allocated the array in accordance with this vector's
|
|
|
|
* AllocPolicy.
|
|
|
|
*
|
|
|
|
* N.B. This call assumes that there are no uninitialized elements in the
|
|
|
|
* passed array.
|
|
|
|
*/
|
|
|
|
void replaceRawBuffer(T* aP, size_t aLength);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Places |aVal| at position |aP|, shifting existing elements from |aP| onward
|
|
|
|
* one position higher. On success, |aP| should not be reused because it'll
|
|
|
|
* be a dangling pointer if reallocation of the vector storage occurred; the
|
|
|
|
* return value should be used instead. On failure, nullptr is returned.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
|
|
|
* if (!(p = vec.insert(p, val))) {
|
|
|
|
* <handle failure>
|
|
|
|
* }
|
|
|
|
* <keep working with p>
|
|
|
|
*
|
|
|
|
* This is inherently a linear-time operation. Be careful!
|
|
|
|
*/
|
|
|
|
template <typename U>
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] T* insert(T* aP, U&& aVal);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Removes the element |aT|, which must fall in the bounds [begin, end),
|
|
|
|
* shifting existing elements from |aT + 1| onward one position lower.
|
|
|
|
*/
|
|
|
|
void erase(T* aT);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Removes the elements [|aBegin|, |aEnd|), which must fall in the bounds
|
2019-10-15 20:38:13 +03:00
|
|
|
* [begin, end), shifting existing elements from |aEnd| onward to aBegin's old
|
|
|
|
* position.
|
2014-07-11 06:10:17 +04:00
|
|
|
*/
|
|
|
|
void erase(T* aBegin, T* aEnd);
|
2014-05-21 00:36:40 +04:00
|
|
|
|
2019-03-28 04:00:35 +03:00
|
|
|
/**
|
|
|
|
* Removes all elements that satisfy the predicate, shifting existing elements
|
|
|
|
* lower to fill erased gaps.
|
|
|
|
*/
|
|
|
|
template <typename Pred>
|
|
|
|
void eraseIf(Pred aPred);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all elements that compare equal to |aU|, shifting existing elements
|
|
|
|
* lower to fill erased gaps.
|
|
|
|
*/
|
|
|
|
template <typename U>
|
|
|
|
void eraseIfEqual(const U& aU);
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Measure the size of the vector's heap-allocated storage.
|
|
|
|
*/
|
|
|
|
size_t sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Like sizeOfExcludingThis, but also measures the size of the vector
|
|
|
|
* object (which must be heap-allocated) itself.
|
|
|
|
*/
|
|
|
|
size_t sizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
void swap(Vector& aOther);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
private:
|
2015-07-13 22:42:52 +03:00
|
|
|
Vector(const Vector&) = delete;
|
|
|
|
void operator=(const Vector&) = delete;
|
2013-07-10 03:33:29 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This does the re-entrancy check plus several other sanity checks. */
|
|
|
|
#define MOZ_REENTRANCY_GUARD_ET_AL \
|
|
|
|
ReentrancyGuard g(*this); \
|
2017-01-31 02:56:05 +03:00
|
|
|
MOZ_ASSERT_IF(usingInlineStorage(), mTail.mCapacity == kInlineCapacity); \
|
|
|
|
MOZ_ASSERT(reserved() <= mTail.mCapacity); \
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_ASSERT(mLength <= reserved()); \
|
2017-01-31 02:56:05 +03:00
|
|
|
MOZ_ASSERT(mLength <= mTail.mCapacity)
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
/* Vector Implementation */
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE Vector<T, N, AP>::Vector(AP aAP)
|
2019-06-27 13:56:26 +03:00
|
|
|
: AP(std::move(aAP)),
|
2014-07-11 06:10:17 +04:00
|
|
|
mLength(0),
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail(kInlineCapacity, 0)
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
,
|
|
|
|
mEntered(false)
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
2013-10-18 01:29:46 +04:00
|
|
|
{
|
2017-01-31 02:56:05 +03:00
|
|
|
mBegin = inlineStorage();
|
2013-10-18 01:29:46 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
/* Move constructor. */
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AllocPolicy>
|
|
|
|
MOZ_ALWAYS_INLINE Vector<T, N, AllocPolicy>::Vector(Vector&& aRhs)
|
2018-05-30 22:15:35 +03:00
|
|
|
: AllocPolicy(std::move(aRhs))
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
,
|
|
|
|
mEntered(false)
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
mLength = aRhs.mLength;
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mCapacity = aRhs.mTail.mCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mReserved = aRhs.mTail.mReserved;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aRhs.usingInlineStorage()) {
|
2013-07-10 03:33:29 +04:00
|
|
|
/* We can't move the buffer over in this case, so copy elements. */
|
2017-01-31 02:56:05 +03:00
|
|
|
mBegin = inlineStorage();
|
2014-07-11 06:10:17 +04:00
|
|
|
Impl::moveConstruct(mBegin, aRhs.beginNoCheck(), aRhs.endNoCheck());
|
2013-07-10 03:33:29 +04:00
|
|
|
/*
|
2014-07-11 06:10:17 +04:00
|
|
|
* Leave aRhs's mLength, mBegin, mCapacity, and mReserved as they are.
|
2013-07-10 03:33:29 +04:00
|
|
|
* The elements in its in-line storage still need to be destroyed.
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Take src's buffer, and turn src into an empty vector using
|
|
|
|
* in-line storage.
|
|
|
|
*/
|
2014-07-11 06:10:17 +04:00
|
|
|
mBegin = aRhs.mBegin;
|
2017-01-31 02:56:05 +03:00
|
|
|
aRhs.mBegin = aRhs.inlineStorage();
|
2017-01-31 02:56:05 +03:00
|
|
|
aRhs.mTail.mCapacity = kInlineCapacity;
|
2014-07-11 06:10:17 +04:00
|
|
|
aRhs.mLength = 0;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
aRhs.mTail.mReserved = 0;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move assignment. */
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE Vector<T, N, AP>& Vector<T, N, AP>::operator=(Vector&& aRhs) {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited");
|
2015-07-13 22:42:52 +03:00
|
|
|
this->~Vector();
|
2018-05-30 22:15:35 +03:00
|
|
|
new (KnownNotNull, this) Vector(std::move(aRhs));
|
2015-07-13 22:42:52 +03:00
|
|
|
return *this;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE Vector<T, N, AP>::~Vector() {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!usingInlineStorage()) {
|
2018-07-21 02:58:34 +03:00
|
|
|
this->free_(beginNoCheck(), mTail.mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2016-02-11 10:33:00 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE void Vector<T, N, AP>::reverse() {
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
T* elems = mBegin;
|
|
|
|
size_t len = mLength;
|
|
|
|
size_t mid = len / 2;
|
|
|
|
for (size_t i = 0; i < mid; i++) {
|
2020-01-20 19:17:06 +03:00
|
|
|
std::swap(elems[i], elems[len - i - 1]);
|
2016-02-11 10:33:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 03:33:29 +04:00
|
|
|
/*
|
2014-07-11 06:10:17 +04:00
|
|
|
* This function will create a new heap buffer with capacity aNewCap,
|
2013-07-10 03:33:29 +04:00
|
|
|
* move all elements in the inline buffer to this new buffer,
|
|
|
|
* and fail on OOM.
|
|
|
|
*/
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline bool Vector<T, N, AP>::convertToHeapStorage(size_t aNewCap) {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_ASSERT(usingInlineStorage());
|
|
|
|
|
|
|
|
/* Allocate buffer. */
|
2022-07-28 01:59:51 +03:00
|
|
|
MOZ_ASSERT(!detail::CapacityHasExcessSpace<sizeof(T)>(aNewCap));
|
2014-08-09 00:22:39 +04:00
|
|
|
T* newBuf = this->template pod_malloc<T>(aNewCap);
|
2015-02-12 19:37:01 +03:00
|
|
|
if (MOZ_UNLIKELY(!newBuf)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
/* Copy inline elements into heap buffer. */
|
|
|
|
Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
|
|
|
|
/* Switch in heap buffer. */
|
|
|
|
mBegin = newBuf;
|
|
|
|
/* mLength is unchanged. */
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mCapacity = aNewCap;
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_NEVER_INLINE bool Vector<T, N, AP>::growStorageBy(size_t aIncr) {
|
2017-01-31 02:56:05 +03:00
|
|
|
MOZ_ASSERT(mLength + aIncr > mTail.mCapacity);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
size_t newCap;
|
|
|
|
|
2022-07-28 01:59:51 +03:00
|
|
|
if (aIncr == 1 && usingInlineStorage()) {
|
|
|
|
/* This case occurs in ~70--80% of the calls to this function. */
|
|
|
|
constexpr size_t newSize =
|
|
|
|
tl::RoundUpPow2<(kInlineCapacity + 1) * sizeof(T)>::value;
|
|
|
|
static_assert(newSize / sizeof(T) > 0,
|
|
|
|
"overflow when exceeding inline Vector storage");
|
|
|
|
newCap = newSize / sizeof(T);
|
2013-07-10 03:33:29 +04:00
|
|
|
} else {
|
2022-07-28 01:59:51 +03:00
|
|
|
newCap = detail::ComputeGrowth<AP, sizeof(T)>(mLength, aIncr, true);
|
|
|
|
if (MOZ_UNLIKELY(newCap == 0)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
this->reportAllocOverflow();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usingInlineStorage()) {
|
|
|
|
return convertToHeapStorage(newCap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Impl::growTo(*this, newCap);
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline bool Vector<T, N, AP>::initCapacity(size_t aRequest) {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_ASSERT(empty());
|
|
|
|
MOZ_ASSERT(usingInlineStorage());
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aRequest == 0) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2014-08-09 00:22:39 +04:00
|
|
|
T* newbuf = this->template pod_malloc<T>(aRequest);
|
2015-02-12 19:37:01 +03:00
|
|
|
if (MOZ_UNLIKELY(!newbuf)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
mBegin = newbuf;
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mCapacity = aRequest;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mReserved = aRequest;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-10 21:28:04 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline bool Vector<T, N, AP>::initLengthUninitialized(size_t aRequest) {
|
|
|
|
if (!initCapacity(aRequest)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
infallibleGrowByUninitialized(aRequest);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-11 22:00:18 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline bool Vector<T, N, AP>::maybeCheckSimulatedOOM(size_t aRequestedSize) {
|
|
|
|
if (aRequestedSize <= N) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
if (aRequestedSize <= mTail.mReserved) {
|
2016-02-11 22:00:18 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return allocPolicy().checkSimulatedOOM();
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline bool Vector<T, N, AP>::reserve(size_t aRequest) {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2017-01-31 02:56:05 +03:00
|
|
|
if (aRequest > mTail.mCapacity) {
|
2015-09-30 13:34:48 +03:00
|
|
|
if (MOZ_UNLIKELY(!growStorageBy(aRequest - mLength))) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-11 22:00:18 +03:00
|
|
|
} else if (!maybeCheckSimulatedOOM(aRequest)) {
|
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
if (aRequest > mTail.mReserved) {
|
|
|
|
mTail.mReserved = aRequest;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2017-01-31 02:56:05 +03:00
|
|
|
MOZ_ASSERT(mLength <= mTail.mReserved);
|
|
|
|
MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline void Vector<T, N, AP>::shrinkBy(size_t aIncr) {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(aIncr <= mLength);
|
|
|
|
Impl::destroy(endNoCheck() - aIncr, endNoCheck());
|
|
|
|
mLength -= aIncr;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2016-01-06 23:06:25 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE void Vector<T, N, AP>::shrinkTo(size_t aNewLength) {
|
|
|
|
MOZ_ASSERT(aNewLength <= mLength);
|
|
|
|
shrinkBy(mLength - aNewLength);
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::growBy(size_t aIncr) {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2017-01-31 02:56:05 +03:00
|
|
|
if (aIncr > mTail.mCapacity - mLength) {
|
2015-09-30 13:34:48 +03:00
|
|
|
if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-11 22:00:18 +03:00
|
|
|
} else if (!maybeCheckSimulatedOOM(mLength + aIncr)) {
|
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2017-01-31 02:56:05 +03:00
|
|
|
MOZ_ASSERT(mLength + aIncr <= mTail.mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
T* newend = endNoCheck() + aIncr;
|
2013-07-10 03:33:29 +04:00
|
|
|
Impl::initialize(endNoCheck(), newend);
|
2014-07-11 06:10:17 +04:00
|
|
|
mLength += aIncr;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
if (mLength > mTail.mReserved) {
|
|
|
|
mTail.mReserved = mLength;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::growByUninitialized(size_t aIncr) {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2017-01-31 02:56:05 +03:00
|
|
|
if (aIncr > mTail.mCapacity - mLength) {
|
2015-09-30 13:34:48 +03:00
|
|
|
if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-11 22:00:18 +03:00
|
|
|
} else if (!maybeCheckSimulatedOOM(mLength + aIncr)) {
|
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2016-04-14 18:32:48 +03:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
if (mLength + aIncr > mTail.mReserved) {
|
|
|
|
mTail.mReserved = mLength + aIncr;
|
2016-04-14 18:32:48 +03:00
|
|
|
}
|
|
|
|
#endif
|
2014-10-09 02:04:12 +04:00
|
|
|
infallibleGrowByUninitialized(aIncr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE void Vector<T, N, AP>::infallibleGrowByUninitialized(
|
|
|
|
size_t aIncr) {
|
2016-04-14 18:32:48 +03:00
|
|
|
MOZ_ASSERT(mLength + aIncr <= reserved());
|
2014-07-11 06:10:17 +04:00
|
|
|
mLength += aIncr;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline bool Vector<T, N, AP>::resize(size_t aNewLength) {
|
2013-07-10 03:33:29 +04:00
|
|
|
size_t curLength = mLength;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aNewLength > curLength) {
|
|
|
|
return growBy(aNewLength - curLength);
|
|
|
|
}
|
|
|
|
shrinkBy(curLength - aNewLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::resizeUninitialized(
|
|
|
|
size_t aNewLength) {
|
2013-07-10 03:33:29 +04:00
|
|
|
size_t curLength = mLength;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aNewLength > curLength) {
|
|
|
|
return growByUninitialized(aNewLength - curLength);
|
|
|
|
}
|
|
|
|
shrinkBy(curLength - aNewLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline void Vector<T, N, AP>::clear() {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
mLength = 0;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline void Vector<T, N, AP>::clearAndFree() {
|
2013-07-10 03:33:29 +04:00
|
|
|
clear();
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
if (usingInlineStorage()) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2018-07-21 02:58:34 +03:00
|
|
|
this->free_(beginNoCheck(), mTail.mCapacity);
|
2017-01-31 02:56:05 +03:00
|
|
|
mBegin = inlineStorage();
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mCapacity = kInlineCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mReserved = 0;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-09 16:59:10 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
2020-02-03 20:32:12 +03:00
|
|
|
inline bool Vector<T, N, AP>::shrinkStorageToFit() {
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
|
|
|
|
const auto length = this->length();
|
|
|
|
if (usingInlineStorage() || length == capacity()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!length) {
|
|
|
|
this->free_(beginNoCheck(), mTail.mCapacity);
|
|
|
|
mBegin = inlineStorage();
|
|
|
|
mTail.mCapacity = kInlineCapacity;
|
|
|
|
#ifdef DEBUG
|
|
|
|
mTail.mReserved = 0;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* newBuf;
|
|
|
|
size_t newCap;
|
|
|
|
if (length <= kInlineCapacity) {
|
|
|
|
newBuf = inlineStorage();
|
|
|
|
newCap = kInlineCapacity;
|
|
|
|
} else {
|
|
|
|
if (kElemIsPod) {
|
|
|
|
newBuf = this->template pod_realloc<T>(beginNoCheck(), mTail.mCapacity,
|
|
|
|
length);
|
|
|
|
} else {
|
|
|
|
newBuf = this->template pod_malloc<T>(length);
|
|
|
|
}
|
|
|
|
if (MOZ_UNLIKELY(!newBuf)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
newCap = length;
|
|
|
|
}
|
|
|
|
if (!kElemIsPod || newBuf == inlineStorage()) {
|
|
|
|
Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
}
|
|
|
|
if (!kElemIsPod) {
|
|
|
|
this->free_(beginNoCheck(), mTail.mCapacity);
|
|
|
|
}
|
|
|
|
mBegin = newBuf;
|
|
|
|
mTail.mCapacity = newCap;
|
|
|
|
#ifdef DEBUG
|
|
|
|
mTail.mReserved = length;
|
|
|
|
#endif
|
|
|
|
return true;
|
2016-05-09 16:59:10 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline bool Vector<T, N, AP>::canAppendWithoutRealloc(size_t aNeeded) const {
|
2017-01-31 02:56:05 +03:00
|
|
|
return mLength + aNeeded <= mTail.mCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
template <typename U, size_t O, class BP>
|
|
|
|
MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppendAll(
|
|
|
|
const Vector<U, O, BP>& aOther) {
|
2014-07-11 06:10:17 +04:00
|
|
|
internalAppend(aOther.begin(), aOther.length());
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
2013-07-10 03:33:29 +04:00
|
|
|
template <typename U>
|
2015-07-13 22:42:52 +03:00
|
|
|
MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppend(U&& aU) {
|
2017-01-31 02:56:05 +03:00
|
|
|
MOZ_ASSERT(mLength + 1 <= mTail.mReserved);
|
|
|
|
MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
|
2018-06-01 19:30:30 +03:00
|
|
|
Impl::new_(endNoCheck(), std::forward<U>(aU));
|
2013-07-10 03:33:29 +04:00
|
|
|
++mLength;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::appendN(const T& aT, size_t aNeeded) {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2017-01-31 02:56:05 +03:00
|
|
|
if (mLength + aNeeded > mTail.mCapacity) {
|
2015-09-30 13:34:48 +03:00
|
|
|
if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-11 22:00:18 +03:00
|
|
|
} else if (!maybeCheckSimulatedOOM(mLength + aNeeded)) {
|
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
if (mLength + aNeeded > mTail.mReserved) {
|
|
|
|
mTail.mReserved = mLength + aNeeded;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
2014-07-11 06:10:17 +04:00
|
|
|
internalAppendN(aT, aNeeded);
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppendN(const T& aT,
|
|
|
|
size_t aNeeded) {
|
2017-01-31 02:56:05 +03:00
|
|
|
MOZ_ASSERT(mLength + aNeeded <= mTail.mReserved);
|
|
|
|
MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
Impl::copyConstructN(endNoCheck(), aNeeded, aT);
|
|
|
|
mLength += aNeeded;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
2013-11-19 21:05:36 +04:00
|
|
|
template <typename U>
|
2015-07-13 22:42:52 +03:00
|
|
|
inline T* Vector<T, N, AP>::insert(T* aP, U&& aVal) {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(begin() <= aP);
|
|
|
|
MOZ_ASSERT(aP <= end());
|
|
|
|
size_t pos = aP - begin();
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_ASSERT(pos <= mLength);
|
|
|
|
size_t oldLength = mLength;
|
|
|
|
if (pos == oldLength) {
|
2018-06-01 19:30:30 +03:00
|
|
|
if (!append(std::forward<U>(aVal))) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return nullptr;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
} else {
|
2018-05-30 22:15:35 +03:00
|
|
|
T oldBack = std::move(back());
|
|
|
|
if (!append(std::move(oldBack))) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return nullptr;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2017-03-30 17:20:30 +03:00
|
|
|
for (size_t i = oldLength - 1; i > pos; --i) {
|
2018-05-30 22:15:35 +03:00
|
|
|
(*this)[i] = std::move((*this)[i - 1]);
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2018-06-01 19:30:30 +03:00
|
|
|
(*this)[pos] = std::forward<U>(aVal);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
return begin() + pos;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline void Vector<T, N, AP>::erase(T* aIt) {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(begin() <= aIt);
|
|
|
|
MOZ_ASSERT(aIt < end());
|
|
|
|
while (aIt + 1 < end()) {
|
2018-05-30 22:15:35 +03:00
|
|
|
*aIt = std::move(*(aIt + 1));
|
2014-07-11 06:10:17 +04:00
|
|
|
++aIt;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-05-21 00:36:40 +04:00
|
|
|
popBack();
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline void Vector<T, N, AP>::erase(T* aBegin, T* aEnd) {
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(begin() <= aBegin);
|
|
|
|
MOZ_ASSERT(aBegin <= aEnd);
|
|
|
|
MOZ_ASSERT(aEnd <= end());
|
|
|
|
while (aEnd < end()) {
|
2018-05-30 22:15:35 +03:00
|
|
|
*aBegin++ = std::move(*aEnd++);
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
|
|
|
shrinkBy(aEnd - aBegin);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2019-03-28 04:00:35 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
template <typename Pred>
|
|
|
|
void Vector<T, N, AP>::eraseIf(Pred aPred) {
|
|
|
|
// remove_if finds the first element to be erased, and then efficiently move-
|
|
|
|
// assigns elements to effectively overwrite elements that satisfy the
|
|
|
|
// predicate. It returns the new end pointer, after which there are only
|
|
|
|
// moved-from elements ready to be destroyed, so we just need to shrink the
|
|
|
|
// vector accordingly.
|
|
|
|
T* newEnd = std::remove_if(begin(), end(),
|
|
|
|
[&aPred](const T& aT) { return aPred(aT); });
|
|
|
|
MOZ_ASSERT(newEnd <= end());
|
|
|
|
shrinkBy(end() - newEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
template <typename U>
|
|
|
|
void Vector<T, N, AP>::eraseIfEqual(const U& aU) {
|
|
|
|
return eraseIf([&aU](const T& aT) { return aT == aU; });
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
2020-10-29 22:41:43 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::internalEnsureCapacity(
|
|
|
|
size_t aNeeded) {
|
2017-01-31 02:56:05 +03:00
|
|
|
if (mLength + aNeeded > mTail.mCapacity) {
|
2015-09-30 13:34:48 +03:00
|
|
|
if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-11 22:00:18 +03:00
|
|
|
} else if (!maybeCheckSimulatedOOM(mLength + aNeeded)) {
|
2015-09-30 13:34:48 +03:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
if (mLength + aNeeded > mTail.mReserved) {
|
|
|
|
mTail.mReserved = mLength + aNeeded;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
2020-10-29 22:41:43 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
template <typename U>
|
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::append(const U* aInsBegin,
|
|
|
|
const U* aInsEnd) {
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
const size_t needed = PointerRangeSize(aInsBegin, aInsEnd);
|
|
|
|
if (!internalEnsureCapacity(needed)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
internalAppend(aInsBegin, needed);
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
2013-07-10 03:33:29 +04:00
|
|
|
template <typename U>
|
2015-07-13 22:42:52 +03:00
|
|
|
MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppend(const U* aInsBegin,
|
|
|
|
size_t aInsLength) {
|
2017-01-31 02:56:05 +03:00
|
|
|
MOZ_ASSERT(mLength + aInsLength <= mTail.mReserved);
|
|
|
|
MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
Impl::copyConstruct(endNoCheck(), aInsBegin, aInsBegin + aInsLength);
|
|
|
|
mLength += aInsLength;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2020-10-29 22:41:43 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
template <typename U>
|
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::moveAppend(U* aInsBegin, U* aInsEnd) {
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
const size_t needed = PointerRangeSize(aInsBegin, aInsEnd);
|
|
|
|
if (!internalEnsureCapacity(needed)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
internalMoveAppend(aInsBegin, needed);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
template <typename U>
|
|
|
|
MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalMoveAppend(U* aInsBegin,
|
|
|
|
size_t aInsLength) {
|
|
|
|
MOZ_ASSERT(mLength + aInsLength <= mTail.mReserved);
|
|
|
|
MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
|
|
|
|
Impl::moveConstruct(endNoCheck(), aInsBegin, aInsBegin + aInsLength);
|
|
|
|
mLength += aInsLength;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
2013-07-10 03:33:29 +04:00
|
|
|
template <typename U>
|
2015-07-13 22:42:52 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::append(U&& aU) {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2017-01-31 02:56:05 +03:00
|
|
|
if (mLength == mTail.mCapacity) {
|
2015-09-30 13:34:48 +03:00
|
|
|
if (MOZ_UNLIKELY(!growStorageBy(1))) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-11 22:00:18 +03:00
|
|
|
} else if (!maybeCheckSimulatedOOM(mLength + 1)) {
|
2015-09-30 13:34:48 +03:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
if (mLength + 1 > mTail.mReserved) {
|
|
|
|
mTail.mReserved = mLength + 1;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
2018-06-01 19:30:30 +03:00
|
|
|
internalAppend(std::forward<U>(aU));
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
template <typename U, size_t O, class BP>
|
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::appendAll(
|
|
|
|
const Vector<U, O, BP>& aOther) {
|
2014-07-11 06:10:17 +04:00
|
|
|
return append(aOther.begin(), aOther.length());
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2020-10-29 22:41:43 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
template <typename U, size_t O, class BP>
|
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::appendAll(Vector<U, O, BP>&& aOther) {
|
|
|
|
if (empty() && capacity() < aOther.length()) {
|
|
|
|
*this = std::move(aOther);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moveAppend(aOther.begin(), aOther.end())) {
|
|
|
|
aOther.clearAndFree();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
2013-07-10 03:33:29 +04:00
|
|
|
template <class U>
|
2015-07-13 22:42:52 +03:00
|
|
|
MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::append(const U* aInsBegin,
|
|
|
|
size_t aInsLength) {
|
2014-07-11 06:10:17 +04:00
|
|
|
return append(aInsBegin, aInsBegin + aInsLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE void Vector<T, N, AP>::popBack() {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
--mLength;
|
|
|
|
endNoCheck()->~T();
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
MOZ_ALWAYS_INLINE T Vector<T, N, AP>::popCopy() {
|
2013-07-10 03:33:29 +04:00
|
|
|
T ret = back();
|
|
|
|
popBack();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
2016-04-25 15:41:25 +03:00
|
|
|
inline T* Vector<T, N, AP>::extractRawBuffer() {
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
|
2013-07-10 03:33:29 +04:00
|
|
|
if (usingInlineStorage()) {
|
2016-04-25 15:41:25 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* ret = mBegin;
|
2017-01-31 02:56:05 +03:00
|
|
|
mBegin = inlineStorage();
|
2016-04-25 15:41:25 +03:00
|
|
|
mLength = 0;
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mCapacity = kInlineCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mReserved = 0;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-04-25 15:41:25 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline T* Vector<T, N, AP>::extractOrCopyRawBuffer() {
|
|
|
|
if (T* ret = extractRawBuffer()) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
|
|
|
|
T* copy = this->template pod_malloc<T>(mLength);
|
|
|
|
if (!copy) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Impl::moveConstruct(copy, beginNoCheck(), endNoCheck());
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
2017-01-31 02:56:05 +03:00
|
|
|
mBegin = inlineStorage();
|
2016-04-25 15:41:25 +03:00
|
|
|
mLength = 0;
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mCapacity = kInlineCapacity;
|
2016-04-25 15:41:25 +03:00
|
|
|
#ifdef DEBUG
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mReserved = 0;
|
2016-04-25 15:41:25 +03:00
|
|
|
#endif
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
2017-09-22 18:34:24 +03:00
|
|
|
inline void Vector<T, N, AP>::replaceRawBuffer(T* aP, size_t aLength,
|
|
|
|
size_t aCapacity) {
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
|
|
|
|
/* Destroy what we have. */
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!usingInlineStorage()) {
|
2018-07-21 02:58:34 +03:00
|
|
|
this->free_(beginNoCheck(), mTail.mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
/* Take in the new buffer. */
|
2017-09-22 18:34:24 +03:00
|
|
|
if (aCapacity <= kInlineCapacity) {
|
2013-07-10 03:33:29 +04:00
|
|
|
/*
|
2014-07-11 06:10:17 +04:00
|
|
|
* We convert to inline storage if possible, even though aP might
|
2013-07-10 03:33:29 +04:00
|
|
|
* otherwise be acceptable. Maybe this behaviour should be
|
|
|
|
* specifiable with an argument to this function.
|
|
|
|
*/
|
2017-01-31 02:56:05 +03:00
|
|
|
mBegin = inlineStorage();
|
2013-07-10 03:33:29 +04:00
|
|
|
mLength = aLength;
|
2017-01-31 02:56:05 +03:00
|
|
|
mTail.mCapacity = kInlineCapacity;
|
2014-07-11 06:10:17 +04:00
|
|
|
Impl::moveConstruct(mBegin, aP, aP + aLength);
|
|
|
|
Impl::destroy(aP, aP + aLength);
|
2018-07-21 02:58:34 +03:00
|
|
|
this->free_(aP, aCapacity);
|
2013-07-10 03:33:29 +04:00
|
|
|
} else {
|
2014-07-11 06:10:17 +04:00
|
|
|
mBegin = aP;
|
2013-07-10 03:33:29 +04:00
|
|
|
mLength = aLength;
|
2017-09-22 18:34:24 +03:00
|
|
|
mTail.mCapacity = aCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2017-09-22 18:34:24 +03:00
|
|
|
mTail.mReserved = aCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-22 18:34:24 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline void Vector<T, N, AP>::replaceRawBuffer(T* aP, size_t aLength) {
|
|
|
|
replaceRawBuffer(aP, aLength, aLength);
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline size_t Vector<T, N, AP>::sizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
2014-07-11 06:10:17 +04:00
|
|
|
return usingInlineStorage() ? 0 : aMallocSizeOf(beginNoCheck());
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline size_t Vector<T, N, AP>::sizeOfIncludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
2014-07-11 06:10:17 +04:00
|
|
|
return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:42:52 +03:00
|
|
|
template <typename T, size_t N, class AP>
|
|
|
|
inline void Vector<T, N, AP>::swap(Vector& aOther) {
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(N == 0, "still need to implement this for N != 0");
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
// This only works when inline storage is always empty.
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!usingInlineStorage() && aOther.usingInlineStorage()) {
|
|
|
|
aOther.mBegin = mBegin;
|
2013-07-10 03:33:29 +04:00
|
|
|
mBegin = inlineStorage();
|
2014-07-11 06:10:17 +04:00
|
|
|
} else if (usingInlineStorage() && !aOther.usingInlineStorage()) {
|
|
|
|
mBegin = aOther.mBegin;
|
|
|
|
aOther.mBegin = aOther.inlineStorage();
|
|
|
|
} else if (!usingInlineStorage() && !aOther.usingInlineStorage()) {
|
2020-01-20 19:17:06 +03:00
|
|
|
std::swap(mBegin, aOther.mBegin);
|
2013-07-10 03:33:29 +04:00
|
|
|
} else {
|
|
|
|
// This case is a no-op, since we'd set both to use their inline storage.
|
|
|
|
}
|
|
|
|
|
2020-01-20 19:17:06 +03:00
|
|
|
std::swap(mLength, aOther.mLength);
|
|
|
|
std::swap(mTail.mCapacity, aOther.mTail.mCapacity);
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2020-01-20 19:17:06 +03:00
|
|
|
std::swap(mTail.mReserved, aOther.mTail.mReserved);
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_Vector_h */
|