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.

arena_impl.h 35 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. // This file defines an Arena allocator for better allocation performance.
  31. #ifndef GOOGLE_PROTOBUF_ARENA_IMPL_H__
  32. #define GOOGLE_PROTOBUF_ARENA_IMPL_H__
  33. #include <atomic>
  34. #include <limits>
  35. #include <typeinfo>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/stubs/logging.h>
  38. #include <google/protobuf/stubs/port.h>
  39. #ifdef ADDRESS_SANITIZER
  40. #include <sanitizer/asan_interface.h>
  41. #endif // ADDRESS_SANITIZER
  42. #include <google/protobuf/arenaz_sampler.h>
  43. // Must be included last.
  44. #include <google/protobuf/port_def.inc>
  45. namespace google
  46. {
  47. namespace protobuf
  48. {
  49. namespace internal
  50. {
  51. // To prevent sharing cache lines between threads
  52. #ifdef __cpp_aligned_new
  53. enum
  54. {
  55. kCacheAlignment = 64
  56. };
  57. #else
  58. enum
  59. {
  60. kCacheAlignment = alignof(max_align_t)
  61. }; // do the best we can
  62. #endif
  63. inline constexpr size_t AlignUpTo8(size_t n)
  64. {
  65. // Align n to next multiple of 8 (from Hacker's Delight, Chapter 3.)
  66. return (n + 7) & static_cast<size_t>(-8);
  67. }
  68. using LifecycleIdAtomic = uint64_t;
  69. // MetricsCollector collects stats for a particular arena.
  70. class PROTOBUF_EXPORT ArenaMetricsCollector
  71. {
  72. public:
  73. ArenaMetricsCollector(bool record_allocs) :
  74. record_allocs_(record_allocs)
  75. {
  76. }
  77. // Invoked when the arena is about to be destroyed. This method will
  78. // typically finalize any metric collection and delete the collector.
  79. // space_allocated is the space used by the arena.
  80. virtual void OnDestroy(uint64_t space_allocated) = 0;
  81. // OnReset() is called when the associated arena is reset.
  82. // space_allocated is the space used by the arena just before the reset.
  83. virtual void OnReset(uint64_t space_allocated) = 0;
  84. // OnAlloc is called when an allocation happens.
  85. // type_info is promised to be static - its lifetime extends to
  86. // match program's lifetime (It is given by typeid operator).
  87. // Note: typeid(void) will be passed as allocated_type every time we
  88. // intentionally want to avoid monitoring an allocation. (i.e. internal
  89. // allocations for managing the arena)
  90. virtual void OnAlloc(const std::type_info* allocated_type, uint64_t alloc_size) = 0;
  91. // Does OnAlloc() need to be called? If false, metric collection overhead
  92. // will be reduced since we will not do extra work per allocation.
  93. bool RecordAllocs()
  94. {
  95. return record_allocs_;
  96. }
  97. protected:
  98. // This class is destructed by the call to OnDestroy().
  99. ~ArenaMetricsCollector() = default;
  100. const bool record_allocs_;
  101. };
  102. struct AllocationPolicy
  103. {
  104. static constexpr size_t kDefaultStartBlockSize = 256;
  105. static constexpr size_t kDefaultMaxBlockSize = 8192;
  106. size_t start_block_size = kDefaultStartBlockSize;
  107. size_t max_block_size = kDefaultMaxBlockSize;
  108. void* (*block_alloc)(size_t) = nullptr;
  109. void (*block_dealloc)(void*, size_t) = nullptr;
  110. ArenaMetricsCollector* metrics_collector = nullptr;
  111. bool IsDefault() const
  112. {
  113. return start_block_size == kDefaultMaxBlockSize &&
  114. max_block_size == kDefaultMaxBlockSize && block_alloc == nullptr &&
  115. block_dealloc == nullptr && metrics_collector == nullptr;
  116. }
  117. };
  118. // Tagged pointer to an AllocationPolicy.
  119. class TaggedAllocationPolicyPtr
  120. {
  121. public:
  122. constexpr TaggedAllocationPolicyPtr() :
  123. policy_(0)
  124. {
  125. }
  126. explicit TaggedAllocationPolicyPtr(AllocationPolicy* policy) :
  127. policy_(reinterpret_cast<uintptr_t>(policy))
  128. {
  129. }
  130. void set_policy(AllocationPolicy* policy)
  131. {
  132. auto bits = policy_ & kTagsMask;
  133. policy_ = reinterpret_cast<uintptr_t>(policy) | bits;
  134. }
  135. AllocationPolicy* get()
  136. {
  137. return reinterpret_cast<AllocationPolicy*>(policy_ & kPtrMask);
  138. }
  139. const AllocationPolicy* get() const
  140. {
  141. return reinterpret_cast<const AllocationPolicy*>(policy_ & kPtrMask);
  142. }
  143. AllocationPolicy& operator*()
  144. {
  145. return *get();
  146. }
  147. const AllocationPolicy& operator*() const
  148. {
  149. return *get();
  150. }
  151. AllocationPolicy* operator->()
  152. {
  153. return get();
  154. }
  155. const AllocationPolicy* operator->() const
  156. {
  157. return get();
  158. }
  159. bool is_user_owned_initial_block() const
  160. {
  161. return static_cast<bool>(get_mask<kUserOwnedInitialBlock>());
  162. }
  163. void set_is_user_owned_initial_block(bool v)
  164. {
  165. set_mask<kUserOwnedInitialBlock>(v);
  166. }
  167. bool should_record_allocs() const
  168. {
  169. return static_cast<bool>(get_mask<kRecordAllocs>());
  170. }
  171. void set_should_record_allocs(bool v)
  172. {
  173. set_mask<kRecordAllocs>(v);
  174. }
  175. uintptr_t get_raw() const
  176. {
  177. return policy_;
  178. }
  179. inline void RecordAlloc(const std::type_info* allocated_type, size_t n) const
  180. {
  181. get()->metrics_collector->OnAlloc(allocated_type, n);
  182. }
  183. private:
  184. enum : uintptr_t
  185. {
  186. kUserOwnedInitialBlock = 1,
  187. kRecordAllocs = 2,
  188. };
  189. static constexpr uintptr_t kTagsMask = 7;
  190. static constexpr uintptr_t kPtrMask = ~kTagsMask;
  191. template<uintptr_t kMask>
  192. uintptr_t get_mask() const
  193. {
  194. return policy_ & kMask;
  195. }
  196. template<uintptr_t kMask>
  197. void set_mask(bool v)
  198. {
  199. if (v)
  200. {
  201. policy_ |= kMask;
  202. }
  203. else
  204. {
  205. policy_ &= ~kMask;
  206. }
  207. }
  208. uintptr_t policy_;
  209. };
  210. enum class AllocationClient
  211. {
  212. kDefault,
  213. kArray
  214. };
  215. // A simple arena allocator. Calls to allocate functions must be properly
  216. // serialized by the caller, hence this class cannot be used as a general
  217. // purpose allocator in a multi-threaded program. It serves as a building block
  218. // for ThreadSafeArena, which provides a thread-safe arena allocator.
  219. //
  220. // This class manages
  221. // 1) Arena bump allocation + owning memory blocks.
  222. // 2) Maintaining a cleanup list.
  223. // It delagetes the actual memory allocation back to ThreadSafeArena, which
  224. // contains the information on block growth policy and backing memory allocation
  225. // used.
  226. class PROTOBUF_EXPORT SerialArena
  227. {
  228. public:
  229. struct Memory
  230. {
  231. void* ptr;
  232. size_t size;
  233. };
  234. // Node contains the ptr of the object to be cleaned up and the associated
  235. // cleanup function ptr.
  236. struct CleanupNode
  237. {
  238. void* elem; // Pointer to the object to be cleaned up.
  239. void (*cleanup)(void*); // Function pointer to the destructor or deleter.
  240. };
  241. void CleanupList();
  242. uint64_t SpaceAllocated() const
  243. {
  244. return space_allocated_.load(std::memory_order_relaxed);
  245. }
  246. uint64_t SpaceUsed() const;
  247. bool HasSpace(size_t n) const
  248. {
  249. return n <= static_cast<size_t>(limit_ - ptr_);
  250. }
  251. // See comments on `cached_blocks_` member for details.
  252. PROTOBUF_ALWAYS_INLINE void* TryAllocateFromCachedBlock(size_t size)
  253. {
  254. if (PROTOBUF_PREDICT_FALSE(size < 16))
  255. return nullptr;
  256. // We round up to the next larger block in case the memory doesn't match
  257. // the pattern we are looking for.
  258. const size_t index = Bits::Log2FloorNonZero64(size - 1) - 3;
  259. if (index >= cached_block_length_)
  260. return nullptr;
  261. auto& cached_head = cached_blocks_[index];
  262. if (cached_head == nullptr)
  263. return nullptr;
  264. void* ret = cached_head;
  265. #ifdef ADDRESS_SANITIZER
  266. ASAN_UNPOISON_MEMORY_REGION(ret, size);
  267. #endif // ADDRESS_SANITIZER
  268. cached_head = cached_head->next;
  269. return ret;
  270. }
  271. // In kArray mode we look through cached blocks.
  272. // We do not do this by default because most non-array allocations will not
  273. // have the right size and will fail to find an appropriate cached block.
  274. //
  275. // TODO(sbenza): Evaluate if we should use cached blocks for message types of
  276. // the right size. We can statically know if the allocation size can benefit
  277. // from it.
  278. template<AllocationClient alloc_client = AllocationClient::kDefault>
  279. void* AllocateAligned(size_t n, const AllocationPolicy* policy)
  280. {
  281. GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned.
  282. GOOGLE_DCHECK_GE(limit_, ptr_);
  283. if (alloc_client == AllocationClient::kArray)
  284. {
  285. if (void* res = TryAllocateFromCachedBlock(n))
  286. {
  287. return res;
  288. }
  289. }
  290. if (PROTOBUF_PREDICT_FALSE(!HasSpace(n)))
  291. {
  292. return AllocateAlignedFallback(n, policy);
  293. }
  294. return AllocateFromExisting(n);
  295. }
  296. private:
  297. void* AllocateFromExisting(size_t n)
  298. {
  299. void* ret = ptr_;
  300. ptr_ += n;
  301. #ifdef ADDRESS_SANITIZER
  302. ASAN_UNPOISON_MEMORY_REGION(ret, n);
  303. #endif // ADDRESS_SANITIZER
  304. return ret;
  305. }
  306. // See comments on `cached_blocks_` member for details.
  307. void ReturnArrayMemory(void* p, size_t size)
  308. {
  309. // We only need to check for 32-bit platforms.
  310. // In 64-bit platforms the minimum allocation size from Repeated*Field will
  311. // be 16 guaranteed.
  312. if (sizeof(void*) < 8)
  313. {
  314. if (PROTOBUF_PREDICT_FALSE(size < 16))
  315. return;
  316. }
  317. else
  318. {
  319. GOOGLE_DCHECK(size >= 16);
  320. }
  321. // We round down to the next smaller block in case the memory doesn't match
  322. // the pattern we are looking for. eg, someone might have called Reserve()
  323. // on the repeated field.
  324. const size_t index = Bits::Log2FloorNonZero64(size) - 4;
  325. if (PROTOBUF_PREDICT_FALSE(index >= cached_block_length_))
  326. {
  327. // We can't put this object on the freelist so make this object the
  328. // freelist. It is guaranteed it is larger than the one we have, and
  329. // large enough to hold another allocation of `size`.
  330. CachedBlock** new_list = static_cast<CachedBlock**>(p);
  331. size_t new_size = size / sizeof(CachedBlock*);
  332. std::copy(cached_blocks_, cached_blocks_ + cached_block_length_, new_list);
  333. std::fill(new_list + cached_block_length_, new_list + new_size, nullptr);
  334. cached_blocks_ = new_list;
  335. // Make the size fit in uint8_t. This is the power of two, so we don't
  336. // need anything larger.
  337. cached_block_length_ =
  338. static_cast<uint8_t>(std::min(size_t{64}, new_size));
  339. return;
  340. }
  341. auto& cached_head = cached_blocks_[index];
  342. auto* new_node = static_cast<CachedBlock*>(p);
  343. new_node->next = cached_head;
  344. cached_head = new_node;
  345. #ifdef ADDRESS_SANITIZER
  346. ASAN_POISON_MEMORY_REGION(p, size);
  347. #endif // ADDRESS_SANITIZER
  348. }
  349. public:
  350. // Allocate space if the current region provides enough space.
  351. bool MaybeAllocateAligned(size_t n, void** out)
  352. {
  353. GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned.
  354. GOOGLE_DCHECK_GE(limit_, ptr_);
  355. if (PROTOBUF_PREDICT_FALSE(!HasSpace(n)))
  356. return false;
  357. *out = AllocateFromExisting(n);
  358. return true;
  359. }
  360. std::pair<void*, CleanupNode*> AllocateAlignedWithCleanup(
  361. size_t n, const AllocationPolicy* policy
  362. )
  363. {
  364. GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned.
  365. if (PROTOBUF_PREDICT_FALSE(!HasSpace(n + kCleanupSize)))
  366. {
  367. return AllocateAlignedWithCleanupFallback(n, policy);
  368. }
  369. return AllocateFromExistingWithCleanupFallback(n);
  370. }
  371. private:
  372. std::pair<void*, CleanupNode*> AllocateFromExistingWithCleanupFallback(
  373. size_t n
  374. )
  375. {
  376. void* ret = ptr_;
  377. ptr_ += n;
  378. limit_ -= kCleanupSize;
  379. #ifdef ADDRESS_SANITIZER
  380. ASAN_UNPOISON_MEMORY_REGION(ret, n);
  381. ASAN_UNPOISON_MEMORY_REGION(limit_, kCleanupSize);
  382. #endif // ADDRESS_SANITIZER
  383. return CreatePair(ret, reinterpret_cast<CleanupNode*>(limit_));
  384. }
  385. public:
  386. void AddCleanup(void* elem, void (*cleanup)(void*), const AllocationPolicy* policy)
  387. {
  388. auto res = AllocateAlignedWithCleanup(0, policy);
  389. res.second->elem = elem;
  390. res.second->cleanup = cleanup;
  391. }
  392. void* owner() const
  393. {
  394. return owner_;
  395. }
  396. SerialArena* next() const
  397. {
  398. return next_;
  399. }
  400. void set_next(SerialArena* next)
  401. {
  402. next_ = next;
  403. }
  404. private:
  405. friend class ThreadSafeArena;
  406. friend class ArenaBenchmark;
  407. // Creates a new SerialArena inside mem using the remaining memory as for
  408. // future allocations.
  409. static SerialArena* New(SerialArena::Memory mem, void* owner, ThreadSafeArenaStats* stats);
  410. // Free SerialArena returning the memory passed in to New
  411. template<typename Deallocator>
  412. Memory Free(Deallocator deallocator);
  413. // Blocks are variable length malloc-ed objects. The following structure
  414. // describes the common header for all blocks.
  415. struct Block
  416. {
  417. Block(Block* next, size_t size) :
  418. next(next),
  419. size(size),
  420. start(nullptr)
  421. {
  422. }
  423. char* Pointer(size_t n)
  424. {
  425. GOOGLE_DCHECK(n <= size);
  426. return reinterpret_cast<char*>(this) + n;
  427. }
  428. Block* const next;
  429. const size_t size;
  430. CleanupNode* start;
  431. // data follows
  432. };
  433. void* owner_; // &ThreadCache of this thread;
  434. Block* head_; // Head of linked list of blocks.
  435. SerialArena* next_; // Next SerialArena in this linked list.
  436. size_t space_used_ = 0; // Necessary for metrics.
  437. std::atomic<size_t> space_allocated_;
  438. // Next pointer to allocate from. Always 8-byte aligned. Points inside
  439. // head_ (and head_->pos will always be non-canonical). We keep these
  440. // here to reduce indirection.
  441. char* ptr_;
  442. // Limiting address up to which memory can be allocated from the head block.
  443. char* limit_;
  444. // For holding sampling information. The pointer is owned by the
  445. // ThreadSafeArena that holds this serial arena.
  446. ThreadSafeArenaStats* arena_stats_;
  447. // Repeated*Field and Arena play together to reduce memory consumption by
  448. // reusing blocks. Currently, natural growth of the repeated field types makes
  449. // them allocate blocks of size `8 + 2^N, N>=3`.
  450. // When the repeated field grows returns the previous block and we put it in
  451. // this free list.
  452. // `cached_blocks_[i]` points to the free list for blocks of size `8+2^(i+3)`.
  453. // The array of freelists is grown when needed in `ReturnArrayMemory()`.
  454. struct CachedBlock
  455. {
  456. // Simple linked list.
  457. CachedBlock* next;
  458. };
  459. uint8_t cached_block_length_ = 0;
  460. CachedBlock** cached_blocks_ = nullptr;
  461. // Constructor is private as only New() should be used.
  462. inline SerialArena(Block* b, void* owner, ThreadSafeArenaStats* stats);
  463. void* AllocateAlignedFallback(size_t n, const AllocationPolicy* policy);
  464. std::pair<void*, CleanupNode*> AllocateAlignedWithCleanupFallback(
  465. size_t n, const AllocationPolicy* policy
  466. );
  467. void AllocateNewBlock(size_t n, const AllocationPolicy* policy);
  468. std::pair<void*, CleanupNode*> CreatePair(void* ptr, CleanupNode* node)
  469. {
  470. return {ptr, node};
  471. }
  472. public:
  473. static constexpr size_t kBlockHeaderSize = AlignUpTo8(sizeof(Block));
  474. static constexpr size_t kCleanupSize = AlignUpTo8(sizeof(CleanupNode));
  475. };
  476. // Tag type used to invoke the constructor of message-owned arena.
  477. // Only message-owned arenas use this constructor for creation.
  478. // Such constructors are internal implementation details of the library.
  479. struct MessageOwned
  480. {
  481. explicit MessageOwned() = default;
  482. };
  483. // This class provides the core Arena memory allocation library. Different
  484. // implementations only need to implement the public interface below.
  485. // Arena is not a template type as that would only be useful if all protos
  486. // in turn would be templates, which will/cannot happen. However separating
  487. // the memory allocation part from the cruft of the API users expect we can
  488. // use #ifdef the select the best implementation based on hardware / OS.
  489. class PROTOBUF_EXPORT ThreadSafeArena
  490. {
  491. public:
  492. ThreadSafeArena()
  493. {
  494. Init();
  495. }
  496. // Constructor solely used by message-owned arena.
  497. ThreadSafeArena(internal::MessageOwned) :
  498. tag_and_id_(kMessageOwnedArena)
  499. {
  500. Init();
  501. }
  502. ThreadSafeArena(char* mem, size_t size)
  503. {
  504. InitializeFrom(mem, size);
  505. }
  506. explicit ThreadSafeArena(void* mem, size_t size, const AllocationPolicy& policy)
  507. {
  508. InitializeWithPolicy(mem, size, policy);
  509. }
  510. // Destructor deletes all owned heap allocated objects, and destructs objects
  511. // that have non-trivial destructors, except for proto2 message objects whose
  512. // destructors can be skipped. Also, frees all blocks except the initial block
  513. // if it was passed in.
  514. ~ThreadSafeArena();
  515. uint64_t Reset();
  516. uint64_t SpaceAllocated() const;
  517. uint64_t SpaceUsed() const;
  518. template<AllocationClient alloc_client = AllocationClient::kDefault>
  519. void* AllocateAligned(size_t n, const std::type_info* type)
  520. {
  521. SerialArena* arena;
  522. if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && GetSerialArenaFast(&arena)))
  523. {
  524. return arena->AllocateAligned<alloc_client>(n, AllocPolicy());
  525. }
  526. else
  527. {
  528. return AllocateAlignedFallback(n, type);
  529. }
  530. }
  531. void ReturnArrayMemory(void* p, size_t size)
  532. {
  533. SerialArena* arena;
  534. if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena)))
  535. {
  536. arena->ReturnArrayMemory(p, size);
  537. }
  538. }
  539. // This function allocates n bytes if the common happy case is true and
  540. // returns true. Otherwise does nothing and returns false. This strange
  541. // semantics is necessary to allow callers to program functions that only
  542. // have fallback function calls in tail position. This substantially improves
  543. // code for the happy path.
  544. PROTOBUF_NDEBUG_INLINE bool MaybeAllocateAligned(size_t n, void** out)
  545. {
  546. SerialArena* arena;
  547. if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() && GetSerialArenaFromThreadCache(&arena)))
  548. {
  549. return arena->MaybeAllocateAligned(n, out);
  550. }
  551. return false;
  552. }
  553. std::pair<void*, SerialArena::CleanupNode*> AllocateAlignedWithCleanup(
  554. size_t n, const std::type_info* type
  555. );
  556. // Add object pointer and cleanup function pointer to the list.
  557. void AddCleanup(void* elem, void (*cleanup)(void*));
  558. // Checks whether this arena is message-owned.
  559. PROTOBUF_ALWAYS_INLINE bool IsMessageOwned() const
  560. {
  561. return tag_and_id_ & kMessageOwnedArena;
  562. }
  563. private:
  564. // Unique for each arena. Changes on Reset().
  565. uint64_t tag_and_id_ = 0;
  566. // The LSB of tag_and_id_ indicates if the arena is message-owned.
  567. enum : uint64_t
  568. {
  569. kMessageOwnedArena = 1
  570. };
  571. TaggedAllocationPolicyPtr alloc_policy_; // Tagged pointer to AllocPolicy.
  572. static_assert(std::is_trivially_destructible<SerialArena>{}, "SerialArena needs to be trivially destructible.");
  573. // Pointer to a linked list of SerialArena.
  574. std::atomic<SerialArena*> threads_;
  575. std::atomic<SerialArena*> hint_; // Fast thread-local block access
  576. const AllocationPolicy* AllocPolicy() const
  577. {
  578. return alloc_policy_.get();
  579. }
  580. void InitializeFrom(void* mem, size_t size);
  581. void InitializeWithPolicy(void* mem, size_t size, AllocationPolicy policy);
  582. void* AllocateAlignedFallback(size_t n, const std::type_info* type);
  583. std::pair<void*, SerialArena::CleanupNode*>
  584. AllocateAlignedWithCleanupFallback(size_t n, const std::type_info* type);
  585. void Init();
  586. void SetInitialBlock(void* mem, size_t size);
  587. // Delete or Destruct all objects owned by the arena.
  588. void CleanupList();
  589. inline uint64_t LifeCycleId() const
  590. {
  591. return tag_and_id_ & ~kMessageOwnedArena;
  592. }
  593. inline void CacheSerialArena(SerialArena* serial)
  594. {
  595. thread_cache().last_serial_arena = serial;
  596. thread_cache().last_lifecycle_id_seen = tag_and_id_;
  597. // TODO(haberman): evaluate whether we would gain efficiency by getting rid
  598. // of hint_. It's the only write we do to ThreadSafeArena in the allocation
  599. // path, which will dirty the cache line.
  600. hint_.store(serial, std::memory_order_release);
  601. }
  602. PROTOBUF_NDEBUG_INLINE bool GetSerialArenaFast(SerialArena** arena)
  603. {
  604. if (GetSerialArenaFromThreadCache(arena))
  605. return true;
  606. // Check whether we own the last accessed SerialArena on this arena. This
  607. // fast path optimizes the case where a single thread uses multiple arenas.
  608. ThreadCache* tc = &thread_cache();
  609. SerialArena* serial = hint_.load(std::memory_order_acquire);
  610. if (PROTOBUF_PREDICT_TRUE(serial != nullptr && serial->owner() == tc))
  611. {
  612. *arena = serial;
  613. return true;
  614. }
  615. return false;
  616. }
  617. PROTOBUF_NDEBUG_INLINE bool GetSerialArenaFromThreadCache(
  618. SerialArena** arena
  619. )
  620. {
  621. // If this thread already owns a block in this arena then try to use that.
  622. // This fast path optimizes the case where multiple threads allocate from
  623. // the same arena.
  624. ThreadCache* tc = &thread_cache();
  625. if (PROTOBUF_PREDICT_TRUE(tc->last_lifecycle_id_seen == tag_and_id_))
  626. {
  627. *arena = tc->last_serial_arena;
  628. return true;
  629. }
  630. return false;
  631. }
  632. SerialArena* GetSerialArenaFallback(void* me);
  633. template<typename Functor>
  634. void PerSerialArena(Functor fn)
  635. {
  636. // By omitting an Acquire barrier we ensure that any user code that doesn't
  637. // properly synchronize Reset() or the destructor will throw a TSAN warning.
  638. SerialArena* serial = threads_.load(std::memory_order_relaxed);
  639. for (; serial; serial = serial->next())
  640. fn(serial);
  641. }
  642. // Releases all memory except the first block which it returns. The first
  643. // block might be owned by the user and thus need some extra checks before
  644. // deleting.
  645. SerialArena::Memory Free(size_t* space_allocated);
  646. #ifdef _MSC_VER
  647. #pragma warning(disable : 4324)
  648. #endif
  649. struct alignas(kCacheAlignment) ThreadCache
  650. {
  651. #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
  652. // If we are using the ThreadLocalStorage class to store the ThreadCache,
  653. // then the ThreadCache's default constructor has to be responsible for
  654. // initializing it.
  655. ThreadCache() :
  656. next_lifecycle_id(0),
  657. last_lifecycle_id_seen(-1),
  658. last_serial_arena(nullptr)
  659. {
  660. }
  661. #endif
  662. // Number of per-thread lifecycle IDs to reserve. Must be power of two.
  663. // To reduce contention on a global atomic, each thread reserves a batch of
  664. // IDs. The following number is calculated based on a stress test with
  665. // ~6500 threads all frequently allocating a new arena.
  666. static constexpr size_t kPerThreadIds = 256;
  667. // Next lifecycle ID available to this thread. We need to reserve a new
  668. // batch, if `next_lifecycle_id & (kPerThreadIds - 1) == 0`.
  669. uint64_t next_lifecycle_id;
  670. // The ThreadCache is considered valid as long as this matches the
  671. // lifecycle_id of the arena being used.
  672. uint64_t last_lifecycle_id_seen;
  673. SerialArena* last_serial_arena;
  674. };
  675. // Lifecycle_id can be highly contended variable in a situation of lots of
  676. // arena creation. Make sure that other global variables are not sharing the
  677. // cacheline.
  678. #ifdef _MSC_VER
  679. #pragma warning(disable : 4324)
  680. #endif
  681. struct alignas(kCacheAlignment) CacheAlignedLifecycleIdGenerator
  682. {
  683. std::atomic<LifecycleIdAtomic> id;
  684. };
  685. static CacheAlignedLifecycleIdGenerator lifecycle_id_generator_;
  686. #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
  687. // iOS does not support __thread keyword so we use a custom thread local
  688. // storage class we implemented.
  689. static ThreadCache& thread_cache();
  690. #elif defined(PROTOBUF_USE_DLLS)
  691. // Thread local variables cannot be exposed through DLL interface but we can
  692. // wrap them in static functions.
  693. static ThreadCache& thread_cache();
  694. #else
  695. static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_;
  696. static ThreadCache& thread_cache()
  697. {
  698. return thread_cache_;
  699. }
  700. #endif
  701. ThreadSafeArenaStatsHandle arena_stats_;
  702. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadSafeArena);
  703. // All protos have pointers back to the arena hence Arena must have
  704. // pointer stability.
  705. ThreadSafeArena(ThreadSafeArena&&) = delete;
  706. ThreadSafeArena& operator=(ThreadSafeArena&&) = delete;
  707. public:
  708. // kBlockHeaderSize is sizeof(Block), aligned up to the nearest multiple of 8
  709. // to protect the invariant that pos is always at a multiple of 8.
  710. static constexpr size_t kBlockHeaderSize = SerialArena::kBlockHeaderSize;
  711. static constexpr size_t kSerialArenaSize =
  712. (sizeof(SerialArena) + 7) & static_cast<size_t>(-8);
  713. static_assert(kBlockHeaderSize % 8 == 0, "kBlockHeaderSize must be a multiple of 8.");
  714. static_assert(kSerialArenaSize % 8 == 0, "kSerialArenaSize must be a multiple of 8.");
  715. };
  716. } // namespace internal
  717. } // namespace protobuf
  718. } // namespace google
  719. #include <google/protobuf/port_undef.inc>
  720. #endif // GOOGLE_PROTOBUF_ARENA_IMPL_H__