Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
7 #ifndef jit_FixedList_h
8 #define jit_FixedList_h
10 #include <stddef.h>
12 #include "jit/Ion.h"
13 #include "jit/IonAllocPolicy.h"
15 namespace js {
16 namespace jit {
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_;
25 private:
26 FixedList(const FixedList&); // no copy definition.
27 void operator= (const FixedList*); // no assignment definition.
29 public:
30 FixedList()
31 : list_(nullptr), length_(0)
32 { }
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;
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 }
46 size_t length() const {
47 return length_;
48 }
50 void shrink(size_t num) {
51 JS_ASSERT(num < length_);
52 length_ -= num;
53 }
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;
65 for (size_t i = 0; i < length_; i++)
66 list[i] = list_[i];
68 length_ += num;
69 list_ = list;
70 return true;
71 }
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 };
83 } // namespace jit
84 } // namespace js
86 #endif /* jit_FixedList_h */