|
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/IonAllocPolicy.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 FixedList() |
|
31 : list_(nullptr), length_(0) |
|
32 { } |
|
33 |
|
34 // Dynamic memory allocation requires the ability to report failure. |
|
35 bool init(TempAllocator &alloc, size_t length) { |
|
36 length_ = length; |
|
37 if (length == 0) |
|
38 return true; |
|
39 |
|
40 if (length & mozilla::tl::MulOverflowMask<sizeof(T)>::value) |
|
41 return false; |
|
42 list_ = (T *)alloc.allocate(length * sizeof(T)); |
|
43 return list_ != nullptr; |
|
44 } |
|
45 |
|
46 size_t length() const { |
|
47 return length_; |
|
48 } |
|
49 |
|
50 void shrink(size_t num) { |
|
51 JS_ASSERT(num < length_); |
|
52 length_ -= num; |
|
53 } |
|
54 |
|
55 bool growBy(TempAllocator &alloc, size_t num) { |
|
56 size_t newlength = length_ + num; |
|
57 if (newlength < length_) |
|
58 return false; |
|
59 if (newlength & mozilla::tl::MulOverflowMask<sizeof(T)>::value) |
|
60 return false; |
|
61 T *list = (T *)alloc.allocate((length_ + num) * sizeof(T)); |
|
62 if (!list) |
|
63 return false; |
|
64 |
|
65 for (size_t i = 0; i < length_; i++) |
|
66 list[i] = list_[i]; |
|
67 |
|
68 length_ += num; |
|
69 list_ = list; |
|
70 return true; |
|
71 } |
|
72 |
|
73 T &operator[](size_t index) { |
|
74 JS_ASSERT(index < length_); |
|
75 return list_[index]; |
|
76 } |
|
77 const T &operator [](size_t index) const { |
|
78 JS_ASSERT(index < length_); |
|
79 return list_[index]; |
|
80 } |
|
81 }; |
|
82 |
|
83 } // namespace jit |
|
84 } // namespace js |
|
85 |
|
86 #endif /* jit_FixedList_h */ |