michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef jit_FixedList_h michael@0: #define jit_FixedList_h michael@0: michael@0: #include michael@0: michael@0: #include "jit/Ion.h" michael@0: #include "jit/IonAllocPolicy.h" michael@0: michael@0: namespace js { michael@0: namespace jit { michael@0: michael@0: // List of a fixed length, but the length is unknown until runtime. michael@0: template michael@0: class FixedList michael@0: { michael@0: T *list_; michael@0: size_t length_; michael@0: michael@0: private: michael@0: FixedList(const FixedList&); // no copy definition. michael@0: void operator= (const FixedList*); // no assignment definition. michael@0: michael@0: public: michael@0: FixedList() michael@0: : list_(nullptr), length_(0) michael@0: { } michael@0: michael@0: // Dynamic memory allocation requires the ability to report failure. michael@0: bool init(TempAllocator &alloc, size_t length) { michael@0: length_ = length; michael@0: if (length == 0) michael@0: return true; michael@0: michael@0: if (length & mozilla::tl::MulOverflowMask::value) michael@0: return false; michael@0: list_ = (T *)alloc.allocate(length * sizeof(T)); michael@0: return list_ != nullptr; michael@0: } michael@0: michael@0: size_t length() const { michael@0: return length_; michael@0: } michael@0: michael@0: void shrink(size_t num) { michael@0: JS_ASSERT(num < length_); michael@0: length_ -= num; michael@0: } michael@0: michael@0: bool growBy(TempAllocator &alloc, size_t num) { michael@0: size_t newlength = length_ + num; michael@0: if (newlength < length_) michael@0: return false; michael@0: if (newlength & mozilla::tl::MulOverflowMask::value) michael@0: return false; michael@0: T *list = (T *)alloc.allocate((length_ + num) * sizeof(T)); michael@0: if (!list) michael@0: return false; michael@0: michael@0: for (size_t i = 0; i < length_; i++) michael@0: list[i] = list_[i]; michael@0: michael@0: length_ += num; michael@0: list_ = list; michael@0: return true; michael@0: } michael@0: michael@0: T &operator[](size_t index) { michael@0: JS_ASSERT(index < length_); michael@0: return list_[index]; michael@0: } michael@0: const T &operator [](size_t index) const { michael@0: JS_ASSERT(index < length_); michael@0: return list_[index]; michael@0: } michael@0: }; michael@0: michael@0: } // namespace jit michael@0: } // namespace js michael@0: michael@0: #endif /* jit_FixedList_h */