| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#ifndef mozilla_Buffer_h |
| 6 |
#define mozilla_Buffer_h |
| 7 |
|
| 8 |
#include <cstddef> |
| 9 |
#include <iterator> |
| 10 |
|
| 11 |
#include "mozilla/Assertions.h" |
| 12 |
#include "mozilla/Maybe.h" |
| 13 |
#include "mozilla/Span.h" |
| 14 |
#include "mozilla/UniquePtr.h" |
| 15 |
#include "mozilla/UniquePtrExtensions.h" |
| 16 |
|
| 17 |
namespace mozilla { |
| 18 |
|
| 19 |
/** |
| 20 |
* A move-only type that wraps a mozilla::UniquePtr<T[]> and the length of |
| 21 |
* the T[]. |
| 22 |
* |
| 23 |
* Unlike mozilla::Array, the length is a run-time property. |
| 24 |
* Unlike mozilla::Vector and nsTArray, does not have capacity and |
| 25 |
* assocatiated growth functionality. |
| 26 |
* Unlike mozilla::Span, mozilla::Buffer owns the allocation it points to. |
| 27 |
*/ |
| 28 |
template <typename T> |
| 29 |
class Buffer final { |
| 30 |
private: |
| 31 |
mozilla::UniquePtr<T[]> mData; |
| 32 |
size_t mLength; |
| 33 |
|
| 34 |
public: |
| 35 |
Buffer(const Buffer<T>& aOther) = delete; |
| 36 |
Buffer<T>& operator=(const Buffer<T>& aOther) = delete; |
| 37 |
|
| 38 |
/** |
| 39 |
* Construct zero-lenth Buffer (without actually pointing to a heap |
| 40 |
* allocation). |
| 41 |
*/ |
| 42 |
Buffer() : mData(nullptr), mLength(0) {}; |
| 43 |
|
| 44 |
/** |
| 45 |
* Construct from raw parts. |
| 46 |
* |
| 47 |
* aLength must not be greater than the actual length of the buffer pointed |
| 48 |
* to by aData. |
| 49 |
*/ |
| 50 |
Buffer(mozilla::UniquePtr<T[]>&& aData, size_t aLength) |
| 51 |
: mData(std::move(aData)), mLength(aLength) {} |
| 52 |
|
| 53 |
/** |
| 54 |
* Move constructor. Sets the moved-from Buffer to zero-length |
| 55 |
* state. |
| 56 |
*/ |
| 57 |
Buffer(Buffer<T>&& aOther) |
| 58 |
: mData(std::move(aOther.mData)), mLength(aOther.mLength) { |
| 59 |
aOther.mLength = 0; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Move assignment. Sets the moved-from Buffer to zero-length |
| 64 |
* state. |
| 65 |
*/ |
| 66 |
Buffer<T>& operator=(Buffer<T>&& aOther) { |
| 67 |
mData = std::move(aOther.mData); |
| 68 |
mLength = aOther.mLength; |
| 69 |
aOther.mLength = 0; |
| 70 |
return *this; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Construct by copying the elements of a Span. |
| 75 |
* |
| 76 |
* Allocates the internal buffer infallibly. Use CopyFrom for fallible |
| 77 |
* allocation. |
| 78 |
*/ |
| 79 |
explicit Buffer(mozilla::Span<const T> aSpan) |
| 80 |
: mData(mozilla::MakeUniqueForOverwrite<T[]>(aSpan.Length())), |
| 81 |
mLength(aSpan.Length()) { |
| 82 |
std::copy(aSpan.cbegin(), aSpan.cend(), mData.get()); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Create a new Buffer by copying the elements of a Span. |
| 87 |
* |
| 88 |
* Allocates the internal buffer fallibly. |
| 89 |
*/ |
| 90 |
static mozilla::Maybe<Buffer<T>> CopyFrom(mozilla::Span<const T> aSpan) { |
| 91 |
if (aSpan.IsEmpty()) { |
| 92 |
return Some(Buffer()); |
| 93 |
} |
| 94 |
|
| 95 |
auto data = mozilla::MakeUniqueForOverwriteFallible<T[]>(aSpan.Length()); |
| 96 |
if (!data) { |
| 97 |
return mozilla::Nothing(); |
| 98 |
} |
| 99 |
std::copy(aSpan.cbegin(), aSpan.cend(), data.get()); |
| 100 |
return mozilla::Some(Buffer(std::move(data), aSpan.Length())); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Construct a buffer of requested length. |
| 105 |
* |
| 106 |
* The contents will be initialized or uninitialized according |
| 107 |
* to the behavior of mozilla::MakeUnique<T[]>(aLength) for T. |
| 108 |
* |
| 109 |
* Allocates the internal buffer infallibly. Use Alloc for fallible |
| 110 |
* allocation. |
| 111 |
*/ |
| 112 |
explicit Buffer(size_t aLength) |
| 113 |
: mData(mozilla::MakeUnique<T[]>(aLength)), mLength(aLength) {} |
| 114 |
|
| 115 |
/** |
| 116 |
* Create a new Buffer with an internal buffer of requested length. |
| 117 |
* |
| 118 |
* The contents will be initialized or uninitialized according to the |
| 119 |
* behavior of mozilla::MakeUnique<T[]>(aLength) for T. |
| 120 |
* |
| 121 |
* Allocates the internal buffer fallibly. |
| 122 |
*/ |
| 123 |
static mozilla::Maybe<Buffer<T>> Alloc(size_t aLength) { |
| 124 |
auto data = mozilla::MakeUniqueFallible<T[]>(aLength); |
| 125 |
if (!data) { |
| 126 |
return mozilla::Nothing(); |
| 127 |
} |
| 128 |
return mozilla::Some(Buffer(std::move(data), aLength)); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Create a new Buffer with an internal buffer of requested length. |
| 133 |
* |
| 134 |
* This uses MakeUniqueFallibleForOverwrite so the contents will be |
| 135 |
* default-initialized. |
| 136 |
* |
| 137 |
* Allocates the internal buffer fallibly. |
| 138 |
*/ |
| 139 |
static Maybe<Buffer<T>> AllocForOverwrite(size_t aLength) { |
| 140 |
auto data = MakeUniqueForOverwriteFallible<T[]>(aLength); |
| 141 |
if (!data) { |
| 142 |
return Nothing(); |
| 143 |
} |
| 144 |
return Some(Buffer(std::move(data), aLength)); |
| 145 |
} |
| 146 |
|
| 147 |
auto AsSpan() const { return mozilla::Span<const T>{mData.get(), mLength}; } |
| 148 |
auto AsWritableSpan() { return mozilla::Span<T>{mData.get(), mLength}; } |
| 149 |
operator mozilla::Span<const T>() const { return AsSpan(); } |
| 150 |
operator mozilla::Span<T>() { return AsWritableSpan(); } |
| 151 |
|
| 152 |
/** |
| 153 |
* Guarantees a non-null and aligned pointer |
| 154 |
* even for the zero-length case. |
| 155 |
*/ |
| 156 |
T* Elements() { return AsWritableSpan().Elements(); } |
| 157 |
size_t Length() const { return mLength; } |
| 158 |
|
| 159 |
T& operator[](size_t aIndex) { |
| 160 |
MOZ_ASSERT(aIndex < mLength); |
| 161 |
return mData.get()[aIndex]; |
| 162 |
} |
| 163 |
|
| 164 |
const T& operator[](size_t aIndex) const { |
| 165 |
MOZ_ASSERT(aIndex < mLength); |
| 166 |
return mData.get()[aIndex]; |
| 167 |
} |
| 168 |
|
| 169 |
typedef T* iterator; |
| 170 |
typedef const T* const_iterator; |
| 171 |
typedef std::reverse_iterator<T*> reverse_iterator; |
| 172 |
typedef std::reverse_iterator<const T*> const_reverse_iterator; |
| 173 |
|
| 174 |
// Methods for range-based for loops. |
| 175 |
iterator begin() { return mData.get(); } |
| 176 |
const_iterator begin() const { return mData.get(); } |
| 177 |
const_iterator cbegin() const { return begin(); } |
| 178 |
iterator end() { return mData.get() + mLength; } |
| 179 |
const_iterator end() const { return mData.get() + mLength; } |
| 180 |
const_iterator cend() const { return end(); } |
| 181 |
|
| 182 |
// Methods for reverse iterating. |
| 183 |
reverse_iterator rbegin() { return reverse_iterator(end()); } |
| 184 |
const_reverse_iterator rbegin() const { |
| 185 |
return const_reverse_iterator(end()); |
| 186 |
} |
| 187 |
const_reverse_iterator crbegin() const { return rbegin(); } |
| 188 |
reverse_iterator rend() { return reverse_iterator(begin()); } |
| 189 |
const_reverse_iterator rend() const { |
| 190 |
return const_reverse_iterator(begin()); |
| 191 |
} |
| 192 |
const_reverse_iterator crend() const { return rend(); } |
| 193 |
}; |
| 194 |
|
| 195 |
} /* namespace mozilla */ |
| 196 |
|
| 197 |
#endif /* mozilla_Buffer_h */ |