arenastring.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #include "google/protobuf/arenastring.h"
  31. #include <cstddef>
  32. #include "absl/log/absl_check.h"
  33. #include "absl/strings/string_view.h"
  34. #include "absl/synchronization/mutex.h"
  35. #include "google/protobuf/io/coded_stream.h"
  36. #include "google/protobuf/message_lite.h"
  37. #include "google/protobuf/parse_context.h"
  38. // clang-format off
  39. #include "google/protobuf/port_def.inc"
  40. // clang-format on
  41. namespace google {
  42. namespace protobuf {
  43. namespace internal {
  44. namespace {
  45. // TaggedStringPtr::Flags uses the lower 2 bits as tags.
  46. // Enforce that allocated data aligns to at least 4 bytes, and that
  47. // the alignment of the global const string value does as well.
  48. // The alignment guaranteed by `new std::string` depends on both:
  49. // - new align = __STDCPP_DEFAULT_NEW_ALIGNMENT__ / max_align_t
  50. // - alignof(std::string)
  51. #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
  52. constexpr size_t kNewAlign = __STDCPP_DEFAULT_NEW_ALIGNMENT__;
  53. #elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900
  54. constexpr size_t kNewAlign = alignof(::max_align_t);
  55. #else
  56. constexpr size_t kNewAlign = alignof(std::max_align_t);
  57. #endif
  58. constexpr size_t kStringAlign = alignof(std::string);
  59. static_assert((kStringAlign > kNewAlign ? kStringAlign : kNewAlign) >= 4, "");
  60. static_assert(alignof(ExplicitlyConstructedArenaString) >= 4, "");
  61. } // namespace
  62. const std::string& LazyString::Init() const {
  63. static absl::Mutex mu{absl::kConstInit};
  64. mu.Lock();
  65. const std::string* res = inited_.load(std::memory_order_acquire);
  66. if (res == nullptr) {
  67. auto init_value = init_value_;
  68. res = ::new (static_cast<void*>(string_buf_))
  69. std::string(init_value.ptr, init_value.size);
  70. inited_.store(res, std::memory_order_release);
  71. }
  72. mu.Unlock();
  73. return *res;
  74. }
  75. namespace {
  76. #if defined(NDEBUG) || !defined(GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL)
  77. class ScopedCheckPtrInvariants {
  78. public:
  79. explicit ScopedCheckPtrInvariants(const TaggedStringPtr*) {}
  80. };
  81. #endif // NDEBUG || !GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL
  82. // Creates a heap allocated std::string value.
  83. inline TaggedStringPtr CreateString(absl::string_view value) {
  84. TaggedStringPtr res;
  85. res.SetAllocated(new std::string(value.data(), value.length()));
  86. return res;
  87. }
  88. #ifndef GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL
  89. // Creates an arena allocated std::string value.
  90. TaggedStringPtr CreateArenaString(Arena& arena, absl::string_view s) {
  91. TaggedStringPtr res;
  92. res.SetMutableArena(Arena::Create<std::string>(&arena, s.data(), s.length()));
  93. return res;
  94. }
  95. #endif // !GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL
  96. } // namespace
  97. void ArenaStringPtr::Set(absl::string_view value, Arena* arena) {
  98. ScopedCheckPtrInvariants check(&tagged_ptr_);
  99. if (IsDefault()) {
  100. // If we're not on an arena, skip straight to a true string to avoid
  101. // possible copy cost later.
  102. tagged_ptr_ = arena != nullptr ? CreateArenaString(*arena, value)
  103. : CreateString(value);
  104. } else {
  105. #ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
  106. if (arena == nullptr) {
  107. auto* old = tagged_ptr_.GetIfAllocated();
  108. tagged_ptr_ = CreateString(value);
  109. delete old;
  110. } else {
  111. auto* old = UnsafeMutablePointer();
  112. tagged_ptr_ = CreateArenaString(*arena, value);
  113. old->assign("garbagedata");
  114. }
  115. #else // PROTOBUF_FORCE_COPY_DEFAULT_STRING
  116. UnsafeMutablePointer()->assign(value.data(), value.length());
  117. #endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
  118. }
  119. }
  120. template <>
  121. void ArenaStringPtr::Set(const std::string& value, Arena* arena) {
  122. ScopedCheckPtrInvariants check(&tagged_ptr_);
  123. if (IsDefault()) {
  124. // If we're not on an arena, skip straight to a true string to avoid
  125. // possible copy cost later.
  126. tagged_ptr_ = arena != nullptr ? CreateArenaString(*arena, value)
  127. : CreateString(value);
  128. } else {
  129. #ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
  130. if (arena == nullptr) {
  131. auto* old = tagged_ptr_.GetIfAllocated();
  132. tagged_ptr_ = CreateString(value);
  133. delete old;
  134. } else {
  135. auto* old = UnsafeMutablePointer();
  136. tagged_ptr_ = CreateArenaString(*arena, value);
  137. old->assign("garbagedata");
  138. }
  139. #else // PROTOBUF_FORCE_COPY_DEFAULT_STRING
  140. UnsafeMutablePointer()->assign(value);
  141. #endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
  142. }
  143. }
  144. void ArenaStringPtr::Set(std::string&& value, Arena* arena) {
  145. ScopedCheckPtrInvariants check(&tagged_ptr_);
  146. if (IsDefault()) {
  147. NewString(arena, std::move(value));
  148. } else if (IsFixedSizeArena()) {
  149. std::string* current = tagged_ptr_.Get();
  150. auto* s = new (current) std::string(std::move(value));
  151. arena->OwnDestructor(s);
  152. tagged_ptr_.SetMutableArena(s);
  153. } else /* !IsFixedSizeArena() */ {
  154. *UnsafeMutablePointer() = std::move(value);
  155. }
  156. }
  157. std::string* ArenaStringPtr::Mutable(Arena* arena) {
  158. ScopedCheckPtrInvariants check(&tagged_ptr_);
  159. if (tagged_ptr_.IsMutable()) {
  160. return tagged_ptr_.Get();
  161. } else {
  162. return MutableSlow(arena);
  163. }
  164. }
  165. std::string* ArenaStringPtr::Mutable(const LazyString& default_value,
  166. Arena* arena) {
  167. ScopedCheckPtrInvariants check(&tagged_ptr_);
  168. if (tagged_ptr_.IsMutable()) {
  169. return tagged_ptr_.Get();
  170. } else {
  171. return MutableSlow(arena, default_value);
  172. }
  173. }
  174. std::string* ArenaStringPtr::MutableNoCopy(Arena* arena) {
  175. ScopedCheckPtrInvariants check(&tagged_ptr_);
  176. if (tagged_ptr_.IsMutable()) {
  177. return tagged_ptr_.Get();
  178. } else {
  179. ABSL_DCHECK(IsDefault());
  180. // Allocate empty. The contents are not relevant.
  181. return NewString(arena);
  182. }
  183. }
  184. template <typename... Lazy>
  185. std::string* ArenaStringPtr::MutableSlow(::google::protobuf::Arena* arena,
  186. const Lazy&... lazy_default) {
  187. ABSL_DCHECK(IsDefault());
  188. // For empty defaults, this ends up calling the default constructor which is
  189. // more efficient than a copy construction from
  190. // GetEmptyStringAlreadyInited().
  191. return NewString(arena, lazy_default.get()...);
  192. }
  193. std::string* ArenaStringPtr::Release() {
  194. ScopedCheckPtrInvariants check(&tagged_ptr_);
  195. if (IsDefault()) return nullptr;
  196. std::string* released = tagged_ptr_.Get();
  197. if (tagged_ptr_.IsArena()) {
  198. released = tagged_ptr_.IsMutable() ? new std::string(std::move(*released))
  199. : new std::string(*released);
  200. }
  201. InitDefault();
  202. return released;
  203. }
  204. void ArenaStringPtr::SetAllocated(std::string* value, Arena* arena) {
  205. ScopedCheckPtrInvariants check(&tagged_ptr_);
  206. // Release what we have first.
  207. Destroy();
  208. if (value == nullptr) {
  209. InitDefault();
  210. } else {
  211. #ifndef NDEBUG
  212. // On debug builds, copy the string so the address differs. delete will
  213. // fail if value was a stack-allocated temporary/etc., which would have
  214. // failed when arena ran its cleanup list.
  215. std::string* new_value = new std::string(std::move(*value));
  216. delete value;
  217. value = new_value;
  218. #endif // !NDEBUG
  219. InitAllocated(value, arena);
  220. }
  221. }
  222. void ArenaStringPtr::Destroy() {
  223. delete tagged_ptr_.GetIfAllocated();
  224. }
  225. void ArenaStringPtr::ClearToEmpty() {
  226. ScopedCheckPtrInvariants check(&tagged_ptr_);
  227. if (IsDefault()) {
  228. // Already set to default -- do nothing.
  229. } else {
  230. // Unconditionally mask away the tag.
  231. //
  232. // UpdateArenaString uses assign when capacity is larger than the new
  233. // value, which is trivially true in the donated string case.
  234. // const_cast<std::string*>(PtrValue<std::string>())->clear();
  235. tagged_ptr_.Get()->clear();
  236. }
  237. }
  238. void ArenaStringPtr::ClearToDefault(const LazyString& default_value,
  239. ::google::protobuf::Arena* arena) {
  240. ScopedCheckPtrInvariants check(&tagged_ptr_);
  241. (void)arena;
  242. if (IsDefault()) {
  243. // Already set to default -- do nothing.
  244. } else {
  245. UnsafeMutablePointer()->assign(default_value.get());
  246. }
  247. }
  248. const char* EpsCopyInputStream::ReadArenaString(const char* ptr,
  249. ArenaStringPtr* s,
  250. Arena* arena) {
  251. ScopedCheckPtrInvariants check(&s->tagged_ptr_);
  252. ABSL_DCHECK(arena != nullptr);
  253. int size = ReadSize(&ptr);
  254. if (!ptr) return nullptr;
  255. auto* str = s->NewString(arena);
  256. ptr = ReadString(ptr, size, str);
  257. GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
  258. return ptr;
  259. }
  260. } // namespace internal
  261. } // namespace protobuf
  262. } // namespace google
  263. #include "google/protobuf/port_undef.inc"