blob: fab8fb08aae39271882a156a11156794af85820b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef ds_FixedLengthVector_h
#define ds_FixedLengthVector_h
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "mozilla/Attributes.h" // MOZ_MUST_USE
#include "mozilla/OperatorNewExtensions.h" // mozilla::KnownNotNull
#include <stddef.h> // size_t
#include "js/Utility.h" // js_free
#include "vm/JSContext.h" // JSContext
namespace js {
// A dynamically-allocated fixed-length vector with bounds checking assertions.
template <typename T>
class FixedLengthVector {
// The pointer to the storage.
T* data_ = nullptr;
// The size of the storage.
size_t length_ = 0;
public:
FixedLengthVector() = default;
FixedLengthVector(FixedLengthVector&) = delete;
FixedLengthVector(FixedLengthVector&&) = default;
~FixedLengthVector() {
if (initialized()) {
js_free(data_);
}
}
size_t length() const { return length_; }
bool initialized() const { return !!data_; }
// Allocate the storage with the given size, wihtout calling constructor.
//
// If the allocation fails, this returns false and sets the
// pending exception on the given context.
MOZ_MUST_USE bool allocateUninitialized(JSContext* cx, size_t length) {
MOZ_ASSERT(!initialized());
length_ = length;
data_ = cx->pod_malloc<T>(length);
if (MOZ_UNLIKELY(!data_)) {
return false;
}
return true;
}
// Allocate the storage with the given size and call default constructor.
//
// If the allocation fails, this returns false and sets the
// pending exception on the given context.
MOZ_MUST_USE bool allocate(JSContext* cx, size_t length) {
if (!allocateUninitialized(cx, length)) {
return false;
}
for (size_t i = 0; i < length; i++) {
new (mozilla::KnownNotNull, &data_[i]) T();
}
return true;
}
T* begin() {
MOZ_ASSERT(initialized());
return data_;
}
const T* begin() const {
MOZ_ASSERT(initialized());
return data_;
}
T* end() {
MOZ_ASSERT(initialized());
return data_ + length_;
}
const T* end() const {
MOZ_ASSERT(initialized());
return data_ + length_;
}
T& operator[](size_t index) {
MOZ_ASSERT(initialized());
MOZ_ASSERT(index < length_);
return begin()[index];
}
const T& operator[](size_t index) const {
MOZ_ASSERT(initialized());
MOZ_ASSERT(index < length_);
return begin()[index];
}
};
} // namespace js
#endif // ds_FixedLengthVector_h
|