arenastring.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 <string>
  33. #include <type_traits>
  34. #include <utility>
  35. #include <google/protobuf/stubs/logging.h>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/arena.h>
  38. #include <google/protobuf/port.h>
  39. #include <google/protobuf/port_def.inc>
  40. #ifdef SWIG
  41. #error "You cannot SWIG proto headers"
  42. #endif
  43. namespace google {
  44. namespace protobuf {
  45. namespace internal {
  46. template <typename T>
  47. class ExplicitlyConstructed;
  48. class SwapFieldHelper;
  49. // Lazy string instance to support string fields with non-empty default.
  50. // These are initialized on the first call to .get().
  51. class PROTOBUF_EXPORT LazyString {
  52. public:
  53. // We explicitly make LazyString an aggregate so that MSVC can do constant
  54. // initialization on it without marking it `constexpr`.
  55. // We do not want to use `constexpr` because it makes it harder to have extern
  56. // storage for it and causes library bloat.
  57. struct InitValue {
  58. const char* ptr;
  59. size_t size;
  60. };
  61. // We keep a union of the initialization value and the std::string to save on
  62. // space. We don't need the string array after Init() is done.
  63. union {
  64. mutable InitValue init_value_;
  65. alignas(std::string) mutable char string_buf_[sizeof(std::string)];
  66. };
  67. mutable std::atomic<const std::string*> inited_;
  68. const std::string& get() const {
  69. // This check generates less code than a call-once invocation.
  70. auto* res = inited_.load(std::memory_order_acquire);
  71. if (PROTOBUF_PREDICT_FALSE(res == nullptr)) return Init();
  72. return *res;
  73. }
  74. private:
  75. // Initialize the string in `string_buf_`, update `inited_` and return it.
  76. // We return it here to avoid having to read it again in the inlined code.
  77. const std::string& Init() const;
  78. };
  79. template <typename T>
  80. class TaggedPtr {
  81. public:
  82. TaggedPtr() = default;
  83. explicit constexpr TaggedPtr(const ExplicitlyConstructed<std::string>* ptr)
  84. : ptr_(const_cast<ExplicitlyConstructed<std::string>*>(ptr)) {}
  85. void SetTagged(T* p) {
  86. Set(p);
  87. ptr_ = reinterpret_cast<void*>(as_int() | 1);
  88. }
  89. void Set(T* p) { ptr_ = p; }
  90. T* Get() const { return reinterpret_cast<T*>(as_int() & -2); }
  91. bool IsTagged() const { return as_int() & 1; }
  92. // Returned value is only safe to dereference if IsTagged() == false.
  93. // It is safe to compare.
  94. T* UnsafeGet() const { return static_cast<T*>(ptr_); }
  95. bool IsNull() { return ptr_ == nullptr; }
  96. private:
  97. uintptr_t as_int() const { return reinterpret_cast<uintptr_t>(ptr_); }
  98. void* ptr_;
  99. };
  100. static_assert(std::is_trivial<TaggedPtr<std::string>>::value,
  101. "TaggedPtr must be trivial");
  102. // This class encapsulates a pointer to a std::string with or without a donated
  103. // buffer, tagged by bottom bit. It is a high-level wrapper that almost directly
  104. // corresponds to the interface required by string fields in generated
  105. // code. It replaces the old std::string* pointer in such cases.
  106. //
  107. // The object has different but similar code paths for when the default value is
  108. // the empty string and when it is a non-empty string.
  109. // The empty string is handled different throughout the library and there is a
  110. // single global instance of it we can share.
  111. //
  112. // For fields with an empty string default value, there are three distinct
  113. // states:
  114. //
  115. // - Pointer set to 'String' tag (LSB is 0), equal to
  116. // &GetEmptyStringAlreadyInited(): field is set to its default value. Points
  117. // to a true std::string*, but we do not own that std::string* (it's a
  118. // globally shared instance).
  119. //
  120. // - Pointer set to 'String' tag (LSB is 0), but not equal to the global empty
  121. // string: field points to a true std::string* instance that we own. This
  122. // instance is either on the heap or on the arena (i.e. registered on
  123. // free()/destructor-call list) as appropriate.
  124. //
  125. // - Pointer set to 'DonatedString' tag (LSB is 1): points to a std::string
  126. // instance with a buffer on the arena (arena != NULL, always, in this case).
  127. //
  128. // For fields with a non-empty string default value, there are three distinct
  129. // states:
  130. //
  131. // - Pointer set to 'String' tag (LSB is 0), equal to `nullptr`:
  132. // Field is in "default" mode and does not point to any actual instance.
  133. // Methods that might need to create an instance of the object will pass a
  134. // `const LazyString&` for it.
  135. //
  136. // - Pointer set to 'String' tag (LSB is 0), but not equal to `nullptr`:
  137. // field points to a true std::string* instance that we own. This instance is
  138. // either on the heap or on the arena (i.e. registered on
  139. // free()/destructor-call list) as appropriate.
  140. //
  141. // - Pointer set to 'DonatedString' tag (LSB is 1): points to a std::string
  142. // instance with a buffer on the arena (arena != NULL, always, in this case).
  143. //
  144. // Generated code and reflection code both ensure that ptr_ is never null for
  145. // fields with an empty default.
  146. // Because ArenaStringPtr is used in oneof unions, its constructor is a NOP and
  147. // so the field is always manually initialized via method calls.
  148. //
  149. // Side-note: why pass information about the default on every API call? Because
  150. // we don't want to hold it in a member variable, or else this would go into
  151. // every proto message instance. This would be a huge waste of space, since the
  152. // default instance pointer is typically a global (static class field). We want
  153. // the generated code to be as efficient as possible, and if we take
  154. // the default value information as a parameter that's in practice taken from a
  155. // static class field, and compare ptr_ to the default value, we end up with a
  156. // single "cmp %reg, GLOBAL" in the resulting machine code. (Note that this also
  157. // requires the String tag to be 0 so we can avoid the mask before comparing.)
  158. struct PROTOBUF_EXPORT ArenaStringPtr {
  159. ArenaStringPtr() = default;
  160. explicit constexpr ArenaStringPtr(
  161. const ExplicitlyConstructed<std::string>* default_value)
  162. : tagged_ptr_(default_value) {}
  163. // Some methods below are overloaded on a `default_value` and on tags.
  164. // The tagged overloads help reduce code size in the callers in generated
  165. // code, while the `default_value` overloads are useful from reflection.
  166. // By-value empty struct arguments are elided in the ABI.
  167. struct EmptyDefault {};
  168. struct NonEmptyDefault {};
  169. void Set(const std::string* default_value, ConstStringParam value,
  170. ::google::protobuf::Arena* arena);
  171. void Set(const std::string* default_value, std::string&& value,
  172. ::google::protobuf::Arena* arena);
  173. void Set(EmptyDefault, ConstStringParam value, ::google::protobuf::Arena* arena);
  174. void Set(EmptyDefault, std::string&& value, ::google::protobuf::Arena* arena);
  175. void Set(NonEmptyDefault, ConstStringParam value, ::google::protobuf::Arena* arena);
  176. void Set(NonEmptyDefault, std::string&& value, ::google::protobuf::Arena* arena);
  177. template <typename FirstParam>
  178. void Set(FirstParam p1, const char* str, ::google::protobuf::Arena* arena) {
  179. Set(p1, ConstStringParam(str), arena);
  180. }
  181. template <typename FirstParam>
  182. void Set(FirstParam p1, const char* str, size_t size,
  183. ::google::protobuf::Arena* arena) {
  184. ConstStringParam sp{str, size}; // for string_view and `const string &`
  185. Set(p1, sp, arena);
  186. }
  187. template <typename FirstParam, typename RefWrappedType>
  188. void Set(FirstParam p1,
  189. std::reference_wrapper<RefWrappedType> const_string_ref,
  190. ::google::protobuf::Arena* arena) {
  191. Set(p1, const_string_ref.get(), arena);
  192. }
  193. template <typename FirstParam, typename SecondParam>
  194. void SetBytes(FirstParam p1, SecondParam&& p2, ::google::protobuf::Arena* arena) {
  195. Set(p1, static_cast<SecondParam&&>(p2), arena);
  196. }
  197. template <typename FirstParam>
  198. void SetBytes(FirstParam p1, const void* str, size_t size,
  199. ::google::protobuf::Arena* arena) {
  200. // must work whether ConstStringParam is string_view or `const string &`
  201. ConstStringParam sp{static_cast<const char*>(str), size};
  202. Set(p1, sp, arena);
  203. }
  204. // Basic accessors.
  205. PROTOBUF_NDEBUG_INLINE const std::string& Get() const {
  206. // Unconditionally mask away the tag.
  207. return *tagged_ptr_.Get();
  208. }
  209. PROTOBUF_NDEBUG_INLINE const std::string* GetPointer() const {
  210. // Unconditionally mask away the tag.
  211. return tagged_ptr_.Get();
  212. }
  213. // For fields with an empty default value.
  214. std::string* Mutable(EmptyDefault, ::google::protobuf::Arena* arena);
  215. // For fields with a non-empty default value.
  216. std::string* Mutable(const LazyString& default_value, ::google::protobuf::Arena* arena);
  217. // Release returns a std::string* instance that is heap-allocated and is not
  218. // Own()'d by any arena. If the field is not set, this returns NULL. The
  219. // caller retains ownership. Clears this field back to NULL state. Used to
  220. // implement release_<field>() methods on generated classes.
  221. PROTOBUF_MUST_USE_RESULT std::string* Release(
  222. const std::string* default_value, ::google::protobuf::Arena* arena);
  223. PROTOBUF_MUST_USE_RESULT std::string* ReleaseNonDefault(
  224. const std::string* default_value, ::google::protobuf::Arena* arena);
  225. // Takes a std::string that is heap-allocated, and takes ownership. The
  226. // std::string's destructor is registered with the arena. Used to implement
  227. // set_allocated_<field> in generated classes.
  228. void SetAllocated(const std::string* default_value, std::string* value,
  229. ::google::protobuf::Arena* arena);
  230. // Swaps internal pointers. Arena-safety semantics: this is guarded by the
  231. // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is
  232. // 'unsafe' if called directly.
  233. inline PROTOBUF_NDEBUG_INLINE static void InternalSwap(
  234. const std::string* default_value, ArenaStringPtr* rhs, Arena* rhs_arena,
  235. ArenaStringPtr* lhs, Arena* lhs_arena);
  236. // Frees storage (if not on an arena).
  237. void Destroy(const std::string* default_value, ::google::protobuf::Arena* arena);
  238. void Destroy(EmptyDefault, ::google::protobuf::Arena* arena);
  239. void Destroy(NonEmptyDefault, ::google::protobuf::Arena* arena);
  240. // Clears content, but keeps allocated std::string, to avoid the overhead of
  241. // heap operations. After this returns, the content (as seen by the user) will
  242. // always be the empty std::string. Assumes that |default_value| is an empty
  243. // std::string.
  244. void ClearToEmpty();
  245. // Clears content, assuming that the current value is not the empty
  246. // string default.
  247. void ClearNonDefaultToEmpty();
  248. // Clears content, but keeps allocated std::string if arena != NULL, to avoid
  249. // the overhead of heap operations. After this returns, the content (as seen
  250. // by the user) will always be equal to |default_value|.
  251. void ClearToDefault(const LazyString& default_value, ::google::protobuf::Arena* arena);
  252. // Called from generated code / reflection runtime only. Resets value to point
  253. // to a default string pointer, with the semantics that this
  254. // ArenaStringPtr does not own the pointed-to memory. Disregards initial value
  255. // of ptr_ (so this is the *ONLY* safe method to call after construction or
  256. // when reinitializing after becoming the active field in a oneof union).
  257. inline void UnsafeSetDefault(const std::string* default_value);
  258. // Returns a mutable pointer, but doesn't initialize the string to the
  259. // default value.
  260. std::string* MutableNoArenaNoDefault(const std::string* default_value);
  261. // Get a mutable pointer with unspecified contents.
  262. // Similar to `MutableNoArenaNoDefault`, but also handles the arena case.
  263. // If the value was donated, the contents are discarded.
  264. std::string* MutableNoCopy(const std::string* default_value,
  265. ::google::protobuf::Arena* arena);
  266. // Destroy the string. Assumes `arena == nullptr`.
  267. void DestroyNoArena(const std::string* default_value);
  268. // Internal setter used only at parse time to directly set a donated string
  269. // value.
  270. void UnsafeSetTaggedPointer(TaggedPtr<std::string> value) {
  271. tagged_ptr_ = value;
  272. }
  273. // Generated code only! An optimization, in certain cases the generated
  274. // code is certain we can obtain a std::string with no default checks and
  275. // tag tests.
  276. std::string* UnsafeMutablePointer() PROTOBUF_RETURNS_NONNULL;
  277. inline bool IsDefault(const std::string* default_value) const {
  278. // Relies on the fact that kPtrTagString == 0, so if IsString(), ptr_ is the
  279. // actual std::string pointer (and if !IsString(), ptr_ will never be equal
  280. // to any aligned |default_value| pointer). The key is that we want to avoid
  281. // masking in the fastpath const-pointer Get() case for non-arena code.
  282. return tagged_ptr_.UnsafeGet() == default_value;
  283. }
  284. private:
  285. TaggedPtr<std::string> tagged_ptr_;
  286. bool IsDonatedString() const { return false; }
  287. // Swaps tagged pointer without debug hardening. This is to allow python
  288. // protobuf to maintain pointer stability even in DEBUG builds.
  289. inline PROTOBUF_NDEBUG_INLINE static void UnsafeShallowSwap(
  290. ArenaStringPtr* rhs, ArenaStringPtr* lhs) {
  291. std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
  292. }
  293. friend class ::google::protobuf::internal::SwapFieldHelper;
  294. // Slow paths.
  295. // MutableSlow requires that !IsString() || IsDefault
  296. // Variadic to support 0 args for EmptyDefault and 1 arg for LazyString.
  297. template <typename... Lazy>
  298. std::string* MutableSlow(::google::protobuf::Arena* arena, const Lazy&... lazy_default);
  299. // Sets value to a newly allocated string and returns it
  300. std::string* SetAndReturnNewString();
  301. // Destroys the non-default string value out-of-line
  302. void DestroyNoArenaSlowPath();
  303. };
  304. inline void ArenaStringPtr::UnsafeSetDefault(const std::string* value) {
  305. tagged_ptr_.Set(const_cast<std::string*>(value));
  306. }
  307. // Make sure rhs_arena allocated rhs, and lhs_arena allocated lhs.
  308. inline PROTOBUF_NDEBUG_INLINE void ArenaStringPtr::InternalSwap( //
  309. const std::string* default_value, //
  310. ArenaStringPtr* rhs, Arena* rhs_arena, //
  311. ArenaStringPtr* lhs, Arena* lhs_arena) {
  312. // Silence unused variable warnings in release buildls.
  313. (void)default_value;
  314. (void)rhs_arena;
  315. (void)lhs_arena;
  316. std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
  317. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  318. auto force_realloc = [default_value](ArenaStringPtr* p, Arena* arena) {
  319. if (p->IsDefault(default_value)) return;
  320. std::string* old_value = p->tagged_ptr_.Get();
  321. std::string* new_value =
  322. p->IsDonatedString()
  323. ? Arena::Create<std::string>(arena, *old_value)
  324. : Arena::Create<std::string>(arena, std::move(*old_value));
  325. if (arena == nullptr) delete old_value;
  326. p->tagged_ptr_.Set(new_value);
  327. };
  328. // Because, at this point, tagged_ptr_ has been swapped, arena should also be
  329. // swapped.
  330. force_realloc(lhs, rhs_arena);
  331. force_realloc(rhs, lhs_arena);
  332. #endif // PROTOBUF_FORCE_COPY_IN_SWAP
  333. }
  334. inline void ArenaStringPtr::ClearNonDefaultToEmpty() {
  335. // Unconditionally mask away the tag.
  336. tagged_ptr_.Get()->clear();
  337. }
  338. inline std::string* ArenaStringPtr::MutableNoArenaNoDefault(
  339. const std::string* default_value) {
  340. // VERY IMPORTANT for performance and code size: this will reduce to a member
  341. // variable load, a pointer check (against |default_value|, in practice a
  342. // static global) and a branch to the slowpath (which calls operator new and
  343. // the ctor). DO NOT add any tagged-pointer operations here.
  344. if (IsDefault(default_value)) {
  345. return SetAndReturnNewString();
  346. } else {
  347. return UnsafeMutablePointer();
  348. }
  349. }
  350. inline void ArenaStringPtr::DestroyNoArena(const std::string* default_value) {
  351. if (!IsDefault(default_value)) {
  352. DestroyNoArenaSlowPath();
  353. }
  354. }
  355. inline std::string* ArenaStringPtr::UnsafeMutablePointer() {
  356. GOOGLE_DCHECK(!tagged_ptr_.IsTagged());
  357. GOOGLE_DCHECK(tagged_ptr_.UnsafeGet() != nullptr);
  358. return tagged_ptr_.UnsafeGet();
  359. }
  360. } // namespace internal
  361. } // namespace protobuf
  362. } // namespace google
  363. #include <google/protobuf/port_undef.inc>
  364. #endif // GOOGLE_PROTOBUF_ARENASTRING_H__