You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

arenastring.h 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 <google/protobuf/stubs/logging.h>
  37. #include <google/protobuf/stubs/common.h>
  38. #include <google/protobuf/arena.h>
  39. #include <google/protobuf/port.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. {
  48. namespace protobuf
  49. {
  50. namespace internal
  51. {
  52. class EpsCopyInputStream;
  53. class SwapFieldHelper;
  54. // Declared in message_lite.h
  55. PROTOBUF_EXPORT extern ExplicitlyConstructedArenaString
  56. fixed_address_empty_string;
  57. // Lazy string instance to support string fields with non-empty default.
  58. // These are initialized on the first call to .get().
  59. class PROTOBUF_EXPORT LazyString
  60. {
  61. public:
  62. // We explicitly make LazyString an aggregate so that MSVC can do constant
  63. // initialization on it without marking it `constexpr`.
  64. // We do not want to use `constexpr` because it makes it harder to have extern
  65. // storage for it and causes library bloat.
  66. struct InitValue
  67. {
  68. const char* ptr;
  69. size_t size;
  70. };
  71. // We keep a union of the initialization value and the std::string to save on
  72. // space. We don't need the string array after Init() is done.
  73. union
  74. {
  75. mutable InitValue init_value_;
  76. alignas(std::string) mutable char string_buf_[sizeof(std::string)];
  77. };
  78. mutable std::atomic<const std::string*> inited_;
  79. const std::string& get() const
  80. {
  81. // This check generates less code than a call-once invocation.
  82. auto* res = inited_.load(std::memory_order_acquire);
  83. if (PROTOBUF_PREDICT_FALSE(res == nullptr))
  84. return Init();
  85. return *res;
  86. }
  87. private:
  88. // Initialize the string in `string_buf_`, update `inited_` and return it.
  89. // We return it here to avoid having to read it again in the inlined code.
  90. const std::string& Init() const;
  91. };
  92. class TaggedStringPtr
  93. {
  94. public:
  95. // Bit flags qualifying string properties. We can use 2 bits as
  96. // ptr_ is guaranteed and enforced to be aligned on 4 byte boundaries.
  97. enum Flags
  98. {
  99. kArenaBit = 0x1, // ptr is arena allocated
  100. kMutableBit = 0x2, // ptr contents are fully mutable
  101. kMask = 0x3 // Bit mask
  102. };
  103. // Composed logical types
  104. enum Type
  105. {
  106. // Default strings are immutable and never owned.
  107. kDefault = 0,
  108. // Allocated strings are mutable and (as the name implies) owned.
  109. // A heap allocated string must be deleted.
  110. kAllocated = kMutableBit,
  111. // Mutable arena strings are strings where the string instance is owned
  112. // by the arena, but the string contents itself are owned by the string
  113. // instance. Mutable arena string instances need to be destroyed which is
  114. // typically done through a cleanup action added to the arena owning it.
  115. kMutableArena = kArenaBit | kMutableBit,
  116. // Fixed size arena strings are strings where both the string instance and
  117. // the string contents are fully owned by the arena. Fixed size arena
  118. // strings are a platform and c++ library specific customization. Fixed
  119. // size arena strings are immutable, with the exception of custom internal
  120. // updates to the content that fit inside the existing capacity.
  121. // Fixed size arena strings must never be deleted or destroyed.
  122. kFixedSizeArena = kArenaBit,
  123. };
  124. TaggedStringPtr() = default;
  125. explicit constexpr TaggedStringPtr(ExplicitlyConstructedArenaString* ptr) :
  126. ptr_(ptr)
  127. {
  128. }
  129. // Sets the value to `p`, tagging the value as being a 'default' value.
  130. // See documentation for kDefault for more info.
  131. inline const std::string* SetDefault(const std::string* p)
  132. {
  133. return TagAs(kDefault, const_cast<std::string*>(p));
  134. }
  135. // Sets the value to `p`, tagging the value as a heap allocated value.
  136. // Allocated strings are mutable and (as the name implies) owned.
  137. // `p` must not be null
  138. inline std::string* SetAllocated(std::string* p)
  139. {
  140. return TagAs(kAllocated, p);
  141. }
  142. // Sets the value to `p`, tagging the value as a fixed size arena string.
  143. // See documentation for kFixedSizeArena for more info.
  144. // `p` must not be null
  145. inline std::string* SetFixedSizeArena(std::string* p)
  146. {
  147. return TagAs(kFixedSizeArena, p);
  148. }
  149. // Sets the value to `p`, tagging the value as a mutable arena string.
  150. // See documentation for kMutableArena for more info.
  151. // `p` must not be null
  152. inline std::string* SetMutableArena(std::string* p)
  153. {
  154. return TagAs(kMutableArena, p);
  155. }
  156. // Returns true if the contents of the current string are fully mutable.
  157. inline bool IsMutable() const
  158. {
  159. return as_int() & kMutableBit;
  160. }
  161. // Returns true if the current string is an immutable default value.
  162. inline bool IsDefault() const
  163. {
  164. return (as_int() & kMask) == kDefault;
  165. }
  166. // If the current string is a heap-allocated mutable value, returns a pointer
  167. // to it. Returns nullptr otherwise.
  168. inline std::string* GetIfAllocated() const
  169. {
  170. auto allocated = as_int() ^ kAllocated;
  171. if (allocated & kMask)
  172. return nullptr;
  173. auto ptr = reinterpret_cast<std::string*>(allocated);
  174. PROTOBUF_ASSUME(ptr != nullptr);
  175. return ptr;
  176. }
  177. // Returns true if the current string is an arena allocated value.
  178. // This means it's either a mutable or fixed size arena string.
  179. inline bool IsArena() const
  180. {
  181. return as_int() & kArenaBit;
  182. }
  183. // Returns true if the current string is a fixed size arena allocated value.
  184. inline bool IsFixedSizeArena() const
  185. {
  186. return (as_int() & kMask) == kFixedSizeArena;
  187. }
  188. // Returns the contained string pointer.
  189. inline std::string* Get() const
  190. {
  191. return reinterpret_cast<std::string*>(as_int() & ~kMask);
  192. }
  193. // Returns true if the contained pointer is null, indicating some error.
  194. // The Null value is only used during parsing for temporary values.
  195. // A persisted ArenaStringPtr value is never null.
  196. inline bool IsNull()
  197. {
  198. return ptr_ == nullptr;
  199. }
  200. private:
  201. static inline void assert_aligned(const void* p)
  202. {
  203. GOOGLE_DCHECK_EQ(reinterpret_cast<uintptr_t>(p) & kMask, 0UL);
  204. }
  205. inline std::string* TagAs(Type type, std::string* p)
  206. {
  207. GOOGLE_DCHECK(p != nullptr);
  208. assert_aligned(p);
  209. ptr_ = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(p) | type);
  210. return p;
  211. }
  212. uintptr_t as_int() const
  213. {
  214. return reinterpret_cast<uintptr_t>(ptr_);
  215. }
  216. void* ptr_;
  217. };
  218. static_assert(std::is_trivial<TaggedStringPtr>::value, "TaggedStringPtr must be trivial");
  219. // This class encapsulates a pointer to a std::string with or without arena
  220. // owned contents, tagged by the bottom bits of the string pointer. It is a
  221. // high-level wrapper that almost directly corresponds to the interface required
  222. // by string fields in generated code. It replaces the old std::string* pointer
  223. // in such cases.
  224. //
  225. // The string pointer is tagged to be either a default, externally owned value,
  226. // a mutable heap allocated value, or an arena allocated value. The object uses
  227. // a single global instance of an empty string that is used as the initial
  228. // default value. Fields that have empty default values directly use this global
  229. // default. Fields that have non empty default values are supported through
  230. // lazily initialized default values managed by the LazyString class.
  231. //
  232. // Generated code and reflection code both ensure that ptr_ is never null.
  233. // Because ArenaStringPtr is used in oneof unions, its constructor is a NOP and
  234. // the field is always manually initialized via method calls.
  235. //
  236. // See TaggedStringPtr for more information about the types of string values
  237. // being held, and the mutable and ownership invariants for each type.
  238. struct PROTOBUF_EXPORT ArenaStringPtr
  239. {
  240. ArenaStringPtr() = default;
  241. constexpr ArenaStringPtr(ExplicitlyConstructedArenaString* default_value, ConstantInitialized) :
  242. tagged_ptr_(default_value)
  243. {
  244. }
  245. // Called from generated code / reflection runtime only. Resets value to point
  246. // to a default string pointer, with the semantics that this ArenaStringPtr
  247. // does not own the pointed-to memory. Disregards initial value of ptr_ (so
  248. // this is the *ONLY* safe method to call after construction or when
  249. // reinitializing after becoming the active field in a oneof union).
  250. inline void InitDefault();
  251. // Similar to `InitDefault` except that it allows the default value to be
  252. // initialized to an externally owned string. This method is called from
  253. // parsing code. `str` must not be null and outlive this instance.
  254. inline void InitExternal(const std::string* str);
  255. // Called from generated code / reflection runtime only. Resets the value of
  256. // this instances to the heap allocated value in `str`. `str` must not be
  257. // null. Invokes `arena->Own(str)` to transfer ownership into the arena if
  258. // `arena` is not null, else, `str` will be owned by ArenaStringPtr. This
  259. // function should only be used to initialize a ArenaStringPtr or on an
  260. // instance known to not carry any heap allocated value.
  261. inline void InitAllocated(std::string* str, Arena* arena);
  262. void Set(ConstStringParam value, Arena* arena);
  263. void Set(std::string&& value, Arena* arena);
  264. void Set(const char* s, Arena* arena);
  265. void Set(const char* s, size_t n, Arena* arena);
  266. void SetBytes(ConstStringParam value, Arena* arena);
  267. void SetBytes(std::string&& value, Arena* arena);
  268. void SetBytes(const char* s, Arena* arena);
  269. void SetBytes(const void* p, size_t n, Arena* arena);
  270. template<typename RefWrappedType>
  271. void Set(std::reference_wrapper<RefWrappedType> const_string_ref, ::google::protobuf::Arena* arena)
  272. {
  273. Set(const_string_ref.get(), arena);
  274. }
  275. // Returns a mutable std::string reference.
  276. // The version accepting a `LazyString` value is used in the generated code to
  277. // initialize mutable copies for fields with a non-empty default where the
  278. // default value is lazily initialized.
  279. std::string* Mutable(Arena* arena);
  280. std::string* Mutable(const LazyString& default_value, Arena* arena);
  281. // Gets a mutable pointer with unspecified contents.
  282. // This function is identical to Mutable(), except it is optimized for the
  283. // case where the caller is not interested in the current contents. For
  284. // example, if the current field is not mutable, it will re-initialize the
  285. // value with an empty string rather than a (non-empty) default value.
  286. // Likewise, if the current value is a fixed size arena string with contents,
  287. // it will be initialized into an empty mutable arena string.
  288. std::string* MutableNoCopy(Arena* arena);
  289. // Basic accessors.
  290. PROTOBUF_NDEBUG_INLINE const std::string& Get() const
  291. {
  292. // Unconditionally mask away the tag.
  293. return *tagged_ptr_.Get();
  294. }
  295. // Returns a pointer to the stored contents for this instance.
  296. // This method is for internal debugging and tracking purposes only.
  297. PROTOBUF_NDEBUG_INLINE const std::string* UnsafeGetPointer() const
  298. PROTOBUF_RETURNS_NONNULL
  299. {
  300. return tagged_ptr_.Get();
  301. }
  302. // Release returns a std::string* instance that is heap-allocated and is not
  303. // Own()'d by any arena. If the field is not set, this returns nullptr. The
  304. // caller retains ownership. Clears this field back to the default state.
  305. // Used to implement release_<field>() methods on generated classes.
  306. PROTOBUF_NODISCARD std::string* Release();
  307. // Takes a std::string that is heap-allocated, and takes ownership. The
  308. // std::string's destructor is registered with the arena. Used to implement
  309. // set_allocated_<field> in generated classes.
  310. void SetAllocated(std::string* value, Arena* arena);
  311. // Frees storage (if not on an arena).
  312. void Destroy();
  313. // Clears content, but keeps allocated std::string, to avoid the overhead of
  314. // heap operations. After this returns, the content (as seen by the user) will
  315. // always be the empty std::string. Assumes that |default_value| is an empty
  316. // std::string.
  317. void ClearToEmpty();
  318. // Clears content, assuming that the current value is not the empty
  319. // string default.
  320. void ClearNonDefaultToEmpty();
  321. // Clears content, but keeps allocated std::string if arena != nullptr, to
  322. // avoid the overhead of heap operations. After this returns, the content
  323. // (as seen by the user) will always be equal to |default_value|.
  324. void ClearToDefault(const LazyString& default_value, ::google::protobuf::Arena* arena);
  325. // Swaps internal pointers. Arena-safety semantics: this is guarded by the
  326. // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is
  327. // 'unsafe' if called directly.
  328. inline PROTOBUF_NDEBUG_INLINE static void InternalSwap(ArenaStringPtr* rhs, Arena* rhs_arena, ArenaStringPtr* lhs, Arena* lhs_arena);
  329. // Internal setter used only at parse time to directly set a donated string
  330. // value.
  331. void UnsafeSetTaggedPointer(TaggedStringPtr value)
  332. {
  333. tagged_ptr_ = value;
  334. }
  335. // Generated code only! An optimization, in certain cases the generated
  336. // code is certain we can obtain a std::string with no default checks and
  337. // tag tests.
  338. std::string* UnsafeMutablePointer() PROTOBUF_RETURNS_NONNULL;
  339. // Returns true if this instances holds an immutable default value.
  340. inline bool IsDefault() const
  341. {
  342. return tagged_ptr_.IsDefault();
  343. }
  344. private:
  345. template<typename... Args>
  346. inline std::string* NewString(Arena* arena, Args&&... args)
  347. {
  348. if (arena == nullptr)
  349. {
  350. auto* s = new std::string(std::forward<Args>(args)...);
  351. return tagged_ptr_.SetAllocated(s);
  352. }
  353. else
  354. {
  355. auto* s = Arena::Create<std::string>(arena, std::forward<Args>(args)...);
  356. return tagged_ptr_.SetMutableArena(s);
  357. }
  358. }
  359. TaggedStringPtr tagged_ptr_;
  360. bool IsFixedSizeArena() const
  361. {
  362. return false;
  363. }
  364. // Swaps tagged pointer without debug hardening. This is to allow python
  365. // protobuf to maintain pointer stability even in DEBUG builds.
  366. inline PROTOBUF_NDEBUG_INLINE static void UnsafeShallowSwap(
  367. ArenaStringPtr* rhs, ArenaStringPtr* lhs
  368. )
  369. {
  370. std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
  371. }
  372. friend class ::google::protobuf::internal::SwapFieldHelper;
  373. friend class TcParser;
  374. // Slow paths.
  375. // MutableSlow requires that !IsString() || IsDefault
  376. // Variadic to support 0 args for empty default and 1 arg for LazyString.
  377. template<typename... Lazy>
  378. std::string* MutableSlow(::google::protobuf::Arena* arena, const Lazy&... lazy_default);
  379. friend class EpsCopyInputStream;
  380. };
  381. inline void ArenaStringPtr::InitDefault()
  382. {
  383. tagged_ptr_ = TaggedStringPtr(&fixed_address_empty_string);
  384. }
  385. inline void ArenaStringPtr::InitExternal(const std::string* str)
  386. {
  387. tagged_ptr_.SetDefault(str);
  388. }
  389. inline void ArenaStringPtr::InitAllocated(std::string* str, Arena* arena)
  390. {
  391. if (arena != nullptr)
  392. {
  393. tagged_ptr_.SetMutableArena(str);
  394. arena->Own(str);
  395. }
  396. else
  397. {
  398. tagged_ptr_.SetAllocated(str);
  399. }
  400. }
  401. inline void ArenaStringPtr::Set(const char* s, Arena* arena)
  402. {
  403. Set(ConstStringParam{s}, arena);
  404. }
  405. inline void ArenaStringPtr::Set(const char* s, size_t n, Arena* arena)
  406. {
  407. Set(ConstStringParam{s, n}, arena);
  408. }
  409. inline void ArenaStringPtr::SetBytes(ConstStringParam value, Arena* arena)
  410. {
  411. Set(value, arena);
  412. }
  413. inline void ArenaStringPtr::SetBytes(std::string&& value, Arena* arena)
  414. {
  415. Set(std::move(value), arena);
  416. }
  417. inline void ArenaStringPtr::SetBytes(const char* s, Arena* arena)
  418. {
  419. Set(s, arena);
  420. }
  421. inline void ArenaStringPtr::SetBytes(const void* p, size_t n, Arena* arena)
  422. {
  423. Set(ConstStringParam{static_cast<const char*>(p), n}, arena);
  424. }
  425. // Make sure rhs_arena allocated rhs, and lhs_arena allocated lhs.
  426. inline PROTOBUF_NDEBUG_INLINE void ArenaStringPtr::InternalSwap( //
  427. ArenaStringPtr* rhs,
  428. Arena* rhs_arena, //
  429. ArenaStringPtr* lhs,
  430. Arena* lhs_arena
  431. )
  432. {
  433. // Silence unused variable warnings in release buildls.
  434. (void)rhs_arena;
  435. (void)lhs_arena;
  436. std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
  437. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  438. auto force_realloc = [](ArenaStringPtr* p, Arena* arena)
  439. {
  440. if (p->IsDefault())
  441. return;
  442. std::string* old_value = p->tagged_ptr_.Get();
  443. std::string* new_value =
  444. p->IsFixedSizeArena() ? Arena::Create<std::string>(arena, *old_value) : Arena::Create<std::string>(arena, std::move(*old_value));
  445. if (arena == nullptr)
  446. {
  447. delete old_value;
  448. p->tagged_ptr_.SetAllocated(new_value);
  449. }
  450. else
  451. {
  452. p->tagged_ptr_.SetMutableArena(new_value);
  453. }
  454. };
  455. // Because, at this point, tagged_ptr_ has been swapped, arena should also be
  456. // swapped.
  457. force_realloc(lhs, rhs_arena);
  458. force_realloc(rhs, lhs_arena);
  459. #endif // PROTOBUF_FORCE_COPY_IN_SWAP
  460. }
  461. inline void ArenaStringPtr::ClearNonDefaultToEmpty()
  462. {
  463. // Unconditionally mask away the tag.
  464. tagged_ptr_.Get()->clear();
  465. }
  466. inline std::string* ArenaStringPtr::UnsafeMutablePointer()
  467. {
  468. GOOGLE_DCHECK(tagged_ptr_.IsMutable());
  469. GOOGLE_DCHECK(tagged_ptr_.Get() != nullptr);
  470. return tagged_ptr_.Get();
  471. }
  472. } // namespace internal
  473. } // namespace protobuf
  474. } // namespace google
  475. #include <google/protobuf/port_undef.inc>
  476. #endif // GOOGLE_PROTOBUF_ARENASTRING_H__