arenastring.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_ARENASTRING_H__
  31. #define GOOGLE_PROTOBUF_ARENASTRING_H__
  32. #include <algorithm>
  33. #include <string>
  34. #include <type_traits>
  35. #include <utility>
  36. #include "absl/log/absl_check.h"
  37. #include "google/protobuf/arena.h"
  38. #include "google/protobuf/port.h"
  39. #include "absl/strings/string_view.h"
  40. #include "google/protobuf/explicitly_constructed.h"
  41. // must be last:
  42. #include "google/protobuf/port_def.inc"
  43. #ifdef SWIG
  44. #error "You cannot SWIG proto headers"
  45. #endif
  46. namespace google {
  47. namespace protobuf {
  48. namespace internal {
  49. class EpsCopyInputStream;
  50. class SwapFieldHelper;
  51. // Declared in message_lite.h
  52. PROTOBUF_EXPORT extern ExplicitlyConstructedArenaString
  53. fixed_address_empty_string;
  54. // Lazy string instance to support string fields with non-empty default.
  55. // These are initialized on the first call to .get().
  56. class PROTOBUF_EXPORT LazyString {
  57. public:
  58. // We explicitly make LazyString an aggregate so that MSVC can do constant
  59. // initialization on it without marking it `constexpr`.
  60. // We do not want to use `constexpr` because it makes it harder to have extern
  61. // storage for it and causes library bloat.
  62. struct InitValue {
  63. const char* ptr;
  64. size_t size;
  65. };
  66. // We keep a union of the initialization value and the std::string to save on
  67. // space. We don't need the string array after Init() is done.
  68. union {
  69. mutable InitValue init_value_;
  70. alignas(std::string) mutable char string_buf_[sizeof(std::string)];
  71. };
  72. mutable std::atomic<const std::string*> inited_;
  73. const std::string& get() const {
  74. // This check generates less code than a call-once invocation.
  75. auto* res = inited_.load(std::memory_order_acquire);
  76. if (PROTOBUF_PREDICT_FALSE(res == nullptr)) return Init();
  77. return *res;
  78. }
  79. private:
  80. // Initialize the string in `string_buf_`, update `inited_` and return it.
  81. // We return it here to avoid having to read it again in the inlined code.
  82. const std::string& Init() const;
  83. };
  84. class TaggedStringPtr {
  85. public:
  86. // Bit flags qualifying string properties. We can use 2 bits as
  87. // ptr_ is guaranteed and enforced to be aligned on 4 byte boundaries.
  88. enum Flags {
  89. kArenaBit = 0x1, // ptr is arena allocated
  90. kMutableBit = 0x2, // ptr contents are fully mutable
  91. kMask = 0x3 // Bit mask
  92. };
  93. // Composed logical types
  94. enum Type {
  95. // Default strings are immutable and never owned.
  96. kDefault = 0,
  97. // Allocated strings are mutable and (as the name implies) owned.
  98. // A heap allocated string must be deleted.
  99. kAllocated = kMutableBit,
  100. // Mutable arena strings are strings where the string instance is owned
  101. // by the arena, but the string contents itself are owned by the string
  102. // instance. Mutable arena string instances need to be destroyed which is
  103. // typically done through a cleanup action added to the arena owning it.
  104. kMutableArena = kArenaBit | kMutableBit,
  105. // Fixed size arena strings are strings where both the string instance and
  106. // the string contents are fully owned by the arena. Fixed size arena
  107. // strings are a platform and c++ library specific customization. Fixed
  108. // size arena strings are immutable, with the exception of custom internal
  109. // updates to the content that fit inside the existing capacity.
  110. // Fixed size arena strings must never be deleted or destroyed.
  111. kFixedSizeArena = kArenaBit,
  112. };
  113. TaggedStringPtr() = default;
  114. explicit constexpr TaggedStringPtr(ExplicitlyConstructedArenaString* ptr)
  115. : ptr_(ptr) {}
  116. // Sets the value to `p`, tagging the value as being a 'default' value.
  117. // See documentation for kDefault for more info.
  118. inline const std::string* SetDefault(const std::string* p) {
  119. return TagAs(kDefault, const_cast<std::string*>(p));
  120. }
  121. // Sets the value to `p`, tagging the value as a heap allocated value.
  122. // Allocated strings are mutable and (as the name implies) owned.
  123. // `p` must not be null
  124. inline std::string* SetAllocated(std::string* p) {
  125. return TagAs(kAllocated, p);
  126. }
  127. // Sets the value to `p`, tagging the value as a fixed size arena string.
  128. // See documentation for kFixedSizeArena for more info.
  129. // `p` must not be null
  130. inline std::string* SetFixedSizeArena(std::string* p) {
  131. return TagAs(kFixedSizeArena, p);
  132. }
  133. // Sets the value to `p`, tagging the value as a mutable arena string.
  134. // See documentation for kMutableArena for more info.
  135. // `p` must not be null
  136. inline std::string* SetMutableArena(std::string* p) {
  137. return TagAs(kMutableArena, p);
  138. }
  139. // Returns true if the contents of the current string are fully mutable.
  140. inline bool IsMutable() const { return as_int() & kMutableBit; }
  141. // Returns true if the current string is an immutable default value.
  142. inline bool IsDefault() const { return (as_int() & kMask) == kDefault; }
  143. // If the current string is a heap-allocated mutable value, returns a pointer
  144. // to it. Returns nullptr otherwise.
  145. inline std::string* GetIfAllocated() const {
  146. auto allocated = as_int() ^ kAllocated;
  147. if (allocated & kMask) return nullptr;
  148. auto ptr = reinterpret_cast<std::string*>(allocated);
  149. PROTOBUF_ASSUME(ptr != nullptr);
  150. return ptr;
  151. }
  152. // Returns true if the current string is an arena allocated value.
  153. // This means it's either a mutable or fixed size arena string.
  154. inline bool IsArena() const { return as_int() & kArenaBit; }
  155. // Returns true if the current string is a fixed size arena allocated value.
  156. inline bool IsFixedSizeArena() const {
  157. return (as_int() & kMask) == kFixedSizeArena;
  158. }
  159. // Returns the contained string pointer.
  160. inline std::string* Get() const {
  161. return reinterpret_cast<std::string*>(as_int() & ~kMask);
  162. }
  163. // Returns true if the contained pointer is null, indicating some error.
  164. // The Null value is only used during parsing for temporary values.
  165. // A persisted ArenaStringPtr value is never null.
  166. inline bool IsNull() { return ptr_ == nullptr; }
  167. private:
  168. static inline void assert_aligned(const void* p) {
  169. ABSL_DCHECK_EQ(reinterpret_cast<uintptr_t>(p) & kMask, 0UL);
  170. }
  171. inline std::string* TagAs(Type type, std::string* p) {
  172. ABSL_DCHECK(p != nullptr);
  173. assert_aligned(p);
  174. ptr_ = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(p) | type);
  175. return p;
  176. }
  177. uintptr_t as_int() const { return reinterpret_cast<uintptr_t>(ptr_); }
  178. void* ptr_;
  179. };
  180. static_assert(std::is_trivial<TaggedStringPtr>::value,
  181. "TaggedStringPtr must be trivial");
  182. // This class encapsulates a pointer to a std::string with or without arena
  183. // owned contents, tagged by the bottom bits of the string pointer. It is a
  184. // high-level wrapper that almost directly corresponds to the interface required
  185. // by string fields in generated code. It replaces the old std::string* pointer
  186. // in such cases.
  187. //
  188. // The string pointer is tagged to be either a default, externally owned value,
  189. // a mutable heap allocated value, or an arena allocated value. The object uses
  190. // a single global instance of an empty string that is used as the initial
  191. // default value. Fields that have empty default values directly use this global
  192. // default. Fields that have non empty default values are supported through
  193. // lazily initialized default values managed by the LazyString class.
  194. //
  195. // Generated code and reflection code both ensure that ptr_ is never null.
  196. // Because ArenaStringPtr is used in oneof unions, its constructor is a NOP and
  197. // the field is always manually initialized via method calls.
  198. //
  199. // See TaggedStringPtr for more information about the types of string values
  200. // being held, and the mutable and ownership invariants for each type.
  201. struct PROTOBUF_EXPORT ArenaStringPtr {
  202. ArenaStringPtr() = default;
  203. constexpr ArenaStringPtr(ExplicitlyConstructedArenaString* default_value,
  204. ConstantInitialized)
  205. : tagged_ptr_(default_value) {}
  206. // Called from generated code / reflection runtime only. Resets value to point
  207. // to a default string pointer, with the semantics that this ArenaStringPtr
  208. // does not own the pointed-to memory. Disregards initial value of ptr_ (so
  209. // this is the *ONLY* safe method to call after construction or when
  210. // reinitializing after becoming the active field in a oneof union).
  211. inline void InitDefault();
  212. // Similar to `InitDefault` except that it allows the default value to be
  213. // initialized to an externally owned string. This method is called from
  214. // parsing code. `str` must not be null and outlive this instance.
  215. inline void InitExternal(const std::string* str);
  216. // Called from generated code / reflection runtime only. Resets the value of
  217. // this instances to the heap allocated value in `str`. `str` must not be
  218. // null. Invokes `arena->Own(str)` to transfer ownership into the arena if
  219. // `arena` is not null, else, `str` will be owned by ArenaStringPtr. This
  220. // function should only be used to initialize a ArenaStringPtr or on an
  221. // instance known to not carry any heap allocated value.
  222. inline void InitAllocated(std::string* str, Arena* arena);
  223. void Set(absl::string_view value, Arena* arena);
  224. void Set(std::string&& value, Arena* arena);
  225. template <typename... OverloadDisambiguator>
  226. void Set(const std::string& value, Arena* arena);
  227. void Set(const char* s, Arena* arena);
  228. void Set(const char* s, size_t n, Arena* arena);
  229. void SetBytes(absl::string_view value, Arena* arena);
  230. void SetBytes(std::string&& value, Arena* arena);
  231. template <typename... OverloadDisambiguator>
  232. void SetBytes(const std::string& value, Arena* arena);
  233. void SetBytes(const char* s, Arena* arena);
  234. void SetBytes(const void* p, size_t n, Arena* arena);
  235. template <typename RefWrappedType>
  236. void Set(std::reference_wrapper<RefWrappedType> const_string_ref,
  237. ::google::protobuf::Arena* arena) {
  238. Set(const_string_ref.get(), arena);
  239. }
  240. // Returns a mutable std::string reference.
  241. // The version accepting a `LazyString` value is used in the generated code to
  242. // initialize mutable copies for fields with a non-empty default where the
  243. // default value is lazily initialized.
  244. std::string* Mutable(Arena* arena);
  245. std::string* Mutable(const LazyString& default_value, Arena* arena);
  246. // Gets a mutable pointer with unspecified contents.
  247. // This function is identical to Mutable(), except it is optimized for the
  248. // case where the caller is not interested in the current contents. For
  249. // example, if the current field is not mutable, it will re-initialize the
  250. // value with an empty string rather than a (non-empty) default value.
  251. // Likewise, if the current value is a fixed size arena string with contents,
  252. // it will be initialized into an empty mutable arena string.
  253. std::string* MutableNoCopy(Arena* arena);
  254. // Basic accessors.
  255. PROTOBUF_NDEBUG_INLINE const std::string& Get() const {
  256. // Unconditionally mask away the tag.
  257. return *tagged_ptr_.Get();
  258. }
  259. // Returns a pointer to the stored contents for this instance.
  260. // This method is for internal debugging and tracking purposes only.
  261. PROTOBUF_NDEBUG_INLINE const std::string* UnsafeGetPointer() const
  262. PROTOBUF_RETURNS_NONNULL {
  263. return tagged_ptr_.Get();
  264. }
  265. // Release returns a std::string* instance that is heap-allocated and is not
  266. // Own()'d by any arena. If the field is not set, this returns nullptr. The
  267. // caller retains ownership. Clears this field back to the default state.
  268. // Used to implement release_<field>() methods on generated classes.
  269. PROTOBUF_NODISCARD std::string* Release();
  270. // Takes a std::string that is heap-allocated, and takes ownership. The
  271. // std::string's destructor is registered with the arena. Used to implement
  272. // set_allocated_<field> in generated classes.
  273. void SetAllocated(std::string* value, Arena* arena);
  274. // Frees storage (if not on an arena).
  275. void Destroy();
  276. // Clears content, but keeps allocated std::string, to avoid the overhead of
  277. // heap operations. After this returns, the content (as seen by the user) will
  278. // always be the empty std::string. Assumes that |default_value| is an empty
  279. // std::string.
  280. void ClearToEmpty();
  281. // Clears content, assuming that the current value is not the empty
  282. // string default.
  283. void ClearNonDefaultToEmpty();
  284. // Clears content, but keeps allocated std::string if arena != nullptr, to
  285. // avoid the overhead of heap operations. After this returns, the content
  286. // (as seen by the user) will always be equal to |default_value|.
  287. void ClearToDefault(const LazyString& default_value, ::google::protobuf::Arena* arena);
  288. // Swaps internal pointers. Arena-safety semantics: this is guarded by the
  289. // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is
  290. // 'unsafe' if called directly.
  291. inline PROTOBUF_NDEBUG_INLINE static void InternalSwap(ArenaStringPtr* rhs,
  292. Arena* rhs_arena,
  293. ArenaStringPtr* lhs,
  294. Arena* lhs_arena);
  295. // Internal setter used only at parse time to directly set a donated string
  296. // value.
  297. void UnsafeSetTaggedPointer(TaggedStringPtr value) { tagged_ptr_ = value; }
  298. // Generated code only! An optimization, in certain cases the generated
  299. // code is certain we can obtain a std::string with no default checks and
  300. // tag tests.
  301. std::string* UnsafeMutablePointer() PROTOBUF_RETURNS_NONNULL;
  302. // Returns true if this instances holds an immutable default value.
  303. inline bool IsDefault() const { return tagged_ptr_.IsDefault(); }
  304. private:
  305. template <typename... Args>
  306. inline std::string* NewString(Arena* arena, Args&&... args) {
  307. if (arena == nullptr) {
  308. auto* s = new std::string(std::forward<Args>(args)...);
  309. return tagged_ptr_.SetAllocated(s);
  310. } else {
  311. auto* s = Arena::Create<std::string>(arena, std::forward<Args>(args)...);
  312. return tagged_ptr_.SetMutableArena(s);
  313. }
  314. }
  315. TaggedStringPtr tagged_ptr_;
  316. bool IsFixedSizeArena() const { return false; }
  317. // Swaps tagged pointer without debug hardening. This is to allow python
  318. // protobuf to maintain pointer stability even in DEBUG builds.
  319. inline PROTOBUF_NDEBUG_INLINE static void UnsafeShallowSwap(
  320. ArenaStringPtr* rhs, ArenaStringPtr* lhs) {
  321. std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
  322. }
  323. friend class ::google::protobuf::internal::SwapFieldHelper;
  324. friend class TcParser;
  325. // Slow paths.
  326. // MutableSlow requires that !IsString() || IsDefault
  327. // Variadic to support 0 args for empty default and 1 arg for LazyString.
  328. template <typename... Lazy>
  329. std::string* MutableSlow(::google::protobuf::Arena* arena, const Lazy&... lazy_default);
  330. friend class EpsCopyInputStream;
  331. };
  332. inline void ArenaStringPtr::InitDefault() {
  333. tagged_ptr_ = TaggedStringPtr(&fixed_address_empty_string);
  334. }
  335. inline void ArenaStringPtr::InitExternal(const std::string* str) {
  336. tagged_ptr_.SetDefault(str);
  337. }
  338. inline void ArenaStringPtr::InitAllocated(std::string* str, Arena* arena) {
  339. if (arena != nullptr) {
  340. tagged_ptr_.SetMutableArena(str);
  341. arena->Own(str);
  342. } else {
  343. tagged_ptr_.SetAllocated(str);
  344. }
  345. }
  346. inline void ArenaStringPtr::Set(const char* s, Arena* arena) {
  347. Set(absl::string_view{s}, arena);
  348. }
  349. inline void ArenaStringPtr::Set(const char* s, size_t n, Arena* arena) {
  350. Set(absl::string_view{s, n}, arena);
  351. }
  352. inline void ArenaStringPtr::SetBytes(absl::string_view value, Arena* arena) {
  353. Set(value, arena);
  354. }
  355. template <>
  356. PROTOBUF_EXPORT void ArenaStringPtr::Set(const std::string& value,
  357. Arena* arena);
  358. template <>
  359. inline void ArenaStringPtr::SetBytes(const std::string& value, Arena* arena) {
  360. Set(value, arena);
  361. }
  362. inline void ArenaStringPtr::SetBytes(std::string&& value, Arena* arena) {
  363. Set(std::move(value), arena);
  364. }
  365. inline void ArenaStringPtr::SetBytes(const char* s, Arena* arena) {
  366. Set(s, arena);
  367. }
  368. inline void ArenaStringPtr::SetBytes(const void* p, size_t n, Arena* arena) {
  369. Set(absl::string_view{static_cast<const char*>(p), n}, arena);
  370. }
  371. // Make sure rhs_arena allocated rhs, and lhs_arena allocated lhs.
  372. inline PROTOBUF_NDEBUG_INLINE void ArenaStringPtr::InternalSwap( //
  373. ArenaStringPtr* rhs, Arena* rhs_arena, //
  374. ArenaStringPtr* lhs, Arena* lhs_arena) {
  375. // Silence unused variable warnings in release buildls.
  376. (void)rhs_arena;
  377. (void)lhs_arena;
  378. std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
  379. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  380. auto force_realloc = [](ArenaStringPtr* p, Arena* arena) {
  381. if (p->IsDefault()) return;
  382. std::string* old_value = p->tagged_ptr_.Get();
  383. std::string* new_value =
  384. p->IsFixedSizeArena()
  385. ? Arena::Create<std::string>(arena, *old_value)
  386. : Arena::Create<std::string>(arena, std::move(*old_value));
  387. if (arena == nullptr) {
  388. delete old_value;
  389. p->tagged_ptr_.SetAllocated(new_value);
  390. } else {
  391. p->tagged_ptr_.SetMutableArena(new_value);
  392. }
  393. };
  394. // Because, at this point, tagged_ptr_ has been swapped, arena should also be
  395. // swapped.
  396. force_realloc(lhs, rhs_arena);
  397. force_realloc(rhs, lhs_arena);
  398. #endif // PROTOBUF_FORCE_COPY_IN_SWAP
  399. }
  400. inline void ArenaStringPtr::ClearNonDefaultToEmpty() {
  401. // Unconditionally mask away the tag.
  402. tagged_ptr_.Get()->clear();
  403. }
  404. inline std::string* ArenaStringPtr::UnsafeMutablePointer() {
  405. ABSL_DCHECK(tagged_ptr_.IsMutable());
  406. ABSL_DCHECK(tagged_ptr_.Get() != nullptr);
  407. return tagged_ptr_.Get();
  408. }
  409. } // namespace internal
  410. } // namespace protobuf
  411. } // namespace google
  412. #include "google/protobuf/port_undef.inc"
  413. #endif // GOOGLE_PROTOBUF_ARENASTRING_H__