1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef jit_FixedList_h
8 #define jit_FixedList_h
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Likely.h"
15 #include "jit/JitAllocPolicy.h"
16 #include "js/Utility.h"
21 // List of a fixed length, but the length is unknown until runtime.
28 FixedList(const FixedList
&); // no copy definition.
29 void operator=(const FixedList
*); // no assignment definition.
32 FixedList() : list_(nullptr), length_(0) {}
34 // Dynamic memory allocation requires the ability to report failure.
35 [[nodiscard
]] bool init(TempAllocator
& alloc
, size_t length
) {
40 list_
= alloc
.allocateArray
<T
>(length
);
49 size_t empty() const { return length_
== 0; }
51 size_t length() const { return length_
; }
53 void shrink(size_t num
) {
54 MOZ_ASSERT(num
< length_
);
58 [[nodiscard
]] bool growBy(TempAllocator
& alloc
, size_t num
) {
59 size_t newlength
= length_
+ num
;
60 if (newlength
< length_
) {
64 if (MOZ_UNLIKELY(!CalculateAllocSize
<T
>(newlength
, &bytes
))) {
67 T
* list
= (T
*)alloc
.allocate(bytes
);
68 if (MOZ_UNLIKELY(!list
)) {
72 for (size_t i
= 0; i
< length_
; i
++) {
81 T
& operator[](size_t index
) {
82 MOZ_ASSERT(index
< length_
);
85 const T
& operator[](size_t index
) const {
86 MOZ_ASSERT(index
< length_
);
90 T
* data() { return list_
; }
92 T
* begin() { return list_
; }
93 T
* end() { return list_
+ length_
; }
99 #endif /* jit_FixedList_h */