Go to the documentation of this file.
31 #include <google/protobuf/arena.h>
40 #include <google/protobuf/arena_impl.h>
42 #include <google/protobuf/stubs/mutex.h>
43 #ifdef ADDRESS_SANITIZER
44 #include <sanitizer/asan_interface.h>
45 #endif // ADDRESS_SANITIZER
47 #include <google/protobuf/port_def.inc>
54 size_t last_size,
size_t min_bytes) {
56 if (policy_ptr) policy = *policy_ptr;
82 :
dealloc_(policy ? policy->block_dealloc : nullptr),
86 #ifdef ADDRESS_SANITIZER
89 ASAN_UNPOISON_MEMORY_REGION(
mem.ptr,
mem.size);
90 #endif // ADDRESS_SANITIZER
94 #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
95 ::operator
delete(
mem.ptr,
mem.size);
97 ::operator
delete(
mem.ptr);
112 limit_ =
b->Pointer(
b->size &
static_cast<size_t>(-8));
122 template <
typename Deallocator>
135 std::pair<void*, SerialArena::CleanupNode*>
160 auto relaxed = std::memory_order_relaxed;
166 #ifdef ADDRESS_SANITIZER
168 #endif // ADDRESS_SANITIZER
184 b->Pointer(
b->size &
static_cast<size_t>(-8)));
186 auto num = limit -
it;
188 for (;
it < limit;
it++) {
189 it->cleanup(
it->elem);
199 #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
201 static internal::ThreadLocalStorage<ThreadCache>*
thread_cache_ =
202 new internal::ThreadLocalStorage<ThreadCache>();
205 #elif defined(PROTOBUF_USE_DLLS)
212 PROTOBUF_THREAD_LOCAL ThreadSafeArena::ThreadCache
234 #define GOOGLE_DCHECK_POLICY_FLAGS_() \
235 if (old_alloc_policy > 3) \
236 GOOGLE_CHECK_EQ(old_alloc_policy & 3, alloc_policy_.get_raw() & 3)
238 #define GOOGLE_DCHECK_POLICY_FLAGS_()
264 if (
mem !=
nullptr &&
size >= kMinimumSize) {
273 auto sa =
threads_.load(std::memory_order_relaxed);
276 if (!sa || !sa->MaybeAllocateAligned(kAPSize, &p)) {
286 #undef GOOGLE_DCHECK_POLICY_FLAGS_
299 if (PROTOBUF_PREDICT_FALSE((
id & (kInc - 1)) == 0)) {
300 constexpr
auto relaxed = std::memory_order_relaxed;
310 hint_.store(
nullptr, std::memory_order_relaxed);
311 threads_.store(
nullptr, std::memory_order_relaxed);
320 threads_.store(serial, std::memory_order_relaxed);
329 size_t space_allocated = 0;
330 auto mem =
Free(&space_allocated);
337 space_allocated +=
mem.size;
342 if (collector) collector->
OnDestroy(space_allocated);
349 if (
mem.ptr) deallocator(
mem);
350 mem =
a->Free(deallocator);
361 size_t space_allocated = 0;
362 auto mem =
Free(&space_allocated);
366 auto saved_policy = *policy;
368 space_allocated +=
mem.size;
375 if (collector) collector->
OnReset(space_allocated);
381 space_allocated +=
mem.size;
389 return space_allocated;
392 std::pair<void*, SerialArena::CleanupNode*>
394 const std::type_info*
type) {
414 const std::type_info*
type) {
427 std::pair<void*, SerialArena::CleanupNode*>
429 size_t n,
const std::type_info*
type) {
444 for (; serial; serial = serial->
next()) {
453 for (; serial; serial = serial->
next()) {
467 for (; serial; serial = serial->
next()) {
468 if (serial->
owner() == me) {
482 }
while (!
threads_.compare_exchange_weak(
483 head, serial, std::memory_order_release, std::memory_order_relaxed));
492 PROTOBUF_FUNC_ALIGN(32)
493 void*
Arena::AllocateAlignedNoHook(
size_t n) {
494 return impl_.AllocateAligned(n,
nullptr);
497 PROTOBUF_FUNC_ALIGN(32)
502 PROTOBUF_FUNC_ALIGN(32)
505 return impl_.AllocateAlignedWithCleanup(n,
type);
511 #include <google/protobuf/port_undef.inc>
static constexpr size_t kCleanupSize
uint64_t SpaceAllocated() const
size_t * space_allocated_
bool should_record_allocs() const
void SetInitialBlock(void *mem, size_t size)
void *(* block_alloc)(size_t)
void CacheSerialArena(SerialArena *serial)
void AllocateNewBlock(size_t n, const AllocationPolicy *policy)
static CacheAlignedLifecycleIdGenerator lifecycle_id_generator_
std::atomic< SerialArena * > hint_
static SerialArena * New(SerialArena::Memory mem, void *owner)
void AddCleanup(void *elem, void(*cleanup)(void *))
void operator()(SerialArena::Memory mem) const
GetDeallocator(const AllocationPolicy *policy, size_t *space_allocated)
std::pair< void *, CleanupNode * > AllocateAlignedWithCleanupFallback(size_t n, const AllocationPolicy *policy)
#define GOOGLE_CHECK_EQ(A, B)
size_t AlignUpTo8(size_t n)
OPENSSL_EXPORT pem_password_cb void * u
static constexpr size_t kSerialArenaSize
const AllocationPolicy * AllocPolicy() const
void set_next(SerialArena *next)
void InitializeFrom(void *mem, size_t size)
static constexpr size_t kPerThreadIds
bool is_user_owned_initial_block() const
grpc_core::ScopedArenaPtr arena
std::pair< void *, SerialArena::CleanupNode * > AllocateAlignedWithCleanup(size_t n, const std::type_info *type)
void * AllocateFromExisting(size_t n)
std::shared_ptr< ExternalConnectionAcceptorImpl > impl_
static ThreadCache & thread_cache()
void * AllocateAligned(size_t n, const AllocationPolicy *policy)
ArenaMetricsCollector * metrics_collector
unsigned __int64 uint64_t
void RecordAlloc(const std::type_info *allocated_type, size_t n) const
std::pair< void *, CleanupNode * > AllocateAlignedWithCleanup(size_t n, const AllocationPolicy *policy)
void * AllocateAlignedFallback(size_t n, const AllocationPolicy *policy)
void(* dealloc_)(void *, size_t)
_W64 unsigned int uintptr_t
SerialArena::Memory Free(size_t *space_allocated)
std::atomic< LifecycleIdAtomic > id
Memory Free(Deallocator deallocator)
SerialArena * next() const
TaggedAllocationPolicyPtr alloc_policy_
uint64_t SpaceUsed() const
void set_should_record_allocs(bool v)
uint64_t SpaceUsed() const
uintptr_t get_raw() const
SerialArena(Block *b, void *owner)
std::atomic< size_t > space_allocated_
SerialArena * GetSerialArenaFallback(void *me)
uint64_t SpaceAllocated() const
virtual void OnReset(uint64_t space_allocated)=0
void set_is_user_owned_initial_block(bool v)
void InitializeWithPolicy(void *mem, size_t size, AllocationPolicy policy)
static SerialArena::Memory AllocateMemory(const AllocationPolicy *policy_ptr, size_t last_size, size_t min_bytes)
#define GOOGLE_DCHECK_POLICY_FLAGS_()
PROTOBUF_NDEBUG_INLINE bool GetSerialArenaFast(SerialArena **arena)
PROTOBUF_ALWAYS_INLINE bool IsMessageOwned() const
void PerSerialArena(Functor fn)
static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_
static constexpr size_t kBlockHeaderSize
std::atomic< SerialArena * > threads_
#define GOOGLE_LOG(LEVEL)
std::pair< void *, CleanupNode * > AllocateFromExistingWithCleanupFallback(size_t n)
std::pair< std::string, std::string > pair
void * AllocateAlignedFallback(size_t n, const std::type_info *type)
std::pair< void *, SerialArena::CleanupNode * > AllocateAlignedWithCleanupFallback(size_t n, const std::type_info *type)
void set_policy(AllocationPolicy *policy)
uint64_t LifecycleIdAtomic
#define GOOGLE_CHECK_LE(A, B)
static constexpr size_t kBlockHeaderSize
virtual void OnDestroy(uint64_t space_allocated)=0
uint64_t next_lifecycle_id
grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:42