arena_impl.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. #ifdef ADDRESS_SANITIZER
  39. #include <sanitizer/asan_interface.h>
  40. #endif // ADDRESS_SANITIZER
  41. #include <google/protobuf/port_def.inc>
  42. namespace google {
  43. namespace protobuf {
  44. namespace internal {
  45. inline constexpr size_t AlignUpTo8(size_t n) {
  46. // Align n to next multiple of 8 (from Hacker's Delight, Chapter 3.)
  47. return (n + 7) & static_cast<size_t>(-8);
  48. }
  49. using LifecycleIdAtomic = uint64_t;
  50. // MetricsCollector collects stats for a particular arena.
  51. class PROTOBUF_EXPORT ArenaMetricsCollector {
  52. public:
  53. ArenaMetricsCollector(bool record_allocs) : record_allocs_(record_allocs) {}
  54. // Invoked when the arena is about to be destroyed. This method will
  55. // typically finalize any metric collection and delete the collector.
  56. // space_allocated is the space used by the arena.
  57. virtual void OnDestroy(uint64_t space_allocated) = 0;
  58. // OnReset() is called when the associated arena is reset.
  59. // space_allocated is the space used by the arena just before the reset.
  60. virtual void OnReset(uint64_t space_allocated) = 0;
  61. // OnAlloc is called when an allocation happens.
  62. // type_info is promised to be static - its lifetime extends to
  63. // match program's lifetime (It is given by typeid operator).
  64. // Note: typeid(void) will be passed as allocated_type every time we
  65. // intentionally want to avoid monitoring an allocation. (i.e. internal
  66. // allocations for managing the arena)
  67. virtual void OnAlloc(const std::type_info* allocated_type,
  68. uint64_t alloc_size) = 0;
  69. // Does OnAlloc() need to be called? If false, metric collection overhead
  70. // will be reduced since we will not do extra work per allocation.
  71. bool RecordAllocs() { return record_allocs_; }
  72. protected:
  73. // This class is destructed by the call to OnDestroy().
  74. ~ArenaMetricsCollector() = default;
  75. const bool record_allocs_;
  76. };
  77. struct AllocationPolicy {
  78. static constexpr size_t kDefaultStartBlockSize = 256;
  79. static constexpr size_t kDefaultMaxBlockSize = 8192;
  80. size_t start_block_size = kDefaultStartBlockSize;
  81. size_t max_block_size = kDefaultMaxBlockSize;
  82. void* (*block_alloc)(size_t) = nullptr;
  83. void (*block_dealloc)(void*, size_t) = nullptr;
  84. ArenaMetricsCollector* metrics_collector = nullptr;
  85. bool IsDefault() const {
  86. return start_block_size == kDefaultMaxBlockSize &&
  87. max_block_size == kDefaultMaxBlockSize && block_alloc == nullptr &&
  88. block_dealloc == nullptr && metrics_collector == nullptr;
  89. }
  90. };
  91. // Tagged pointer to an AllocationPolicy.
  92. class TaggedAllocationPolicyPtr {
  93. public:
  94. constexpr TaggedAllocationPolicyPtr() : policy_(0) {}
  95. explicit TaggedAllocationPolicyPtr(AllocationPolicy* policy)
  96. : policy_(reinterpret_cast<uintptr_t>(policy)) {}
  97. void set_policy(AllocationPolicy* policy) {
  98. auto bits = policy_ & kTagsMask;
  99. policy_ = reinterpret_cast<uintptr_t>(policy) | bits;
  100. }
  101. AllocationPolicy* get() {
  102. return reinterpret_cast<AllocationPolicy*>(policy_ & kPtrMask);
  103. }
  104. const AllocationPolicy* get() const {
  105. return reinterpret_cast<const AllocationPolicy*>(policy_ & kPtrMask);
  106. }
  107. AllocationPolicy& operator*() { return *get(); }
  108. const AllocationPolicy& operator*() const { return *get(); }
  109. AllocationPolicy* operator->() { return get(); }
  110. const AllocationPolicy* operator->() const { return get(); }
  111. bool is_user_owned_initial_block() const {
  112. return static_cast<bool>(get_mask<kUserOwnedInitialBlock>());
  113. }
  114. void set_is_user_owned_initial_block(bool v) {
  115. set_mask<kUserOwnedInitialBlock>(v);
  116. }
  117. bool should_record_allocs() const {
  118. return static_cast<bool>(get_mask<kRecordAllocs>());
  119. }
  120. void set_should_record_allocs(bool v) { set_mask<kRecordAllocs>(v); }
  121. uintptr_t get_raw() const { return policy_; }
  122. inline void RecordAlloc(const std::type_info* allocated_type,
  123. size_t n) const {
  124. get()->metrics_collector->OnAlloc(allocated_type, n);
  125. }
  126. private:
  127. enum : uintptr_t {
  128. kUserOwnedInitialBlock = 1,
  129. kRecordAllocs = 2,
  130. };
  131. static constexpr uintptr_t kTagsMask = 7;
  132. static constexpr uintptr_t kPtrMask = ~kTagsMask;
  133. template <uintptr_t kMask>
  134. uintptr_t get_mask() const {
  135. return policy_ & kMask;
  136. }
  137. template <uintptr_t kMask>
  138. void set_mask(bool v) {
  139. if (v) {
  140. policy_ |= kMask;
  141. } else {
  142. policy_ &= ~kMask;
  143. }
  144. }
  145. uintptr_t policy_;
  146. };
  147. // A simple arena allocator. Calls to allocate functions must be properly
  148. // serialized by the caller, hence this class cannot be used as a general
  149. // purpose allocator in a multi-threaded program. It serves as a building block
  150. // for ThreadSafeArena, which provides a thread-safe arena allocator.
  151. //
  152. // This class manages
  153. // 1) Arena bump allocation + owning memory blocks.
  154. // 2) Maintaining a cleanup list.
  155. // It delagetes the actual memory allocation back to ThreadSafeArena, which
  156. // contains the information on block growth policy and backing memory allocation
  157. // used.
  158. class PROTOBUF_EXPORT SerialArena {
  159. public:
  160. struct Memory {
  161. void* ptr;
  162. size_t size;
  163. };
  164. // Node contains the ptr of the object to be cleaned up and the associated
  165. // cleanup function ptr.
  166. struct CleanupNode {
  167. void* elem; // Pointer to the object to be cleaned up.
  168. void (*cleanup)(void*); // Function pointer to the destructor or deleter.
  169. };
  170. void CleanupList();
  171. uint64_t SpaceAllocated() const {
  172. return space_allocated_.load(std::memory_order_relaxed);
  173. }
  174. uint64_t SpaceUsed() const;
  175. bool HasSpace(size_t n) { return n <= static_cast<size_t>(limit_ - ptr_); }
  176. void* AllocateAligned(size_t n, const AllocationPolicy* policy) {
  177. GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned.
  178. GOOGLE_DCHECK_GE(limit_, ptr_);
  179. if (PROTOBUF_PREDICT_FALSE(!HasSpace(n))) {
  180. return AllocateAlignedFallback(n, policy);
  181. }
  182. return AllocateFromExisting(n);
  183. }
  184. private:
  185. void* AllocateFromExisting(size_t n) {
  186. void* ret = ptr_;
  187. ptr_ += n;
  188. #ifdef ADDRESS_SANITIZER
  189. ASAN_UNPOISON_MEMORY_REGION(ret, n);
  190. #endif // ADDRESS_SANITIZER
  191. return ret;
  192. }
  193. public:
  194. // Allocate space if the current region provides enough space.
  195. bool MaybeAllocateAligned(size_t n, void** out) {
  196. GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned.
  197. GOOGLE_DCHECK_GE(limit_, ptr_);
  198. if (PROTOBUF_PREDICT_FALSE(!HasSpace(n))) return false;
  199. *out = AllocateFromExisting(n);
  200. return true;
  201. }
  202. std::pair<void*, CleanupNode*> AllocateAlignedWithCleanup(
  203. size_t n, const AllocationPolicy* policy) {
  204. GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned.
  205. if (PROTOBUF_PREDICT_FALSE(!HasSpace(n + kCleanupSize))) {
  206. return AllocateAlignedWithCleanupFallback(n, policy);
  207. }
  208. return AllocateFromExistingWithCleanupFallback(n);
  209. }
  210. private:
  211. std::pair<void*, CleanupNode*> AllocateFromExistingWithCleanupFallback(
  212. size_t n) {
  213. void* ret = ptr_;
  214. ptr_ += n;
  215. limit_ -= kCleanupSize;
  216. #ifdef ADDRESS_SANITIZER
  217. ASAN_UNPOISON_MEMORY_REGION(ret, n);
  218. ASAN_UNPOISON_MEMORY_REGION(limit_, kCleanupSize);
  219. #endif // ADDRESS_SANITIZER
  220. return CreatePair(ret, reinterpret_cast<CleanupNode*>(limit_));
  221. }
  222. public:
  223. void AddCleanup(void* elem, void (*cleanup)(void*),
  224. const AllocationPolicy* policy) {
  225. auto res = AllocateAlignedWithCleanup(0, policy);
  226. res.second->elem = elem;
  227. res.second->cleanup = cleanup;
  228. }
  229. void* owner() const { return owner_; }
  230. SerialArena* next() const { return next_; }
  231. void set_next(SerialArena* next) { next_ = next; }
  232. private:
  233. friend class ThreadSafeArena;
  234. friend class ArenaBenchmark;
  235. // Creates a new SerialArena inside mem using the remaining memory as for
  236. // future allocations.
  237. static SerialArena* New(SerialArena::Memory mem, void* owner);
  238. // Free SerialArena returning the memory passed in to New
  239. template <typename Deallocator>
  240. Memory Free(Deallocator deallocator);
  241. // Blocks are variable length malloc-ed objects. The following structure
  242. // describes the common header for all blocks.
  243. struct Block {
  244. Block(Block* next, size_t size) : next(next), size(size), start(nullptr) {}
  245. char* Pointer(size_t n) {
  246. GOOGLE_DCHECK(n <= size);
  247. return reinterpret_cast<char*>(this) + n;
  248. }
  249. Block* const next;
  250. const size_t size;
  251. CleanupNode* start;
  252. // data follows
  253. };
  254. void* owner_; // &ThreadCache of this thread;
  255. Block* head_; // Head of linked list of blocks.
  256. SerialArena* next_; // Next SerialArena in this linked list.
  257. size_t space_used_ = 0; // Necessary for metrics.
  258. std::atomic<size_t> space_allocated_;
  259. // Next pointer to allocate from. Always 8-byte aligned. Points inside
  260. // head_ (and head_->pos will always be non-canonical). We keep these
  261. // here to reduce indirection.
  262. char* ptr_;
  263. char* limit_;
  264. // Constructor is private as only New() should be used.
  265. inline SerialArena(Block* b, void* owner);
  266. void* AllocateAlignedFallback(size_t n, const AllocationPolicy* policy);
  267. std::pair<void*, CleanupNode*> AllocateAlignedWithCleanupFallback(
  268. size_t n, const AllocationPolicy* policy);
  269. void AllocateNewBlock(size_t n, const AllocationPolicy* policy);
  270. std::pair<void*, CleanupNode*> CreatePair(void* ptr, CleanupNode* node) {
  271. return {ptr, node};
  272. }
  273. public:
  274. static constexpr size_t kBlockHeaderSize = AlignUpTo8(sizeof(Block));
  275. static constexpr size_t kCleanupSize = AlignUpTo8(sizeof(CleanupNode));
  276. };
  277. // Tag type used to invoke the constructor of message-owned arena.
  278. // Only message-owned arenas use this constructor for creation.
  279. // Such constructors are internal implementation details of the library.
  280. struct MessageOwned {
  281. explicit MessageOwned() = default;
  282. };
  283. // This class provides the core Arena memory allocation library. Different
  284. // implementations only need to implement the public interface below.
  285. // Arena is not a template type as that would only be useful if all protos
  286. // in turn would be templates, which will/cannot happen. However separating
  287. // the memory allocation part from the cruft of the API users expect we can
  288. // use #ifdef the select the best implementation based on hardware / OS.
  289. class PROTOBUF_EXPORT ThreadSafeArena {
  290. public:
  291. ThreadSafeArena() { Init(); }
  292. // Constructor solely used by message-owned arena.
  293. ThreadSafeArena(internal::MessageOwned) : tag_and_id_(kMessageOwnedArena) {
  294. Init();
  295. }
  296. ThreadSafeArena(char* mem, size_t size) { InitializeFrom(mem, size); }
  297. explicit ThreadSafeArena(void* mem, size_t size,
  298. const AllocationPolicy& policy) {
  299. InitializeWithPolicy(mem, size, policy);
  300. }
  301. // Destructor deletes all owned heap allocated objects, and destructs objects
  302. // that have non-trivial destructors, except for proto2 message objects whose
  303. // destructors can be skipped. Also, frees all blocks except the initial block
  304. // if it was passed in.
  305. ~ThreadSafeArena();
  306. uint64_t Reset();
  307. uint64_t SpaceAllocated() const;
  308. uint64_t SpaceUsed() const;
  309. void* AllocateAligned(size_t n, const std::type_info* type) {
  310. SerialArena* arena;
  311. if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() &&
  312. GetSerialArenaFast(&arena))) {
  313. return arena->AllocateAligned(n, AllocPolicy());
  314. } else {
  315. return AllocateAlignedFallback(n, type);
  316. }
  317. }
  318. // This function allocates n bytes if the common happy case is true and
  319. // returns true. Otherwise does nothing and returns false. This strange
  320. // semantics is necessary to allow callers to program functions that only
  321. // have fallback function calls in tail position. This substantially improves
  322. // code for the happy path.
  323. PROTOBUF_NDEBUG_INLINE bool MaybeAllocateAligned(size_t n, void** out) {
  324. SerialArena* a;
  325. if (PROTOBUF_PREDICT_TRUE(!alloc_policy_.should_record_allocs() &&
  326. GetSerialArenaFromThreadCache(&a))) {
  327. return a->MaybeAllocateAligned(n, out);
  328. }
  329. return false;
  330. }
  331. std::pair<void*, SerialArena::CleanupNode*> AllocateAlignedWithCleanup(
  332. size_t n, const std::type_info* type);
  333. // Add object pointer and cleanup function pointer to the list.
  334. void AddCleanup(void* elem, void (*cleanup)(void*));
  335. // Checks whether this arena is message-owned.
  336. PROTOBUF_ALWAYS_INLINE bool IsMessageOwned() const {
  337. return tag_and_id_ & kMessageOwnedArena;
  338. }
  339. private:
  340. // Unique for each arena. Changes on Reset().
  341. uint64_t tag_and_id_ = 0;
  342. // The LSB of tag_and_id_ indicates if the arena is message-owned.
  343. enum : uint64_t { kMessageOwnedArena = 1 };
  344. TaggedAllocationPolicyPtr alloc_policy_; // Tagged pointer to AllocPolicy.
  345. // Pointer to a linked list of SerialArena.
  346. std::atomic<SerialArena*> threads_;
  347. std::atomic<SerialArena*> hint_; // Fast thread-local block access
  348. const AllocationPolicy* AllocPolicy() const { return alloc_policy_.get(); }
  349. void InitializeFrom(void* mem, size_t size);
  350. void InitializeWithPolicy(void* mem, size_t size, AllocationPolicy policy);
  351. void* AllocateAlignedFallback(size_t n, const std::type_info* type);
  352. std::pair<void*, SerialArena::CleanupNode*>
  353. AllocateAlignedWithCleanupFallback(size_t n, const std::type_info* type);
  354. void Init();
  355. void SetInitialBlock(void* mem, size_t size);
  356. // Delete or Destruct all objects owned by the arena.
  357. void CleanupList();
  358. inline uint64_t LifeCycleId() const {
  359. return tag_and_id_ & ~kMessageOwnedArena;
  360. }
  361. inline void CacheSerialArena(SerialArena* serial) {
  362. thread_cache().last_serial_arena = serial;
  363. thread_cache().last_lifecycle_id_seen = tag_and_id_;
  364. // TODO(haberman): evaluate whether we would gain efficiency by getting rid
  365. // of hint_. It's the only write we do to ThreadSafeArena in the allocation
  366. // path, which will dirty the cache line.
  367. hint_.store(serial, std::memory_order_release);
  368. }
  369. PROTOBUF_NDEBUG_INLINE bool GetSerialArenaFast(SerialArena** arena) {
  370. if (GetSerialArenaFromThreadCache(arena)) return true;
  371. // Check whether we own the last accessed SerialArena on this arena. This
  372. // fast path optimizes the case where a single thread uses multiple arenas.
  373. ThreadCache* tc = &thread_cache();
  374. SerialArena* serial = hint_.load(std::memory_order_acquire);
  375. if (PROTOBUF_PREDICT_TRUE(serial != NULL && serial->owner() == tc)) {
  376. *arena = serial;
  377. return true;
  378. }
  379. return false;
  380. }
  381. PROTOBUF_NDEBUG_INLINE bool GetSerialArenaFromThreadCache(
  382. SerialArena** arena) {
  383. // If this thread already owns a block in this arena then try to use that.
  384. // This fast path optimizes the case where multiple threads allocate from
  385. // the same arena.
  386. ThreadCache* tc = &thread_cache();
  387. if (PROTOBUF_PREDICT_TRUE(tc->last_lifecycle_id_seen == tag_and_id_)) {
  388. *arena = tc->last_serial_arena;
  389. return true;
  390. }
  391. return false;
  392. }
  393. SerialArena* GetSerialArenaFallback(void* me);
  394. template <typename Functor>
  395. void PerSerialArena(Functor fn) {
  396. // By omitting an Acquire barrier we ensure that any user code that doesn't
  397. // properly synchronize Reset() or the destructor will throw a TSAN warning.
  398. SerialArena* serial = threads_.load(std::memory_order_relaxed);
  399. for (; serial; serial = serial->next()) fn(serial);
  400. }
  401. // Releases all memory except the first block which it returns. The first
  402. // block might be owned by the user and thus need some extra checks before
  403. // deleting.
  404. SerialArena::Memory Free(size_t* space_allocated);
  405. #ifdef _MSC_VER
  406. #pragma warning(disable : 4324)
  407. #endif
  408. struct alignas(64) ThreadCache {
  409. #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
  410. // If we are using the ThreadLocalStorage class to store the ThreadCache,
  411. // then the ThreadCache's default constructor has to be responsible for
  412. // initializing it.
  413. ThreadCache()
  414. : next_lifecycle_id(0),
  415. last_lifecycle_id_seen(-1),
  416. last_serial_arena(NULL) {}
  417. #endif
  418. // Number of per-thread lifecycle IDs to reserve. Must be power of two.
  419. // To reduce contention on a global atomic, each thread reserves a batch of
  420. // IDs. The following number is calculated based on a stress test with
  421. // ~6500 threads all frequently allocating a new arena.
  422. static constexpr size_t kPerThreadIds = 256;
  423. // Next lifecycle ID available to this thread. We need to reserve a new
  424. // batch, if `next_lifecycle_id & (kPerThreadIds - 1) == 0`.
  425. uint64_t next_lifecycle_id;
  426. // The ThreadCache is considered valid as long as this matches the
  427. // lifecycle_id of the arena being used.
  428. uint64_t last_lifecycle_id_seen;
  429. SerialArena* last_serial_arena;
  430. };
  431. // Lifecycle_id can be highly contended variable in a situation of lots of
  432. // arena creation. Make sure that other global variables are not sharing the
  433. // cacheline.
  434. #ifdef _MSC_VER
  435. #pragma warning(disable : 4324)
  436. #endif
  437. struct alignas(64) CacheAlignedLifecycleIdGenerator {
  438. std::atomic<LifecycleIdAtomic> id;
  439. };
  440. static CacheAlignedLifecycleIdGenerator lifecycle_id_generator_;
  441. #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
  442. // iOS does not support __thread keyword so we use a custom thread local
  443. // storage class we implemented.
  444. static ThreadCache& thread_cache();
  445. #elif defined(PROTOBUF_USE_DLLS)
  446. // Thread local variables cannot be exposed through DLL interface but we can
  447. // wrap them in static functions.
  448. static ThreadCache& thread_cache();
  449. #else
  450. static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_;
  451. static ThreadCache& thread_cache() { return thread_cache_; }
  452. #endif
  453. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ThreadSafeArena);
  454. // All protos have pointers back to the arena hence Arena must have
  455. // pointer stability.
  456. ThreadSafeArena(ThreadSafeArena&&) = delete;
  457. ThreadSafeArena& operator=(ThreadSafeArena&&) = delete;
  458. public:
  459. // kBlockHeaderSize is sizeof(Block), aligned up to the nearest multiple of 8
  460. // to protect the invariant that pos is always at a multiple of 8.
  461. static constexpr size_t kBlockHeaderSize = SerialArena::kBlockHeaderSize;
  462. static constexpr size_t kSerialArenaSize =
  463. (sizeof(SerialArena) + 7) & static_cast<size_t>(-8);
  464. static_assert(kBlockHeaderSize % 8 == 0,
  465. "kBlockHeaderSize must be a multiple of 8.");
  466. static_assert(kSerialArenaSize % 8 == 0,
  467. "kSerialArenaSize must be a multiple of 8.");
  468. };
  469. } // namespace internal
  470. } // namespace protobuf
  471. } // namespace google
  472. #include <google/protobuf/port_undef.inc>
  473. #endif // GOOGLE_PROTOBUF_ARENA_IMPL_H__