3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #ifndef GRPC_CORE_LIB_GPRPP_INLINED_VECTOR_H
20 #define GRPC_CORE_LIB_GPRPP_INLINED_VECTOR_H
22 #include <grpc/support/port_platform.h>
27 #include "src/core/lib/gprpp/memory.h"
31 // NOTE: We eventually want to use absl::InlinedVector here. However,
32 // there are currently build problems that prevent us from using absl.
33 // In the interim, we define a custom implementation as a place-holder,
34 // with the intent to eventually replace this with the absl
37 // This place-holder implementation does not implement the full set of
38 // functionality from the absl version; it has just the methods that we
39 // currently happen to need in gRPC. If additional functionality is
40 // needed before this gets replaced with the absl version, it can be
41 // added, with the following proviso:
43 // ANY METHOD ADDED HERE MUST COMPLY WITH THE INTERFACE IN THE absl
46 // TODO(nnoble, roth): Replace this with absl::InlinedVector once we
47 // integrate absl into the gRPC build system in a usable way.
48 template <typename T, size_t N>
51 InlinedVector() { init_data(); }
52 ~InlinedVector() { destroy_elements(); }
55 InlinedVector(const InlinedVector& v) {
60 InlinedVector& operator=(const InlinedVector& v) {
69 InlinedVector(InlinedVector&& v) {
74 InlinedVector& operator=(InlinedVector&& v) {
83 return dynamic_ != nullptr ? dynamic_ : reinterpret_cast<T*>(inline_);
86 const T* data() const {
87 return dynamic_ != nullptr ? dynamic_ : reinterpret_cast<const T*>(inline_);
90 T& operator[](size_t offset) {
91 assert(offset < size_);
92 return data()[offset];
95 const T& operator[](size_t offset) const {
96 assert(offset < size_);
97 return data()[offset];
100 void reserve(size_t capacity) {
101 if (capacity > capacity_) {
102 T* new_dynamic = static_cast<T*>(gpr_malloc(sizeof(T) * capacity));
103 move_elements(data(), new_dynamic, size_);
105 dynamic_ = new_dynamic;
106 capacity_ = capacity;
110 template <typename... Args>
111 void emplace_back(Args&&... args) {
112 if (size_ == capacity_) {
113 reserve(capacity_ * 2);
115 new (&(data()[size_])) T(std::forward<Args>(args)...);
119 void push_back(const T& value) { emplace_back(value); }
121 void push_back(T&& value) { emplace_back(std::move(value)); }
126 T& value = data()[s - 1];
131 size_t size() const { return size_; }
132 bool empty() const { return size_ == 0; }
134 size_t capacity() const { return capacity_; }
142 void copy_from(const InlinedVector& v) {
143 // if v is allocated, make sure we have enough capacity.
144 if (v.dynamic_ != nullptr) {
145 reserve(v.capacity_);
147 // copy over elements
148 for (size_t i = 0; i < v.size_; ++i) {
149 new (&(data()[i])) T(v[i]);
151 // copy over metadata
153 capacity_ = v.capacity_;
156 void move_from(InlinedVector& v) {
157 // if v is allocated, then we steal its dynamic array; otherwise, we
158 // move the elements individually.
159 if (v.dynamic_ != nullptr) {
160 dynamic_ = v.dynamic_;
162 move_elements(v.data(), data(), v.size_);
164 // copy over metadata
166 capacity_ = v.capacity_;
167 // null out the original
171 static void move_elements(T* src, T* dst, size_t num_elements) {
172 for (size_t i = 0; i < num_elements; ++i) {
173 new (&dst[i]) T(std::move(src[i]));
184 void destroy_elements() {
185 for (size_t i = 0; i < size_; ++i) {
186 T& value = data()[i];
192 typename std::aligned_storage<sizeof(T)>::type inline_[N];
198 } // namespace grpc_core
200 #endif /* GRPC_CORE_LIB_GPRPP_INLINED_VECTOR_H */