Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef jit_FixedList_h
8 : #define jit_FixedList_h
9 :
10 : #include <stddef.h>
11 :
12 : #include "jit/Ion.h"
13 : #include "jit/JitAllocPolicy.h"
14 :
15 : namespace js {
16 : namespace jit {
17 :
18 : // List of a fixed length, but the length is unknown until runtime.
19 : template <typename T>
20 : class FixedList
21 : {
22 : T* list_;
23 : size_t length_;
24 :
25 : private:
26 : FixedList(const FixedList&); // no copy definition.
27 : void operator= (const FixedList*); // no assignment definition.
28 :
29 : public:
30 15886 : FixedList()
31 15886 : : list_(nullptr), length_(0)
32 15886 : { }
33 :
34 : // Dynamic memory allocation requires the ability to report failure.
35 15930 : MOZ_MUST_USE bool init(TempAllocator& alloc, size_t length) {
36 15930 : if (length == 0)
37 341 : return true;
38 :
39 : size_t bytes;
40 15589 : if (MOZ_UNLIKELY(!CalculateAllocSize<T>(length, &bytes)))
41 0 : return false;
42 15589 : list_ = (T*)alloc.allocate(bytes);
43 15589 : if (!list_)
44 0 : return false;
45 :
46 15589 : length_ = length;
47 15589 : return true;
48 : }
49 :
50 0 : size_t empty() const {
51 0 : return length_ == 0;
52 : }
53 :
54 679801 : size_t length() const {
55 679801 : return length_;
56 : }
57 :
58 : void shrink(size_t num) {
59 : MOZ_ASSERT(num < length_);
60 : length_ -= num;
61 : }
62 :
63 0 : MOZ_MUST_USE bool growBy(TempAllocator& alloc, size_t num) {
64 0 : size_t newlength = length_ + num;
65 0 : if (newlength < length_)
66 0 : return false;
67 : size_t bytes;
68 0 : if (MOZ_UNLIKELY(!CalculateAllocSize<T>(newlength, &bytes)))
69 0 : return false;
70 0 : T* list = (T*)alloc.allocate(bytes);
71 0 : if (MOZ_UNLIKELY(!list))
72 0 : return false;
73 :
74 0 : for (size_t i = 0; i < length_; i++)
75 0 : list[i] = list_[i];
76 :
77 0 : length_ += num;
78 0 : list_ = list;
79 0 : return true;
80 : }
81 :
82 1669178 : T& operator[](size_t index) {
83 1669178 : MOZ_ASSERT(index < length_);
84 1669178 : return list_[index];
85 : }
86 2744395 : const T& operator [](size_t index) const {
87 2744395 : MOZ_ASSERT(index < length_);
88 2744395 : return list_[index];
89 : }
90 :
91 0 : T* data() {
92 0 : return list_;
93 : }
94 :
95 7334 : T* begin() {
96 7334 : return list_;
97 : }
98 : T* end() {
99 : return list_ + length_;
100 : }
101 : };
102 :
103 : } // namespace jit
104 : } // namespace js
105 :
106 : #endif /* jit_FixedList_h */
|