Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:26

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2022 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 //
0008 // This file defines the internal class SerialArena
0009 
0010 #ifndef GOOGLE_PROTOBUF_SERIAL_ARENA_H__
0011 #define GOOGLE_PROTOBUF_SERIAL_ARENA_H__
0012 
0013 #include <algorithm>
0014 #include <atomic>
0015 #include <cstddef>
0016 #include <cstdint>
0017 #include <string>
0018 #include <vector>
0019 
0020 #include "absl/base/attributes.h"
0021 #include "absl/base/optimization.h"
0022 #include "absl/base/prefetch.h"
0023 #include "absl/log/absl_check.h"
0024 #include "absl/numeric/bits.h"
0025 #include "google/protobuf/arena_align.h"
0026 #include "google/protobuf/arena_cleanup.h"
0027 #include "google/protobuf/port.h"
0028 #include "google/protobuf/string_block.h"
0029 
0030 // Must be included last.
0031 #include "google/protobuf/port_def.inc"
0032 
0033 namespace google {
0034 namespace protobuf {
0035 namespace internal {
0036 
0037 // Arena blocks are variable length malloc-ed objects.  The following structure
0038 // describes the common header for all blocks.
0039 struct ArenaBlock {
0040   // For the sentry block with zero-size where ptr_/limit_ both point to `this`.
0041   constexpr ArenaBlock() : next(nullptr), size(0) {}
0042 
0043   ArenaBlock(ArenaBlock* next, size_t size) : next(next), size(size) {
0044     ABSL_DCHECK_GT(size, sizeof(ArenaBlock));
0045   }
0046 
0047   char* Pointer(size_t n) {
0048     ABSL_DCHECK_LE(n, size);
0049     return reinterpret_cast<char*>(this) + n;
0050   }
0051   char* Limit() { return Pointer(size & static_cast<size_t>(-8)); }
0052 
0053   bool IsSentry() const { return size == 0; }
0054 
0055   ArenaBlock* const next;
0056   const size_t size;
0057   // data follows
0058 };
0059 
0060 enum class AllocationClient { kDefault, kArray };
0061 
0062 class ThreadSafeArena;
0063 
0064 // Tag type used to invoke the constructor of the first SerialArena.
0065 struct FirstSerialArena {
0066   explicit FirstSerialArena() = default;
0067 };
0068 
0069 // A simple arena allocator. Calls to allocate functions must be properly
0070 // serialized by the caller, hence this class cannot be used as a general
0071 // purpose allocator in a multi-threaded program. It serves as a building block
0072 // for ThreadSafeArena, which provides a thread-safe arena allocator.
0073 //
0074 // This class manages
0075 // 1) Arena bump allocation + owning memory blocks.
0076 // 2) Maintaining a cleanup list.
0077 // It delegates the actual memory allocation back to ThreadSafeArena, which
0078 // contains the information on block growth policy and backing memory allocation
0079 // used.
0080 class PROTOBUF_EXPORT SerialArena {
0081  public:
0082   static constexpr size_t kBlockHeaderSize =
0083       ArenaAlignDefault::Ceil(sizeof(ArenaBlock));
0084 
0085   void CleanupList() { cleanup_list_.Cleanup(*this); }
0086   uint64_t SpaceAllocated() const {
0087     return space_allocated_.load(std::memory_order_relaxed);
0088   }
0089   uint64_t SpaceUsed() const;
0090 
0091   // See comments on `cached_blocks_` member for details.
0092   PROTOBUF_ALWAYS_INLINE void* TryAllocateFromCachedBlock(size_t size) {
0093     if (PROTOBUF_PREDICT_FALSE(size < 16)) return nullptr;
0094     // We round up to the next larger block in case the memory doesn't match
0095     // the pattern we are looking for.
0096     const size_t index = absl::bit_width(size - 1) - 4;
0097 
0098     if (PROTOBUF_PREDICT_FALSE(index >= cached_block_length_)) return nullptr;
0099     auto& cached_head = cached_blocks_[index];
0100     if (cached_head == nullptr) return nullptr;
0101 
0102     void* ret = cached_head;
0103     PROTOBUF_UNPOISON_MEMORY_REGION(ret, size);
0104     cached_head = cached_head->next;
0105     return ret;
0106   }
0107 
0108   // In kArray mode we look through cached blocks.
0109   // We do not do this by default because most non-array allocations will not
0110   // have the right size and will fail to find an appropriate cached block.
0111   //
0112   // TODO: Evaluate if we should use cached blocks for message types of
0113   // the right size. We can statically know if the allocation size can benefit
0114   // from it.
0115   template <AllocationClient alloc_client = AllocationClient::kDefault>
0116   void* AllocateAligned(size_t n) {
0117     ABSL_DCHECK(internal::ArenaAlignDefault::IsAligned(n));
0118     ABSL_DCHECK_GE(limit_, ptr());
0119 
0120     if (alloc_client == AllocationClient::kArray) {
0121       if (void* res = TryAllocateFromCachedBlock(n)) {
0122         return res;
0123       }
0124     }
0125 
0126     void* ptr;
0127     if (PROTOBUF_PREDICT_TRUE(MaybeAllocateAligned(n, &ptr))) {
0128       return ptr;
0129     }
0130     return AllocateAlignedFallback(n);
0131   }
0132 
0133  private:
0134   static inline PROTOBUF_ALWAYS_INLINE constexpr size_t AlignUpTo(size_t n,
0135                                                                   size_t a) {
0136     // We are wasting space by over allocating align - 8 bytes. Compared to a
0137     // dedicated function that takes current alignment in consideration.  Such a
0138     // scheme would only waste (align - 8)/2 bytes on average, but requires a
0139     // dedicated function in the outline arena allocation functions. Possibly
0140     // re-evaluate tradeoffs later.
0141     return a <= 8 ? ArenaAlignDefault::Ceil(n) : ArenaAlignAs(a).Padded(n);
0142   }
0143 
0144   static inline PROTOBUF_ALWAYS_INLINE void* AlignTo(void* p, size_t a) {
0145     return (a <= ArenaAlignDefault::align)
0146                ? ArenaAlignDefault::CeilDefaultAligned(p)
0147                : ArenaAlignAs(a).CeilDefaultAligned(p);
0148   }
0149 
0150   // See comments on `cached_blocks_` member for details.
0151   void ReturnArrayMemory(void* p, size_t size) {
0152     // We only need to check for 32-bit platforms.
0153     // In 64-bit platforms the minimum allocation size from Repeated*Field will
0154     // be 16 guaranteed.
0155     if (sizeof(void*) < 8) {
0156       if (PROTOBUF_PREDICT_FALSE(size < 16)) return;
0157     } else {
0158       PROTOBUF_ASSUME(size >= 16);
0159     }
0160 
0161     // We round down to the next smaller block in case the memory doesn't match
0162     // the pattern we are looking for. eg, someone might have called Reserve()
0163     // on the repeated field.
0164     const size_t index = absl::bit_width(size) - 5;
0165 
0166     if (PROTOBUF_PREDICT_FALSE(index >= cached_block_length_)) {
0167       // We can't put this object on the freelist so make this object the
0168       // freelist. It is guaranteed it is larger than the one we have, and
0169       // large enough to hold another allocation of `size`.
0170       CachedBlock** new_list = static_cast<CachedBlock**>(p);
0171       size_t new_size = size / sizeof(CachedBlock*);
0172 
0173       std::copy(cached_blocks_, cached_blocks_ + cached_block_length_,
0174                 new_list);
0175 
0176       // We need to unpoison this memory before filling it in case it has been
0177       // poisoned by another santizer client.
0178       PROTOBUF_UNPOISON_MEMORY_REGION(
0179           new_list + cached_block_length_,
0180           (new_size - cached_block_length_) * sizeof(CachedBlock*));
0181 
0182       std::fill(new_list + cached_block_length_, new_list + new_size, nullptr);
0183 
0184       cached_blocks_ = new_list;
0185       // Make the size fit in uint8_t. This is the power of two, so we don't
0186       // need anything larger.
0187       cached_block_length_ =
0188           static_cast<uint8_t>(std::min(size_t{64}, new_size));
0189 
0190       return;
0191     }
0192 
0193     auto& cached_head = cached_blocks_[index];
0194     auto* new_node = static_cast<CachedBlock*>(p);
0195     new_node->next = cached_head;
0196     cached_head = new_node;
0197     PROTOBUF_POISON_MEMORY_REGION(p, size);
0198   }
0199 
0200  public:
0201   // Allocate space if the current region provides enough space.
0202   bool MaybeAllocateAligned(size_t n, void** out) {
0203     ABSL_DCHECK(internal::ArenaAlignDefault::IsAligned(n));
0204     ABSL_DCHECK_GE(limit_, ptr());
0205     char* ret = ptr();
0206     // ret + n may point out of the block bounds, or ret may be nullptr.
0207     // Both computations have undefined behavior when done on pointers,
0208     // so do them on uintptr_t instead.
0209     if (PROTOBUF_PREDICT_FALSE(reinterpret_cast<uintptr_t>(ret) + n >
0210                                reinterpret_cast<uintptr_t>(limit_))) {
0211       return false;
0212     }
0213     PROTOBUF_UNPOISON_MEMORY_REGION(ret, n);
0214     *out = ret;
0215     char* next = ret + n;
0216     set_ptr(next);
0217     MaybePrefetchData(next);
0218     return true;
0219   }
0220 
0221   // If there is enough space in the current block, allocate space for one
0222   // std::string object and register for destruction. The object has not been
0223   // constructed and the memory returned is uninitialized.
0224   PROTOBUF_ALWAYS_INLINE void* MaybeAllocateStringWithCleanup() {
0225     void* p;
0226     return MaybeAllocateString(p) ? p : nullptr;
0227   }
0228 
0229   PROTOBUF_ALWAYS_INLINE
0230   void* AllocateAlignedWithCleanup(size_t n, size_t align,
0231                                    void (*destructor)(void*)) {
0232     n = ArenaAlignDefault::Ceil(n);
0233     char* ret = ArenaAlignAs(align).CeilDefaultAligned(ptr());
0234     // See the comment in MaybeAllocateAligned re uintptr_t.
0235     if (PROTOBUF_PREDICT_FALSE(reinterpret_cast<uintptr_t>(ret) + n >
0236                                reinterpret_cast<uintptr_t>(limit_))) {
0237       return AllocateAlignedWithCleanupFallback(n, align, destructor);
0238     }
0239     PROTOBUF_UNPOISON_MEMORY_REGION(ret, n);
0240     char* next = ret + n;
0241     set_ptr(next);
0242     AddCleanup(ret, destructor);
0243     ABSL_DCHECK_GE(limit_, ptr());
0244     MaybePrefetchData(next);
0245     return ret;
0246   }
0247 
0248   PROTOBUF_ALWAYS_INLINE
0249   void AddCleanup(void* elem, void (*destructor)(void*)) {
0250     cleanup_list_.Add(elem, destructor, *this);
0251     MaybePrefetchCleanup();
0252   }
0253 
0254   ABSL_ATTRIBUTE_RETURNS_NONNULL void* AllocateFromStringBlock();
0255 
0256   std::vector<void*> PeekCleanupListForTesting();
0257 
0258  private:
0259   friend class ThreadSafeArena;
0260   friend class cleanup::ChunkList;
0261 
0262   // See comments for cached_blocks_.
0263   struct CachedBlock {
0264     // Simple linked list.
0265     CachedBlock* next;
0266   };
0267 
0268   static constexpr ptrdiff_t kPrefetchDataDegree = ABSL_CACHELINE_SIZE * 16;
0269   static constexpr ptrdiff_t kPrefetchCleanupDegree = ABSL_CACHELINE_SIZE * 6;
0270 
0271   // Constructor is private as only New() should be used.
0272   inline SerialArena(ArenaBlock* b, ThreadSafeArena& parent);
0273 
0274   // Constructors to handle the first SerialArena.
0275   inline explicit SerialArena(ThreadSafeArena& parent);
0276   inline SerialArena(FirstSerialArena, ArenaBlock* b, ThreadSafeArena& parent);
0277 
0278   bool MaybeAllocateString(void*& p);
0279   ABSL_ATTRIBUTE_RETURNS_NONNULL void* AllocateFromStringBlockFallback();
0280 
0281   // Prefetch the next prefetch_degree bytes after `prefetch_ptr` and
0282   // up to `limit`, if `next` is within prefetch_degree bytes of `prefetch_ptr`.
0283   PROTOBUF_ALWAYS_INLINE
0284   static const char* MaybePrefetchImpl(const ptrdiff_t prefetch_degree,
0285                                        const char* next, const char* limit,
0286                                        const char* prefetch_ptr) {
0287     if (PROTOBUF_PREDICT_TRUE(prefetch_ptr - next > prefetch_degree))
0288       return prefetch_ptr;
0289     if (PROTOBUF_PREDICT_TRUE(prefetch_ptr < limit)) {
0290       prefetch_ptr = std::max(next, prefetch_ptr);
0291       ABSL_DCHECK(prefetch_ptr != nullptr);
0292       const char* end = std::min(limit, prefetch_ptr + prefetch_degree);
0293       for (; prefetch_ptr < end; prefetch_ptr += ABSL_CACHELINE_SIZE) {
0294         absl::PrefetchToLocalCacheForWrite(prefetch_ptr);
0295       }
0296     }
0297     return prefetch_ptr;
0298   }
0299   PROTOBUF_ALWAYS_INLINE
0300   void MaybePrefetchData(const char* next) {
0301     ABSL_DCHECK(static_cast<const void*>(prefetch_ptr_) == nullptr ||
0302                 static_cast<const void*>(prefetch_ptr_) >= head());
0303     prefetch_ptr_ =
0304         MaybePrefetchImpl(kPrefetchDataDegree, next, limit_, prefetch_ptr_);
0305   }
0306   PROTOBUF_ALWAYS_INLINE
0307   void MaybePrefetchCleanup() {
0308     ABSL_DCHECK(static_cast<const void*>(cleanup_list_.prefetch_ptr_) ==
0309                     nullptr ||
0310                 static_cast<const void*>(cleanup_list_.prefetch_ptr_) >=
0311                     cleanup_list_.head_);
0312     cleanup_list_.prefetch_ptr_ = MaybePrefetchImpl(
0313         kPrefetchCleanupDegree, reinterpret_cast<char*>(cleanup_list_.next_),
0314         reinterpret_cast<char*>(cleanup_list_.limit_),
0315         cleanup_list_.prefetch_ptr_);
0316   }
0317 
0318   // Creates a new SerialArena inside mem using the remaining memory as for
0319   // future allocations.
0320   // The `parent` arena must outlive the serial arena, which is guaranteed
0321   // because the parent manages the lifetime of the serial arenas.
0322   static SerialArena* New(SizedPtr mem, ThreadSafeArena& parent);
0323   // Free SerialArena returning the memory passed in to New.
0324   template <typename Deallocator>
0325   SizedPtr Free(Deallocator deallocator);
0326 
0327   size_t FreeStringBlocks() {
0328     // On the active block delete all strings skipping the unused instances.
0329     size_t unused_bytes = string_block_unused_.load(std::memory_order_relaxed);
0330     if (StringBlock* sb = string_block_.load(std::memory_order_relaxed)) {
0331       return FreeStringBlocks(sb, unused_bytes);
0332     }
0333     return 0;
0334   }
0335   static size_t FreeStringBlocks(StringBlock* string_block, size_t unused);
0336 
0337   // Adds 'used` to space_used_ in relaxed atomic order.
0338   void AddSpaceUsed(size_t space_used) {
0339     space_used_.store(space_used_.load(std::memory_order_relaxed) + space_used,
0340                       std::memory_order_relaxed);
0341   }
0342 
0343   // Adds 'allocated` to space_allocated_ in relaxed atomic order.
0344   void AddSpaceAllocated(size_t space_allocated) {
0345     space_allocated_.store(
0346         space_allocated_.load(std::memory_order_relaxed) + space_allocated,
0347         std::memory_order_relaxed);
0348   }
0349 
0350   // Helper getters/setters to handle relaxed operations on atomic variables.
0351   ArenaBlock* head() { return head_.load(std::memory_order_relaxed); }
0352   const ArenaBlock* head() const {
0353     return head_.load(std::memory_order_relaxed);
0354   }
0355 
0356   char* ptr() { return ptr_.load(std::memory_order_relaxed); }
0357   const char* ptr() const { return ptr_.load(std::memory_order_relaxed); }
0358   void set_ptr(char* ptr) { return ptr_.store(ptr, std::memory_order_relaxed); }
0359   PROTOBUF_ALWAYS_INLINE void set_range(char* ptr, char* limit) {
0360     set_ptr(ptr);
0361     prefetch_ptr_ = ptr;
0362     limit_ = limit;
0363   }
0364 
0365   void* AllocateAlignedFallback(size_t n);
0366   void* AllocateAlignedWithCleanupFallback(size_t n, size_t align,
0367                                            void (*destructor)(void*));
0368   void AddCleanupFallback(void* elem, void (*destructor)(void*));
0369   inline void AllocateNewBlock(size_t n);
0370   inline void Init(ArenaBlock* b, size_t offset);
0371 
0372   // Members are declared here to track sizeof(SerialArena) and hotness
0373   // centrally. They are (roughly) laid out in descending order of hotness.
0374 
0375   // Next pointer to allocate from.  Always 8-byte aligned.  Points inside
0376   // head_ (and head_->pos will always be non-canonical).  We keep these
0377   // here to reduce indirection.
0378   std::atomic<char*> ptr_{nullptr};
0379   // Limiting address up to which memory can be allocated from the head block.
0380   char* limit_ = nullptr;
0381   // Current prefetch positions. Data from `ptr_` up to but not including
0382   // `prefetch_ptr_` is software prefetched.
0383   const char* prefetch_ptr_ = nullptr;
0384 
0385   // Chunked linked list for managing cleanup for arena elements.
0386   cleanup::ChunkList cleanup_list_;
0387 
0388   // The active string block.
0389   std::atomic<StringBlock*> string_block_{nullptr};
0390 
0391   // The number of unused bytes in string_block_.
0392   // We allocate from `effective_size()` down to 0 inside `string_block_`.
0393   // `unused  == 0` means that `string_block_` is exhausted. (or null).
0394   std::atomic<size_t> string_block_unused_{0};
0395 
0396   std::atomic<ArenaBlock*> head_{nullptr};  // Head of linked list of blocks.
0397   std::atomic<size_t> space_used_{0};       // Necessary for metrics.
0398   std::atomic<size_t> space_allocated_{0};
0399   ThreadSafeArena& parent_;
0400 
0401   // Repeated*Field and Arena play together to reduce memory consumption by
0402   // reusing blocks. Currently, natural growth of the repeated field types makes
0403   // them allocate blocks of size `8 + 2^N, N>=3`.
0404   // When the repeated field grows returns the previous block and we put it in
0405   // this free list.
0406   // `cached_blocks_[i]` points to the free list for blocks of size `8+2^(i+3)`.
0407   // The array of freelists is grown when needed in `ReturnArrayMemory()`.
0408   uint8_t cached_block_length_ = 0;
0409   CachedBlock** cached_blocks_ = nullptr;
0410 };
0411 
0412 inline PROTOBUF_ALWAYS_INLINE bool SerialArena::MaybeAllocateString(void*& p) {
0413   // Check how many unused instances are in the current block.
0414   size_t unused_bytes = string_block_unused_.load(std::memory_order_relaxed);
0415   if (PROTOBUF_PREDICT_TRUE(unused_bytes != 0)) {
0416     unused_bytes -= sizeof(std::string);
0417     string_block_unused_.store(unused_bytes, std::memory_order_relaxed);
0418     p = string_block_.load(std::memory_order_relaxed)->AtOffset(unused_bytes);
0419     return true;
0420   }
0421   return false;
0422 }
0423 
0424 ABSL_ATTRIBUTE_RETURNS_NONNULL inline PROTOBUF_ALWAYS_INLINE void*
0425 SerialArena::AllocateFromStringBlock() {
0426   void* p;
0427   if (ABSL_PREDICT_TRUE(MaybeAllocateString(p))) return p;
0428   return AllocateFromStringBlockFallback();
0429 }
0430 
0431 }  // namespace internal
0432 }  // namespace protobuf
0433 }  // namespace google
0434 
0435 #include "google/protobuf/port_undef.inc"
0436 
0437 #endif  // GOOGLE_PROTOBUF_SERIAL_ARENA_H__