buffer.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #ifndef FLATBUFFERS_BUFFER_H_
  17. #define FLATBUFFERS_BUFFER_H_
  18. #include <algorithm>
  19. #include "base.h"
  20. namespace flatbuffers {
  21. // Wrapper for uoffset_t to allow safe template specialization.
  22. // Value is allowed to be 0 to indicate a null object (see e.g. AddOffset).
  23. template<typename T = void> struct Offset {
  24. // The type of offset to use.
  25. typedef uoffset_t offset_type;
  26. offset_type o;
  27. Offset() : o(0) {}
  28. Offset(const offset_type _o) : o(_o) {}
  29. Offset<> Union() const { return o; }
  30. bool IsNull() const { return !o; }
  31. };
  32. // Wrapper for uoffset64_t Offsets.
  33. template<typename T = void> struct Offset64 {
  34. // The type of offset to use.
  35. typedef uoffset64_t offset_type;
  36. offset_type o;
  37. Offset64() : o(0) {}
  38. Offset64(const offset_type offset) : o(offset) {}
  39. Offset64<> Union() const { return o; }
  40. bool IsNull() const { return !o; }
  41. };
  42. // Litmus check for ensuring the Offsets are the expected size.
  43. static_assert(sizeof(Offset<>) == 4, "Offset has wrong size");
  44. static_assert(sizeof(Offset64<>) == 8, "Offset64 has wrong size");
  45. inline void EndianCheck() {
  46. int endiantest = 1;
  47. // If this fails, see FLATBUFFERS_LITTLEENDIAN above.
  48. FLATBUFFERS_ASSERT(*reinterpret_cast<char *>(&endiantest) ==
  49. FLATBUFFERS_LITTLEENDIAN);
  50. (void)endiantest;
  51. }
  52. template<typename T> FLATBUFFERS_CONSTEXPR size_t AlignOf() {
  53. // clang-format off
  54. #ifdef _MSC_VER
  55. return __alignof(T);
  56. #else
  57. #ifndef alignof
  58. return __alignof__(T);
  59. #else
  60. return alignof(T);
  61. #endif
  62. #endif
  63. // clang-format on
  64. }
  65. // Lexicographically compare two strings (possibly containing nulls), and
  66. // return true if the first is less than the second.
  67. static inline bool StringLessThan(const char *a_data, uoffset_t a_size,
  68. const char *b_data, uoffset_t b_size) {
  69. const auto cmp = memcmp(a_data, b_data, (std::min)(a_size, b_size));
  70. return cmp == 0 ? a_size < b_size : cmp < 0;
  71. }
  72. // When we read serialized data from memory, in the case of most scalars,
  73. // we want to just read T, but in the case of Offset, we want to actually
  74. // perform the indirection and return a pointer.
  75. // The template specialization below does just that.
  76. // It is wrapped in a struct since function templates can't overload on the
  77. // return type like this.
  78. // The typedef is for the convenience of callers of this function
  79. // (avoiding the need for a trailing return decltype)
  80. template<typename T> struct IndirectHelper {
  81. typedef T return_type;
  82. typedef T mutable_return_type;
  83. static const size_t element_stride = sizeof(T);
  84. static return_type Read(const uint8_t *p, const size_t i) {
  85. return EndianScalar((reinterpret_cast<const T *>(p))[i]);
  86. }
  87. static mutable_return_type Read(uint8_t *p, const size_t i) {
  88. return reinterpret_cast<mutable_return_type>(
  89. Read(const_cast<const uint8_t *>(p), i));
  90. }
  91. };
  92. // For vector of Offsets.
  93. template<typename T, template<typename> class OffsetT>
  94. struct IndirectHelper<OffsetT<T>> {
  95. typedef const T *return_type;
  96. typedef T *mutable_return_type;
  97. typedef typename OffsetT<T>::offset_type offset_type;
  98. static const offset_type element_stride = sizeof(offset_type);
  99. static return_type Read(const uint8_t *const p, const offset_type i) {
  100. // Offsets are relative to themselves, so first update the pointer to
  101. // point to the offset location.
  102. const uint8_t *const offset_location = p + i * element_stride;
  103. // Then read the scalar value of the offset (which may be 32 or 64-bits) and
  104. // then determine the relative location from the offset location.
  105. return reinterpret_cast<return_type>(
  106. offset_location + ReadScalar<offset_type>(offset_location));
  107. }
  108. static mutable_return_type Read(uint8_t *const p, const offset_type i) {
  109. // Offsets are relative to themselves, so first update the pointer to
  110. // point to the offset location.
  111. uint8_t *const offset_location = p + i * element_stride;
  112. // Then read the scalar value of the offset (which may be 32 or 64-bits) and
  113. // then determine the relative location from the offset location.
  114. return reinterpret_cast<mutable_return_type>(
  115. offset_location + ReadScalar<offset_type>(offset_location));
  116. }
  117. };
  118. // For vector of structs.
  119. template<typename T> struct IndirectHelper<const T *> {
  120. typedef const T *return_type;
  121. typedef T *mutable_return_type;
  122. static const size_t element_stride = sizeof(T);
  123. static return_type Read(const uint8_t *const p, const size_t i) {
  124. // Structs are stored inline, relative to the first struct pointer.
  125. return reinterpret_cast<return_type>(p + i * element_stride);
  126. }
  127. static mutable_return_type Read(uint8_t *const p, const size_t i) {
  128. // Structs are stored inline, relative to the first struct pointer.
  129. return reinterpret_cast<mutable_return_type>(p + i * element_stride);
  130. }
  131. };
  132. /// @brief Get a pointer to the file_identifier section of the buffer.
  133. /// @return Returns a const char pointer to the start of the file_identifier
  134. /// characters in the buffer. The returned char * has length
  135. /// 'flatbuffers::FlatBufferBuilder::kFileIdentifierLength'.
  136. /// This function is UNDEFINED for FlatBuffers whose schema does not include
  137. /// a file_identifier (likely points at padding or the start of a the root
  138. /// vtable).
  139. inline const char *GetBufferIdentifier(const void *buf,
  140. bool size_prefixed = false) {
  141. return reinterpret_cast<const char *>(buf) +
  142. ((size_prefixed) ? 2 * sizeof(uoffset_t) : sizeof(uoffset_t));
  143. }
  144. // Helper to see if the identifier in a buffer has the expected value.
  145. inline bool BufferHasIdentifier(const void *buf, const char *identifier,
  146. bool size_prefixed = false) {
  147. return strncmp(GetBufferIdentifier(buf, size_prefixed), identifier,
  148. flatbuffers::kFileIdentifierLength) == 0;
  149. }
  150. /// @cond FLATBUFFERS_INTERNAL
  151. // Helpers to get a typed pointer to the root object contained in the buffer.
  152. template<typename T> T *GetMutableRoot(void *buf) {
  153. if (!buf) return nullptr;
  154. EndianCheck();
  155. return reinterpret_cast<T *>(
  156. reinterpret_cast<uint8_t *>(buf) +
  157. EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
  158. }
  159. template<typename T, typename SizeT = uoffset_t>
  160. T *GetMutableSizePrefixedRoot(void *buf) {
  161. return GetMutableRoot<T>(reinterpret_cast<uint8_t *>(buf) + sizeof(SizeT));
  162. }
  163. template<typename T> const T *GetRoot(const void *buf) {
  164. return GetMutableRoot<T>(const_cast<void *>(buf));
  165. }
  166. template<typename T, typename SizeT = uoffset_t>
  167. const T *GetSizePrefixedRoot(const void *buf) {
  168. return GetRoot<T>(reinterpret_cast<const uint8_t *>(buf) + sizeof(SizeT));
  169. }
  170. } // namespace flatbuffers
  171. #endif // FLATBUFFERS_BUFFER_H_