Skip to content

Commit e8c59f4

Browse files
committed
⬆️ Upgrade flatbuffers to 2.0.7
The following patches from upstream are used: ``` --- a/flatbuffers/lib/flatbuffers/src/util.cpp +++ b/flatbuffers/lib/flatbuffers/src/util.cpp @@ -51,7 +51,6 @@ #include <cstdlib> #include <fstream> #include <functional> -#include <sstream> #include "flatbuffers/base.h" ```
1 parent e4c9625 commit e8c59f4

37 files changed

+7978
-4288
lines changed

flatbuffers/lib/flatbuffers/LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
same "printed page" as the copyright notice for easier
188188
identification within third-party archives.
189189

190-
Copyright 2014 Google Inc.
190+
Copyright [yyyy] [name of copyright owner]
191191

192192
Licensed under the Apache License, Version 2.0 (the "License");
193193
you may not use this file except in compliance with the License.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2021 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FLATBUFFERS_ALLOCATOR_H_
18+
#define FLATBUFFERS_ALLOCATOR_H_
19+
20+
#include "flatbuffers/base.h"
21+
22+
namespace flatbuffers {
23+
24+
// Allocator interface. This is flatbuffers-specific and meant only for
25+
// `vector_downward` usage.
26+
class Allocator {
27+
public:
28+
virtual ~Allocator() {}
29+
30+
// Allocate `size` bytes of memory.
31+
virtual uint8_t *allocate(size_t size) = 0;
32+
33+
// Deallocate `size` bytes of memory at `p` allocated by this allocator.
34+
virtual void deallocate(uint8_t *p, size_t size) = 0;
35+
36+
// Reallocate `new_size` bytes of memory, replacing the old region of size
37+
// `old_size` at `p`. In contrast to a normal realloc, this grows downwards,
38+
// and is intended specifcally for `vector_downward` use.
39+
// `in_use_back` and `in_use_front` indicate how much of `old_size` is
40+
// actually in use at each end, and needs to be copied.
41+
virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size,
42+
size_t new_size, size_t in_use_back,
43+
size_t in_use_front) {
44+
FLATBUFFERS_ASSERT(new_size > old_size); // vector_downward only grows
45+
uint8_t *new_p = allocate(new_size);
46+
memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
47+
in_use_front);
48+
deallocate(old_p, old_size);
49+
return new_p;
50+
}
51+
52+
protected:
53+
// Called by `reallocate_downward` to copy memory from `old_p` of `old_size`
54+
// to `new_p` of `new_size`. Only memory of size `in_use_front` and
55+
// `in_use_back` will be copied from the front and back of the old memory
56+
// allocation.
57+
void memcpy_downward(uint8_t *old_p, size_t old_size, uint8_t *new_p,
58+
size_t new_size, size_t in_use_back,
59+
size_t in_use_front) {
60+
memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back,
61+
in_use_back);
62+
memcpy(new_p, old_p, in_use_front);
63+
}
64+
};
65+
66+
} // namespace flatbuffers
67+
68+
#endif // FLATBUFFERS_ALLOCATOR_H_
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/*
2+
* Copyright 2021 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FLATBUFFERS_ARRAY_H_
18+
#define FLATBUFFERS_ARRAY_H_
19+
20+
#include "flatbuffers/base.h"
21+
#include "flatbuffers/stl_emulation.h"
22+
#include "flatbuffers/vector.h"
23+
24+
namespace flatbuffers {
25+
26+
// This is used as a helper type for accessing arrays.
27+
template<typename T, uint16_t length> class Array {
28+
// Array<T> can carry only POD data types (scalars or structs).
29+
typedef typename flatbuffers::bool_constant<flatbuffers::is_scalar<T>::value>
30+
scalar_tag;
31+
typedef
32+
typename flatbuffers::conditional<scalar_tag::value, T, const T *>::type
33+
IndirectHelperType;
34+
35+
public:
36+
typedef uint16_t size_type;
37+
typedef typename IndirectHelper<IndirectHelperType>::return_type return_type;
38+
typedef VectorIterator<T, return_type> const_iterator;
39+
typedef VectorReverseIterator<const_iterator> const_reverse_iterator;
40+
41+
// If T is a LE-scalar or a struct (!scalar_tag::value).
42+
static FLATBUFFERS_CONSTEXPR bool is_span_observable =
43+
(scalar_tag::value && (FLATBUFFERS_LITTLEENDIAN || sizeof(T) == 1)) ||
44+
!scalar_tag::value;
45+
46+
FLATBUFFERS_CONSTEXPR uint16_t size() const { return length; }
47+
48+
return_type Get(uoffset_t i) const {
49+
FLATBUFFERS_ASSERT(i < size());
50+
return IndirectHelper<IndirectHelperType>::Read(Data(), i);
51+
}
52+
53+
return_type operator[](uoffset_t i) const { return Get(i); }
54+
55+
// If this is a Vector of enums, T will be its storage type, not the enum
56+
// type. This function makes it convenient to retrieve value with enum
57+
// type E.
58+
template<typename E> E GetEnum(uoffset_t i) const {
59+
return static_cast<E>(Get(i));
60+
}
61+
62+
const_iterator begin() const { return const_iterator(Data(), 0); }
63+
const_iterator end() const { return const_iterator(Data(), size()); }
64+
65+
const_reverse_iterator rbegin() const {
66+
return const_reverse_iterator(end());
67+
}
68+
const_reverse_iterator rend() const {
69+
return const_reverse_iterator(begin());
70+
}
71+
72+
const_iterator cbegin() const { return begin(); }
73+
const_iterator cend() const { return end(); }
74+
75+
const_reverse_iterator crbegin() const { return rbegin(); }
76+
const_reverse_iterator crend() const { return rend(); }
77+
78+
// Get a mutable pointer to elements inside this array.
79+
// This method used to mutate arrays of structs followed by a @p Mutate
80+
// operation. For primitive types use @p Mutate directly.
81+
// @warning Assignments and reads to/from the dereferenced pointer are not
82+
// automatically converted to the correct endianness.
83+
typename flatbuffers::conditional<scalar_tag::value, void, T *>::type
84+
GetMutablePointer(uoffset_t i) const {
85+
FLATBUFFERS_ASSERT(i < size());
86+
return const_cast<T *>(&data()[i]);
87+
}
88+
89+
// Change elements if you have a non-const pointer to this object.
90+
void Mutate(uoffset_t i, const T &val) { MutateImpl(scalar_tag(), i, val); }
91+
92+
// The raw data in little endian format. Use with care.
93+
const uint8_t *Data() const { return data_; }
94+
95+
uint8_t *Data() { return data_; }
96+
97+
// Similarly, but typed, much like std::vector::data
98+
const T *data() const { return reinterpret_cast<const T *>(Data()); }
99+
T *data() { return reinterpret_cast<T *>(Data()); }
100+
101+
// Copy data from a span with endian conversion.
102+
// If this Array and the span overlap, the behavior is undefined.
103+
void CopyFromSpan(flatbuffers::span<const T, length> src) {
104+
const auto p1 = reinterpret_cast<const uint8_t *>(src.data());
105+
const auto p2 = Data();
106+
FLATBUFFERS_ASSERT(!(p1 >= p2 && p1 < (p2 + length)) &&
107+
!(p2 >= p1 && p2 < (p1 + length)));
108+
(void)p1;
109+
(void)p2;
110+
CopyFromSpanImpl(flatbuffers::bool_constant<is_span_observable>(), src);
111+
}
112+
113+
protected:
114+
void MutateImpl(flatbuffers::true_type, uoffset_t i, const T &val) {
115+
FLATBUFFERS_ASSERT(i < size());
116+
WriteScalar(data() + i, val);
117+
}
118+
119+
void MutateImpl(flatbuffers::false_type, uoffset_t i, const T &val) {
120+
*(GetMutablePointer(i)) = val;
121+
}
122+
123+
void CopyFromSpanImpl(flatbuffers::true_type,
124+
flatbuffers::span<const T, length> src) {
125+
// Use std::memcpy() instead of std::copy() to avoid performance degradation
126+
// due to aliasing if T is char or unsigned char.
127+
// The size is known at compile time, so memcpy would be inlined.
128+
std::memcpy(data(), src.data(), length * sizeof(T));
129+
}
130+
131+
// Copy data from flatbuffers::span with endian conversion.
132+
void CopyFromSpanImpl(flatbuffers::false_type,
133+
flatbuffers::span<const T, length> src) {
134+
for (size_type k = 0; k < length; k++) { Mutate(k, src[k]); }
135+
}
136+
137+
// This class is only used to access pre-existing data. Don't ever
138+
// try to construct these manually.
139+
// 'constexpr' allows us to use 'size()' at compile time.
140+
// @note Must not use 'FLATBUFFERS_CONSTEXPR' here, as const is not allowed on
141+
// a constructor.
142+
#if defined(__cpp_constexpr)
143+
constexpr Array();
144+
#else
145+
Array();
146+
#endif
147+
148+
uint8_t data_[length * sizeof(T)];
149+
150+
private:
151+
// This class is a pointer. Copying will therefore create an invalid object.
152+
// Private and unimplemented copy constructor.
153+
Array(const Array &);
154+
Array &operator=(const Array &);
155+
};
156+
157+
// Specialization for Array[struct] with access using Offset<void> pointer.
158+
// This specialization used by idl_gen_text.cpp.
159+
template<typename T, uint16_t length> class Array<Offset<T>, length> {
160+
static_assert(flatbuffers::is_same<T, void>::value, "unexpected type T");
161+
162+
public:
163+
typedef const void *return_type;
164+
165+
const uint8_t *Data() const { return data_; }
166+
167+
// Make idl_gen_text.cpp::PrintContainer happy.
168+
return_type operator[](uoffset_t) const {
169+
FLATBUFFERS_ASSERT(false);
170+
return nullptr;
171+
}
172+
173+
private:
174+
// This class is only used to access pre-existing data.
175+
Array();
176+
Array(const Array &);
177+
Array &operator=(const Array &);
178+
179+
uint8_t data_[1];
180+
};
181+
182+
template<class U, uint16_t N>
183+
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<U, N> make_span(Array<U, N> &arr)
184+
FLATBUFFERS_NOEXCEPT {
185+
static_assert(
186+
Array<U, N>::is_span_observable,
187+
"wrong type U, only plain struct, LE-scalar, or byte types are allowed");
188+
return span<U, N>(arr.data(), N);
189+
}
190+
191+
template<class U, uint16_t N>
192+
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const U, N> make_span(
193+
const Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
194+
static_assert(
195+
Array<U, N>::is_span_observable,
196+
"wrong type U, only plain struct, LE-scalar, or byte types are allowed");
197+
return span<const U, N>(arr.data(), N);
198+
}
199+
200+
template<class U, uint16_t N>
201+
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<uint8_t, sizeof(U) * N>
202+
make_bytes_span(Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
203+
static_assert(Array<U, N>::is_span_observable,
204+
"internal error, Array<T> might hold only scalars or structs");
205+
return span<uint8_t, sizeof(U) * N>(arr.Data(), sizeof(U) * N);
206+
}
207+
208+
template<class U, uint16_t N>
209+
FLATBUFFERS_CONSTEXPR_CPP11 flatbuffers::span<const uint8_t, sizeof(U) * N>
210+
make_bytes_span(const Array<U, N> &arr) FLATBUFFERS_NOEXCEPT {
211+
static_assert(Array<U, N>::is_span_observable,
212+
"internal error, Array<T> might hold only scalars or structs");
213+
return span<const uint8_t, sizeof(U) * N>(arr.Data(), sizeof(U) * N);
214+
}
215+
216+
// Cast a raw T[length] to a raw flatbuffers::Array<T, length>
217+
// without endian conversion. Use with care.
218+
// TODO: move these Cast-methods to `internal` namespace.
219+
template<typename T, uint16_t length>
220+
Array<T, length> &CastToArray(T (&arr)[length]) {
221+
return *reinterpret_cast<Array<T, length> *>(arr);
222+
}
223+
224+
template<typename T, uint16_t length>
225+
const Array<T, length> &CastToArray(const T (&arr)[length]) {
226+
return *reinterpret_cast<const Array<T, length> *>(arr);
227+
}
228+
229+
template<typename E, typename T, uint16_t length>
230+
Array<E, length> &CastToArrayOfEnum(T (&arr)[length]) {
231+
static_assert(sizeof(E) == sizeof(T), "invalid enum type E");
232+
return *reinterpret_cast<Array<E, length> *>(arr);
233+
}
234+
235+
template<typename E, typename T, uint16_t length>
236+
const Array<E, length> &CastToArrayOfEnum(const T (&arr)[length]) {
237+
static_assert(sizeof(E) == sizeof(T), "invalid enum type E");
238+
return *reinterpret_cast<const Array<E, length> *>(arr);
239+
}
240+
241+
} // namespace flatbuffers
242+
243+
#endif // FLATBUFFERS_ARRAY_H_

0 commit comments

Comments
 (0)