Skip to content

Commit 05503dc

Browse files
committed
Bug 1983704 - Stop passing alignment around to nsTArray_base. r=nika
After the previous patch it's unused. Conceptually it's the right thing to do if nsTArray wanted to ever deal with over-aligned types. But that doesn't seem to be the case for now, so maybe worth taking the simplification? Differential Revision: https://phabricator.services.mozilla.com/D257932
1 parent 4f16a87 commit 05503dc

File tree

2 files changed

+50
-68
lines changed

2 files changed

+50
-68
lines changed

xpcom/ds/nsTArray-inl.h

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ nsTArray_base<Alloc, RelocationStrategy>::EnsureCapacityImpl(
161161
// shrink the capacity will leave the array unchanged.
162162
template <class Alloc, class RelocationStrategy>
163163
void nsTArray_base<Alloc, RelocationStrategy>::ShrinkCapacity(
164-
size_type aElemSize, size_t aElemAlign) {
164+
size_type aElemSize) {
165165
if (HasEmptyHeader() || UsesAutoArrayBuffer()) {
166166
return;
167167
}
@@ -211,7 +211,7 @@ void nsTArray_base<Alloc, RelocationStrategy>::ShrinkCapacity(
211211

212212
template <class Alloc, class RelocationStrategy>
213213
void nsTArray_base<Alloc, RelocationStrategy>::ShrinkCapacityToZero(
214-
size_type aElemSize, size_t aElemAlign) {
214+
size_type aElemSize) {
215215
MOZ_ASSERT(mHdr->mLength == 0);
216216

217217
if (HasEmptyHeader() || UsesAutoArrayBuffer()) {
@@ -227,8 +227,7 @@ template <typename ActualAlloc>
227227
void nsTArray_base<Alloc, RelocationStrategy>::ShiftData(index_type aStart,
228228
size_type aOldLen,
229229
size_type aNewLen,
230-
size_type aElemSize,
231-
size_t aElemAlign) {
230+
size_type aElemSize) {
232231
if (aOldLen == aNewLen) {
233232
return;
234233
}
@@ -239,7 +238,7 @@ void nsTArray_base<Alloc, RelocationStrategy>::ShiftData(index_type aStart,
239238
// Compute the resulting length of the array
240239
mHdr->mLength += aNewLen - aOldLen;
241240
if (mHdr->mLength == 0) {
242-
ShrinkCapacityToZero(aElemSize, aElemAlign);
241+
ShrinkCapacityToZero(aElemSize);
243242
} else {
244243
// Maybe nothing needs to be shifted
245244
if (num == 0) {
@@ -257,10 +256,8 @@ void nsTArray_base<Alloc, RelocationStrategy>::ShiftData(index_type aStart,
257256

258257
template <class Alloc, class RelocationStrategy>
259258
template <typename ActualAlloc>
260-
void nsTArray_base<Alloc, RelocationStrategy>::SwapFromEnd(index_type aStart,
261-
size_type aCount,
262-
size_type aElemSize,
263-
size_t aElemAlign) {
259+
void nsTArray_base<Alloc, RelocationStrategy>::SwapFromEnd(
260+
index_type aStart, size_type aCount, size_type aElemSize) {
264261
// This method is part of the implementation of
265262
// nsTArray::SwapRemoveElement{s,}At. For more information, read the
266263
// documentation on that method.
@@ -275,7 +272,7 @@ void nsTArray_base<Alloc, RelocationStrategy>::SwapFromEnd(index_type aStart,
275272

276273
if (mHdr->mLength == 0) {
277274
// If we have no elements remaining in the array, we can free our buffer.
278-
ShrinkCapacityToZero(aElemSize, aElemAlign);
275+
ShrinkCapacityToZero(aElemSize);
279276
return;
280277
}
281278

@@ -311,8 +308,7 @@ template <typename ActualAlloc>
311308
typename ActualAlloc::ResultTypeProxy
312309
nsTArray_base<Alloc, RelocationStrategy>::InsertSlotsAt(index_type aIndex,
313310
size_type aCount,
314-
size_type aElemSize,
315-
size_t aElemAlign) {
311+
size_type aElemSize) {
316312
if (MOZ_UNLIKELY(aIndex > Length())) {
317313
mozilla::detail::InvalidArrayIndex_CRASH(aIndex, Length());
318314
}
@@ -324,7 +320,7 @@ nsTArray_base<Alloc, RelocationStrategy>::InsertSlotsAt(index_type aIndex,
324320

325321
// Move the existing elements as needed. Note that this will
326322
// change our mLength, so no need to call IncrementLength.
327-
ShiftData<ActualAlloc>(aIndex, 0, aCount, aElemSize, aElemAlign);
323+
ShiftData<ActualAlloc>(aIndex, 0, aCount, aElemSize);
328324

329325
return ActualAlloc::SuccessResult();
330326
}
@@ -333,8 +329,7 @@ template <class Alloc, class RelocationStrategy>
333329
template <typename ActualAlloc, class Allocator>
334330
typename ActualAlloc::ResultTypeProxy
335331
nsTArray_base<Alloc, RelocationStrategy>::SwapArrayElements(
336-
nsTArray_base<Allocator, RelocationStrategy>& aOther, size_type aElemSize,
337-
size_t aElemAlign) {
332+
nsTArray_base<Allocator, RelocationStrategy>& aOther, size_type aElemSize) {
338333
// If neither array uses an auto buffer which is big enough to store the
339334
// other array's elements, then ensure that both arrays use malloc'ed storage
340335
// and swap their mHdr pointers.
@@ -438,8 +433,7 @@ nsTArray_base<Alloc, RelocationStrategy>::SwapArrayElements(
438433
template <class Alloc, class RelocationStrategy>
439434
template <class Allocator>
440435
void nsTArray_base<Alloc, RelocationStrategy>::MoveInit(
441-
nsTArray_base<Allocator, RelocationStrategy>& aOther, size_type aElemSize,
442-
size_t aElemAlign) {
436+
nsTArray_base<Allocator, RelocationStrategy>& aOther, size_type aElemSize) {
443437
// This method is similar to SwapArrayElements, but specialized for the case
444438
// where the target array is empty with no allocated heap storage. It is
445439
// provided and used to simplify template instantiation and enable better code
@@ -491,8 +485,7 @@ void nsTArray_base<Alloc, RelocationStrategy>::MoveInit(
491485
template <class Alloc, class RelocationStrategy>
492486
template <class Allocator>
493487
void nsTArray_base<Alloc, RelocationStrategy>::MoveConstructNonAutoArray(
494-
nsTArray_base<Allocator, RelocationStrategy>& aOther, size_type aElemSize,
495-
size_t aElemAlign) {
488+
nsTArray_base<Allocator, RelocationStrategy>& aOther, size_type aElemSize) {
496489
// We know that we are not an (Copyable)AutoTArray and we know that we are
497490
// empty, so don't use SwapArrayElements which doesn't know either of these
498491
// facts and is very complex. Use nsTArrayInfallibleAllocator regardless of

xpcom/ds/nsTArray.h

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,12 @@ class nsTArray_base {
492492
// Tries to resize the storage to the minimum required amount. If this fails,
493493
// the array is left as-is.
494494
// @param aElemSize The size of an array element.
495-
// @param aElemAlign The alignment in bytes of an array element.
496-
void ShrinkCapacity(size_type aElemSize, size_t aElemAlign);
495+
void ShrinkCapacity(size_type aElemSize);
497496

498497
// Resizes the storage to 0. This may only be called when Length() is already
499498
// 0.
500499
// @param aElemSize The size of an array element.
501-
// @param aElemAlign The alignment in bytes of an array element.
502-
void ShrinkCapacityToZero(size_type aElemSize, size_t aElemAlign);
500+
void ShrinkCapacityToZero(size_type aElemSize);
503501

504502
// This method may be called to resize a "gap" in the array by shifting
505503
// elements around. It updates mLength appropriately. If the resulting
@@ -508,21 +506,18 @@ class nsTArray_base {
508506
// @param aOldLen The current length of the gap.
509507
// @param aNewLen The desired length of the gap.
510508
// @param aElemSize The size of an array element.
511-
// @param aElemAlign The alignment in bytes of an array element.
512509
template <typename ActualAlloc>
513510
void ShiftData(index_type aStart, size_type aOldLen, size_type aNewLen,
514-
size_type aElemSize, size_t aElemAlign);
511+
size_type aElemSize);
515512

516513
// This method may be called to swap elements from the end of the array to
517514
// fill a "gap" in the array. If the resulting array has zero elements, then
518515
// the array's memory is free'd.
519516
// @param aStart The starting index of the gap.
520517
// @param aCount The length of the gap.
521518
// @param aElemSize The size of an array element.
522-
// @param aElemAlign The alignment in bytes of an array element.
523519
template <typename ActualAlloc>
524-
void SwapFromEnd(index_type aStart, size_type aCount, size_type aElemSize,
525-
size_t aElemAlign);
520+
void SwapFromEnd(index_type aStart, size_type aCount, size_type aElemSize);
526521

527522
// This method increments the length member of the array's header.
528523
// Note that mHdr may actually be sEmptyTArrayHeader in the case where a
@@ -544,26 +539,24 @@ class nsTArray_base {
544539
// greater than the current length of the array.
545540
// @param aCount the number of slots to insert
546541
// @param aElementSize the size of an array element.
547-
// @param aElemAlign the alignment in bytes of an array element.
548542
template <typename ActualAlloc>
549543
typename ActualAlloc::ResultTypeProxy InsertSlotsAt(index_type aIndex,
550544
size_type aCount,
551-
size_type aElementSize,
552-
size_t aElemAlign);
545+
size_type aElementSize);
553546

554547
template <typename ActualAlloc, class Allocator>
555548
typename ActualAlloc::ResultTypeProxy SwapArrayElements(
556-
nsTArray_base<Allocator, RelocationStrategy>& aOther, size_type aElemSize,
557-
size_t aElemAlign);
549+
nsTArray_base<Allocator, RelocationStrategy>& aOther,
550+
size_type aElemSize);
558551

559552
template <class Allocator>
560553
void MoveConstructNonAutoArray(
561-
nsTArray_base<Allocator, RelocationStrategy>& aOther, size_type aElemSize,
562-
size_t aElemAlign);
554+
nsTArray_base<Allocator, RelocationStrategy>& aOther,
555+
size_type aElemSize);
563556

564557
template <class Allocator>
565558
void MoveInit(nsTArray_base<Allocator, RelocationStrategy>& aOther,
566-
size_type aElemSize, size_t aElemAlign);
559+
size_type aElemSize);
567560

568561
// Helper function for move construction and SwapArrayElements.
569562
// Takes the storage from the nsTArray as a non-auto Header pointer.
@@ -1036,8 +1029,7 @@ class nsTArray_Impl
10361029
MOZ_ASSERT(!this->UsesAutoArrayBuffer());
10371030

10381031
// This does not use SwapArrayElements because that's unnecessarily complex.
1039-
this->MoveConstructNonAutoArray(aOther, sizeof(value_type),
1040-
alignof(value_type));
1032+
this->MoveConstructNonAutoArray(aOther, sizeof(value_type));
10411033
}
10421034

10431035
// The array's copy-constructor performs a 'deep' copy of the given array.
@@ -1082,7 +1074,7 @@ class nsTArray_Impl
10821074
self_type& operator=(self_type&& aOther) {
10831075
if (this != &aOther) {
10841076
Clear();
1085-
this->MoveInit(aOther, sizeof(value_type), alignof(value_type));
1077+
this->MoveInit(aOther, sizeof(value_type));
10861078
}
10871079
return *this;
10881080
}
@@ -1128,7 +1120,7 @@ class nsTArray_Impl
11281120
template <typename Allocator>
11291121
self_type& operator=(nsTArray_Impl<E, Allocator>&& aOther) {
11301122
Clear();
1131-
this->MoveInit(aOther, sizeof(value_type), alignof(value_type));
1123+
this->MoveInit(aOther, sizeof(value_type));
11321124
return *this;
11331125
}
11341126

@@ -1447,7 +1439,7 @@ class nsTArray_Impl
14471439
template <class Allocator>
14481440
void Assign(nsTArray_Impl<E, Allocator>&& aOther) {
14491441
Clear();
1450-
this->MoveInit(aOther, sizeof(value_type), alignof(value_type));
1442+
this->MoveInit(aOther, sizeof(value_type));
14511443
}
14521444

14531445
// This method call the destructor on each element of the array, empties it,
@@ -1922,7 +1914,7 @@ class nsTArray_Impl
19221914

19231915
void Clear() {
19241916
ClearAndRetainStorage();
1925-
base_type::ShrinkCapacityToZero(sizeof(value_type), alignof(value_type));
1917+
base_type::ShrinkCapacityToZero(sizeof(value_type));
19261918
}
19271919

19281920
// This method removes elements based on the return value of the
@@ -1987,8 +1979,8 @@ class nsTArray_Impl
19871979
// The only case this might fail were if someone called this with a
19881980
// AutoTArray upcast to nsTArray_Impl, under the conditions mentioned in the
19891981
// overload for AutoTArray below.
1990-
this->template SwapArrayElements<InfallibleAlloc>(
1991-
aOther, sizeof(value_type), alignof(value_type));
1982+
this->template SwapArrayElements<InfallibleAlloc>(aOther,
1983+
sizeof(value_type));
19921984
}
19931985

19941986
template <size_t N>
@@ -1998,8 +1990,8 @@ class nsTArray_Impl
19981990
// small inline sizes, and crash in the rare case of a small OOM error.
19991991
static_assert(!std::is_same_v<Alloc, FallibleAlloc> ||
20001992
sizeof(E) * N <= 1024);
2001-
this->template SwapArrayElements<InfallibleAlloc>(
2002-
aOther, sizeof(value_type), alignof(value_type));
1993+
this->template SwapArrayElements<InfallibleAlloc>(aOther,
1994+
sizeof(value_type));
20031995
}
20041996

20051997
template <class Allocator>
@@ -2008,8 +2000,8 @@ class nsTArray_Impl
20082000
// Allocation might fail if Alloc==FallibleAlloc and
20092001
// Allocator==InfallibleAlloc and aOther uses auto storage.
20102002
return FallibleAlloc::Result(
2011-
this->template SwapArrayElements<FallibleAlloc>(
2012-
aOther, sizeof(value_type), alignof(value_type)));
2003+
this->template SwapArrayElements<FallibleAlloc>(aOther,
2004+
sizeof(value_type)));
20132005
}
20142006

20152007
private:
@@ -2285,7 +2277,7 @@ class nsTArray_Impl
22852277
template <typename ActualAlloc>
22862278
value_type* InsertElementsAtInternal(index_type aIndex, size_type aCount) {
22872279
if (!ActualAlloc::Successful(this->template InsertSlotsAt<ActualAlloc>(
2288-
aIndex, aCount, sizeof(value_type), alignof(value_type)))) {
2280+
aIndex, aCount, sizeof(value_type)))) {
22892281
return nullptr;
22902282
}
22912283

@@ -2328,7 +2320,7 @@ class nsTArray_Impl
23282320
}
23292321

23302322
// This method may be called to minimize the memory used by this array.
2331-
void Compact() { ShrinkCapacity(sizeof(value_type), alignof(value_type)); }
2323+
void Compact() { ShrinkCapacity(sizeof(value_type)); }
23322324

23332325
//
23342326
// Sorting
@@ -2472,8 +2464,8 @@ auto nsTArray_Impl<E, Alloc>::ReplaceElementsAtInternal(index_type aStart,
24722464
return nullptr;
24732465
}
24742466
DestructRange(aStart, aCount);
2475-
this->template ShiftData<ActualAlloc>(
2476-
aStart, aCount, aArrayLen, sizeof(value_type), alignof(value_type));
2467+
this->template ShiftData<ActualAlloc>(aStart, aCount, aArrayLen,
2468+
sizeof(value_type));
24772469
AssignRange(aStart, aArrayLen, aArray);
24782470
return Elements() + aStart;
24792471
}
@@ -2497,8 +2489,8 @@ template <typename E, class Alloc>
24972489
void nsTArray_Impl<E, Alloc>::RemoveElementsAtUnsafe(index_type aStart,
24982490
size_type aCount) {
24992491
DestructRange(aStart, aCount);
2500-
this->template ShiftData<InfallibleAlloc>(
2501-
aStart, aCount, 0, sizeof(value_type), alignof(value_type));
2492+
this->template ShiftData<InfallibleAlloc>(aStart, aCount, 0,
2493+
sizeof(value_type));
25022494
}
25032495

25042496
template <typename E, class Alloc>
@@ -2517,8 +2509,8 @@ void nsTArray_Impl<E, Alloc>::UnorderedRemoveElementsAt(index_type aStart,
25172509
// replace them from the end. See the docs on the declaration of this
25182510
// function.
25192511
DestructRange(aStart, aCount);
2520-
this->template SwapFromEnd<InfallibleAlloc>(
2521-
aStart, aCount, sizeof(value_type), alignof(value_type));
2512+
this->template SwapFromEnd<InfallibleAlloc>(aStart, aCount,
2513+
sizeof(value_type));
25222514
}
25232515

25242516
template <typename E, class Alloc>
@@ -2561,7 +2553,7 @@ auto nsTArray_Impl<E, Alloc>::InsertElementsAtInternal(index_type aIndex,
25612553
const Item& aItem)
25622554
-> value_type* {
25632555
if (!ActualAlloc::Successful(this->template InsertSlotsAt<ActualAlloc>(
2564-
aIndex, aCount, sizeof(value_type), alignof(value_type)))) {
2556+
aIndex, aCount, sizeof(value_type)))) {
25652557
return nullptr;
25662558
}
25672559

@@ -2588,8 +2580,7 @@ auto nsTArray_Impl<E, Alloc>::InsertElementAtInternal(index_type aIndex)
25882580
Length() + 1, sizeof(value_type)))) {
25892581
return nullptr;
25902582
}
2591-
this->template ShiftData<ActualAlloc>(aIndex, 0, 1, sizeof(value_type),
2592-
alignof(value_type));
2583+
this->template ShiftData<ActualAlloc>(aIndex, 0, 1, sizeof(value_type));
25932584
value_type* elem = Elements() + aIndex;
25942585
elem_traits::Construct(elem);
25952586
return elem;
@@ -2609,8 +2600,7 @@ auto nsTArray_Impl<E, Alloc>::InsertElementAtInternal(index_type aIndex,
26092600
Length() + 1, sizeof(value_type)))) {
26102601
return nullptr;
26112602
}
2612-
this->template ShiftData<ActualAlloc>(aIndex, 0, 1, sizeof(value_type),
2613-
alignof(value_type));
2603+
this->template ShiftData<ActualAlloc>(aIndex, 0, 1, sizeof(value_type));
26142604
value_type* elem = Elements() + aIndex;
26152605
elem_traits::Construct(elem, std::forward<Item>(aItem));
26162606
return elem;
@@ -2641,8 +2631,8 @@ auto nsTArray_Impl<E, Alloc>::AppendElementsInternal(
26412631
if (Length() == 0) {
26422632
// XXX This might still be optimized. If aArray uses auto-storage but we
26432633
// won't, we might better retain our storage if it's sufficiently large.
2644-
this->ShrinkCapacityToZero(sizeof(value_type), alignof(value_type));
2645-
this->MoveInit(aArray, sizeof(value_type), alignof(value_type));
2634+
this->ShrinkCapacityToZero(sizeof(value_type));
2635+
this->MoveInit(aArray, sizeof(value_type));
26462636
return Elements();
26472637
}
26482638

@@ -2655,8 +2645,7 @@ auto nsTArray_Impl<E, Alloc>::AppendElementsInternal(
26552645
relocation_type::RelocateNonOverlappingRegion(
26562646
Elements() + len, aArray.Elements(), otherLen, sizeof(value_type));
26572647
this->IncrementLength(otherLen);
2658-
aArray.template ShiftData<ActualAlloc>(0, otherLen, 0, sizeof(value_type),
2659-
alignof(value_type));
2648+
aArray.template ShiftData<ActualAlloc>(0, otherLen, 0, sizeof(value_type));
26602649
return Elements() + len;
26612650
}
26622651

@@ -3021,18 +3010,18 @@ class MOZ_NON_MEMMOVABLE MOZ_GSL_OWNER AutoTArray : public nsTArray<E> {
30213010

30223011
AutoTArray(self_type&& aOther) : nsTArray<E>() {
30233012
Init();
3024-
this->MoveInit(aOther, sizeof(value_type), alignof(value_type));
3013+
this->MoveInit(aOther, sizeof(value_type));
30253014
}
30263015

30273016
explicit AutoTArray(base_type&& aOther) : mAlign() {
30283017
Init();
3029-
this->MoveInit(aOther, sizeof(value_type), alignof(value_type));
3018+
this->MoveInit(aOther, sizeof(value_type));
30303019
}
30313020

30323021
template <typename Allocator>
30333022
explicit AutoTArray(nsTArray_Impl<value_type, Allocator>&& aOther) {
30343023
Init();
3035-
this->MoveInit(aOther, sizeof(value_type), alignof(value_type));
3024+
this->MoveInit(aOther, sizeof(value_type));
30363025
}
30373026

30383027
MOZ_IMPLICIT AutoTArray(std::initializer_list<E> aIL) : mAlign() {

0 commit comments

Comments
 (0)