1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit/FixedList.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,86 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef jit_FixedList_h 1.11 +#define jit_FixedList_h 1.12 + 1.13 +#include <stddef.h> 1.14 + 1.15 +#include "jit/Ion.h" 1.16 +#include "jit/IonAllocPolicy.h" 1.17 + 1.18 +namespace js { 1.19 +namespace jit { 1.20 + 1.21 +// List of a fixed length, but the length is unknown until runtime. 1.22 +template <typename T> 1.23 +class FixedList 1.24 +{ 1.25 + T *list_; 1.26 + size_t length_; 1.27 + 1.28 + private: 1.29 + FixedList(const FixedList&); // no copy definition. 1.30 + void operator= (const FixedList*); // no assignment definition. 1.31 + 1.32 + public: 1.33 + FixedList() 1.34 + : list_(nullptr), length_(0) 1.35 + { } 1.36 + 1.37 + // Dynamic memory allocation requires the ability to report failure. 1.38 + bool init(TempAllocator &alloc, size_t length) { 1.39 + length_ = length; 1.40 + if (length == 0) 1.41 + return true; 1.42 + 1.43 + if (length & mozilla::tl::MulOverflowMask<sizeof(T)>::value) 1.44 + return false; 1.45 + list_ = (T *)alloc.allocate(length * sizeof(T)); 1.46 + return list_ != nullptr; 1.47 + } 1.48 + 1.49 + size_t length() const { 1.50 + return length_; 1.51 + } 1.52 + 1.53 + void shrink(size_t num) { 1.54 + JS_ASSERT(num < length_); 1.55 + length_ -= num; 1.56 + } 1.57 + 1.58 + bool growBy(TempAllocator &alloc, size_t num) { 1.59 + size_t newlength = length_ + num; 1.60 + if (newlength < length_) 1.61 + return false; 1.62 + if (newlength & mozilla::tl::MulOverflowMask<sizeof(T)>::value) 1.63 + return false; 1.64 + T *list = (T *)alloc.allocate((length_ + num) * sizeof(T)); 1.65 + if (!list) 1.66 + return false; 1.67 + 1.68 + for (size_t i = 0; i < length_; i++) 1.69 + list[i] = list_[i]; 1.70 + 1.71 + length_ += num; 1.72 + list_ = list; 1.73 + return true; 1.74 + } 1.75 + 1.76 + T &operator[](size_t index) { 1.77 + JS_ASSERT(index < length_); 1.78 + return list_[index]; 1.79 + } 1.80 + const T &operator [](size_t index) const { 1.81 + JS_ASSERT(index < length_); 1.82 + return list_[index]; 1.83 + } 1.84 +}; 1.85 + 1.86 +} // namespace jit 1.87 +} // namespace js 1.88 + 1.89 +#endif /* jit_FixedList_h */