Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 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 #ifndef GOOGLE_PROTOBUF_ARENA_CLEANUP_H__
0009 #define GOOGLE_PROTOBUF_ARENA_CLEANUP_H__
0010 
0011 #include <cstddef>
0012 #include <cstring>
0013 #include <vector>
0014 
0015 #include "absl/base/attributes.h"
0016 #include "absl/base/prefetch.h"
0017 
0018 // Must be included last.
0019 #include "google/protobuf/port_def.inc"
0020 
0021 namespace google {
0022 namespace protobuf {
0023 namespace internal {
0024 
0025 class SerialArena;
0026 
0027 namespace cleanup {
0028 
0029 // Helper function invoking the destructor of `object`
0030 template <typename T>
0031 void arena_destruct_object(void* object) {
0032   reinterpret_cast<T*>(object)->~T();
0033 }
0034 
0035 // CleanupNode contains the object (`elem`) that needs to be
0036 // destroyed, and the function to destroy it (`destructor`)
0037 // elem must be aligned at minimum on a 4 byte boundary.
0038 struct CleanupNode {
0039   // Optimization: performs a prefetch on the elem for the cleanup node. We
0040   // explicitly use NTA prefetch here to avoid polluting remote caches: we are
0041   // destroying these instances, there is no purpose for these cache lines to
0042   // linger around in remote caches.
0043   ABSL_ATTRIBUTE_ALWAYS_INLINE void Prefetch() {
0044     // TODO: we should also prefetch the destructor code once
0045     // processors support code prefetching.
0046     absl::PrefetchToLocalCacheNta(elem);
0047   }
0048 
0049   // Destroys the object referenced by the cleanup node.
0050   ABSL_ATTRIBUTE_ALWAYS_INLINE void Destroy() { destructor(elem); }
0051 
0052   void* elem;
0053   void (*destructor)(void*);
0054 };
0055 
0056 // Manages the list of cleanup nodes in a chunked linked list. Chunks grow by
0057 // factors of two up to a limit. Trivially destructible, but Cleanup() must be
0058 // called before destruction.
0059 class ChunkList {
0060  public:
0061   PROTOBUF_ALWAYS_INLINE void Add(void* elem, void (*destructor)(void*),
0062                                   SerialArena& arena) {
0063     if (PROTOBUF_PREDICT_TRUE(next_ < limit_)) {
0064       AddFromExisting(elem, destructor);
0065       return;
0066     }
0067     AddFallback(elem, destructor, arena);
0068   }
0069 
0070   // Runs all inserted cleanups and frees allocated chunks. Must be called
0071   // before destruction.
0072   void Cleanup(const SerialArena& arena);
0073 
0074  private:
0075   struct Chunk;
0076   friend class internal::SerialArena;
0077 
0078   void AddFallback(void* elem, void (*destructor)(void*), SerialArena& arena);
0079   ABSL_ATTRIBUTE_ALWAYS_INLINE void AddFromExisting(void* elem,
0080                                                     void (*destructor)(void*)) {
0081     *next_++ = CleanupNode{elem, destructor};
0082   }
0083 
0084   // Returns the pointers to the to-be-cleaned objects.
0085   std::vector<void*> PeekForTesting();
0086 
0087   Chunk* head_ = nullptr;
0088   CleanupNode* next_ = nullptr;
0089   CleanupNode* limit_ = nullptr;
0090   // Current prefetch position. Data from `next_` up to but not including
0091   // `prefetch_ptr_` is software prefetched. Used in SerialArena prefetching.
0092   const char* prefetch_ptr_ = nullptr;
0093 };
0094 
0095 }  // namespace cleanup
0096 }  // namespace internal
0097 }  // namespace protobuf
0098 }  // namespace google
0099 
0100 #include "google/protobuf/port_undef.inc"
0101 
0102 #endif  // GOOGLE_PROTOBUF_ARENA_CLEANUP_H__