arena_impl.h
Go to the documentation of this file.
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 
31 // This file defines an Arena allocator for better allocation performance.
32 
33 #ifndef GOOGLE_PROTOBUF_ARENA_IMPL_H__
34 #define GOOGLE_PROTOBUF_ARENA_IMPL_H__
35 
36 #include <atomic>
37 #include <limits>
38 
41 
42 #ifdef ADDRESS_SANITIZER
43 #include <sanitizer/asan_interface.h>
44 #endif // ADDRESS_SANITIZER
45 
46 #include <google/protobuf/port_def.inc>
47 
48 
49 namespace google {
50 namespace protobuf {
51 namespace internal {
52 
53 inline size_t AlignUpTo8(size_t n) {
54  // Align n to next multiple of 8 (from Hacker's Delight, Chapter 3.)
55  return (n + 7) & static_cast<size_t>(-8);
56 }
57 
58 using LifecycleId = int64_t;
59 
60 // This class provides the core Arena memory allocation library. Different
61 // implementations only need to implement the public interface below.
62 // Arena is not a template type as that would only be useful if all protos
63 // in turn would be templates, which will/cannot happen. However separating
64 // the memory allocation part from the cruft of the API users expect we can
65 // use #ifdef the select the best implementation based on hardware / OS.
66 class PROTOBUF_EXPORT ArenaImpl {
67  public:
68  struct Options {
73  void* (*block_alloc)(size_t);
74  void (*block_dealloc)(void*, size_t);
75 
76  template <typename O>
77  explicit Options(const O& options)
78  : start_block_size(options.start_block_size),
79  max_block_size(options.max_block_size),
80  initial_block(options.initial_block),
81  initial_block_size(options.initial_block_size),
82  block_alloc(options.block_alloc),
83  block_dealloc(options.block_dealloc) {}
84  };
85 
86  template <typename O>
87  explicit ArenaImpl(const O& options) : options_(options) {
88  if (options_.initial_block != NULL && options_.initial_block_size > 0) {
89  GOOGLE_CHECK_GE(options_.initial_block_size, sizeof(Block))
90  << ": Initial block size too small for header.";
91  initial_block_ = reinterpret_cast<Block*>(options_.initial_block);
92  } else {
93  initial_block_ = NULL;
94  }
95 
96  Init();
97  }
98 
99  // Destructor deletes all owned heap allocated objects, and destructs objects
100  // that have non-trivial destructors, except for proto2 message objects whose
101  // destructors can be skipped. Also, frees all blocks except the initial block
102  // if it was passed in.
103  ~ArenaImpl();
104 
105  uint64 Reset();
106 
107  uint64 SpaceAllocated() const;
108  uint64 SpaceUsed() const;
109 
110  void* AllocateAligned(size_t n);
111 
112  void* AllocateAlignedAndAddCleanup(size_t n, void (*cleanup)(void*));
113 
114  // Add object pointer and cleanup function pointer to the list.
115  void AddCleanup(void* elem, void (*cleanup)(void*));
116 
117  private:
118  void* AllocateAlignedFallback(size_t n);
119  void* AllocateAlignedAndAddCleanupFallback(size_t n, void (*cleanup)(void*));
120  void AddCleanupFallback(void* elem, void (*cleanup)(void*));
121 
122  // Node contains the ptr of the object to be cleaned up and the associated
123  // cleanup function ptr.
124  struct CleanupNode {
125  void* elem; // Pointer to the object to be cleaned up.
126  void (*cleanup)(void*); // Function pointer to the destructor or deleter.
127  };
128 
129  // Cleanup uses a chunked linked list, to reduce pointer chasing.
130  struct CleanupChunk {
131  static size_t SizeOf(size_t i) {
132  return sizeof(CleanupChunk) + (sizeof(CleanupNode) * (i - 1));
133  }
134  size_t size; // Total elements in the list.
135  CleanupChunk* next; // Next node in the list.
136  CleanupNode nodes[1]; // True length is |size|.
137  };
138 
139  class Block;
140 
141  // A thread-unsafe Arena that can only be used within its owning thread.
142  class PROTOBUF_EXPORT SerialArena {
143  public:
144  // The allocate/free methods here are a little strange, since SerialArena is
145  // allocated inside a Block which it also manages. This is to avoid doing
146  // an extra allocation for the SerialArena itself.
147 
148  // Creates a new SerialArena inside Block* and returns it.
149  static SerialArena* New(Block* b, void* owner, ArenaImpl* arena);
150 
151  // Destroys this SerialArena, freeing all blocks with the given dealloc
152  // function, except any block equal to |initial_block|.
153  static uint64 Free(SerialArena* serial, Block* initial_block,
154  void (*block_dealloc)(void*, size_t));
155 
156  void CleanupList();
157  uint64 SpaceUsed() const;
158 
159  void* AllocateAligned(size_t n) {
160  GOOGLE_DCHECK_EQ(internal::AlignUpTo8(n), n); // Must be already aligned.
161  GOOGLE_DCHECK_GE(limit_, ptr_);
162  if (PROTOBUF_PREDICT_FALSE(static_cast<size_t>(limit_ - ptr_) < n)) {
163  return AllocateAlignedFallback(n);
164  }
165  void* ret = ptr_;
166  ptr_ += n;
167 #ifdef ADDRESS_SANITIZER
168  ASAN_UNPOISON_MEMORY_REGION(ret, n);
169 #endif // ADDRESS_SANITIZER
170  return ret;
171  }
172 
173  void AddCleanup(void* elem, void (*cleanup)(void*)) {
174  if (PROTOBUF_PREDICT_FALSE(cleanup_ptr_ == cleanup_limit_)) {
175  AddCleanupFallback(elem, cleanup);
176  return;
177  }
178  cleanup_ptr_->elem = elem;
179  cleanup_ptr_->cleanup = cleanup;
180  cleanup_ptr_++;
181  }
182 
183  void* AllocateAlignedAndAddCleanup(size_t n, void (*cleanup)(void*)) {
184  void* ret = AllocateAligned(n);
185  AddCleanup(ret, cleanup);
186  return ret;
187  }
188 
189  void* owner() const { return owner_; }
190  SerialArena* next() const { return next_; }
191  void set_next(SerialArena* next) { next_ = next; }
192 
193  private:
194  void* AllocateAlignedFallback(size_t n);
195  void AddCleanupFallback(void* elem, void (*cleanup)(void*));
196  void CleanupListFallback();
197 
198  ArenaImpl* arena_; // Containing arena.
199  void* owner_; // &ThreadCache of this thread;
200  Block* head_; // Head of linked list of blocks.
201  CleanupChunk* cleanup_; // Head of cleanup list.
202  SerialArena* next_; // Next SerialArena in this linked list.
203 
204  // Next pointer to allocate from. Always 8-byte aligned. Points inside
205  // head_ (and head_->pos will always be non-canonical). We keep these
206  // here to reduce indirection.
207  char* ptr_;
208  char* limit_;
209 
210  // Next CleanupList members to append to. These point inside cleanup_.
213  };
214 
215  // Blocks are variable length malloc-ed objects. The following structure
216  // describes the common header for all blocks.
217  class PROTOBUF_EXPORT Block {
218  public:
219  Block(size_t size, Block* next);
220 
221  char* Pointer(size_t n) {
222  GOOGLE_DCHECK(n <= size_);
223  return reinterpret_cast<char*>(this) + n;
224  }
225 
226  Block* next() const { return next_; }
227  size_t pos() const { return pos_; }
228  size_t size() const { return size_; }
229  void set_pos(size_t pos) { pos_ = pos; }
230 
231  private:
232  Block* next_; // Next block for this thread.
233  size_t pos_;
234  size_t size_;
235  // data follows
236  };
237 
238  struct ThreadCache {
239 #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
240  // If we are using the ThreadLocalStorage class to store the ThreadCache,
241  // then the ThreadCache's default constructor has to be responsible for
242  // initializing it.
243  ThreadCache() : last_lifecycle_id_seen(-1), last_serial_arena(NULL) {}
244 #endif
245 
246  // The ThreadCache is considered valid as long as this matches the
247  // lifecycle_id of the arena being used.
250  };
251  static std::atomic<LifecycleId> lifecycle_id_generator_;
252 #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
253  // Android ndk does not support GOOGLE_THREAD_LOCAL keyword so we use a custom thread
254  // local storage class we implemented.
255  // iOS also does not support the GOOGLE_THREAD_LOCAL keyword.
256  static ThreadCache& thread_cache();
257 #elif defined(PROTOBUF_USE_DLLS)
258  // Thread local variables cannot be exposed through DLL interface but we can
259  // wrap them in static functions.
260  static ThreadCache& thread_cache();
261 #else
263  static ThreadCache& thread_cache() { return thread_cache_; }
264 #endif
265 
266  void Init();
267 
268  // Free all blocks and return the total space used which is the sums of sizes
269  // of the all the allocated blocks.
270  uint64 FreeBlocks();
271  // Delete or Destruct all objects owned by the arena.
272  void CleanupList();
273 
274  inline void CacheSerialArena(SerialArena* serial) {
275  thread_cache().last_serial_arena = serial;
276  thread_cache().last_lifecycle_id_seen = lifecycle_id_;
277  // TODO(haberman): evaluate whether we would gain efficiency by getting rid
278  // of hint_. It's the only write we do to ArenaImpl in the allocation path,
279  // which will dirty the cache line.
280 
281  hint_.store(serial, std::memory_order_release);
282  }
283 
284  std::atomic<SerialArena*>
285  threads_; // Pointer to a linked list of SerialArena.
286  std::atomic<SerialArena*> hint_; // Fast thread-local block access
287  std::atomic<size_t> space_allocated_; // Total size of all allocated blocks.
288 
289  Block* initial_block_; // If non-NULL, points to the block that came from
290  // user data.
291 
292  Block* NewBlock(Block* last_block, size_t min_bytes);
293 
294  SerialArena* GetSerialArena();
295  bool GetSerialArenaFast(SerialArena** arena);
296  SerialArena* GetSerialArenaFallback(void* me);
297  LifecycleId lifecycle_id_; // Unique for each arena. Changes on Reset().
298 
300 
302  // All protos have pointers back to the arena hence Arena must have
303  // pointer stability.
304  ArenaImpl(ArenaImpl&&) = delete;
305  ArenaImpl& operator=(ArenaImpl&&) = delete;
306 
307  public:
308  // kBlockHeaderSize is sizeof(Block), aligned up to the nearest multiple of 8
309  // to protect the invariant that pos is always at a multiple of 8.
310  static const size_t kBlockHeaderSize =
311  (sizeof(Block) + 7) & static_cast<size_t>(-8);
312  static const size_t kSerialArenaSize =
313  (sizeof(SerialArena) + 7) & static_cast<size_t>(-8);
314  static_assert(kBlockHeaderSize % 8 == 0,
315  "kBlockHeaderSize must be a multiple of 8.");
316  static_assert(kSerialArenaSize % 8 == 0,
317  "kSerialArenaSize must be a multiple of 8.");
318 };
319 
320 } // namespace internal
321 } // namespace protobuf
322 } // namespace google
323 
324 #include <google/protobuf/port_undef.inc>
325 
326 #endif // GOOGLE_PROTOBUF_ARENA_IMPL_H__
google::protobuf.internal::ArenaImpl::options_
Options options_
Definition: arena_impl.h:299
google::protobuf.internal::ArenaImpl::hint_
std::atomic< SerialArena * > hint_
Definition: arena_impl.h:286
google::protobuf.internal::ArenaImpl::Options::Options
Options(const O &options)
Definition: arena_impl.h:77
google::protobuf.internal::ArenaImpl::ThreadCache::last_lifecycle_id_seen
LifecycleId last_lifecycle_id_seen
Definition: arena_impl.h:248
google::protobuf.internal::ArenaImpl::Block::size_
size_t size_
Definition: arena_impl.h:234
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
google::protobuf.internal::ArenaImpl::SerialArena::next
SerialArena * next() const
Definition: arena_impl.h:190
google::protobuf.internal::ArenaImpl::thread_cache
static ThreadCache & thread_cache()
Definition: arena_impl.h:263
google::protobuf.internal::ArenaImpl::SerialArena::next_
SerialArena * next_
Definition: arena_impl.h:202
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf.internal::ArenaImpl::ThreadCache
Definition: arena_impl.h:238
google::protobuf.internal::ArenaImpl::SerialArena::cleanup_ptr_
CleanupNode * cleanup_ptr_
Definition: arena_impl.h:211
GOOGLE_DCHECK_GE
#define GOOGLE_DCHECK_GE
Definition: logging.h:201
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf.internal::ArenaImpl::SerialArena::ptr_
char * ptr_
Definition: arena_impl.h:207
GOOGLE_DCHECK
#define GOOGLE_DCHECK
Definition: logging.h:194
google::protobuf.internal::ArenaImpl::ArenaImpl
ArenaImpl(const O &options)
Definition: arena_impl.h:87
google::protobuf.internal::ArenaImpl::CleanupChunk::size
size_t size
Definition: arena_impl.h:134
google::protobuf.internal::AlignUpTo8
size_t AlignUpTo8(size_t n)
Definition: arena_impl.h:53
google::protobuf.internal::ArenaImpl::SerialArena::cleanup_
CleanupChunk * cleanup_
Definition: arena_impl.h:201
google::protobuf.internal::ArenaImpl::space_allocated_
std::atomic< size_t > space_allocated_
Definition: arena_impl.h:287
google::protobuf.internal::ArenaImpl::Block::size
size_t size() const
Definition: arena_impl.h:228
google::protobuf.internal::ArenaImpl::CleanupChunk
Definition: arena_impl.h:130
google::protobuf.internal::ArenaImpl::SerialArena::AllocateAligned
void * AllocateAligned(size_t n)
Definition: arena_impl.h:159
google::protobuf::python::cmessage::Init
static int Init(CMessage *self, PyObject *args, PyObject *kwargs)
Definition: python/google/protobuf/pyext/message.cc:1286
google::protobuf.internal::ArenaImpl::Options::initial_block_size
size_t initial_block_size
Definition: arena_impl.h:72
google::protobuf.internal::ArenaImpl::SerialArena::arena_
ArenaImpl * arena_
Definition: arena_impl.h:198
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
mox.Reset
def Reset(*args)
Definition: mox.py:257
google::protobuf.internal::ArenaImpl::initial_block_
Block * initial_block_
Definition: arena_impl.h:289
google::protobuf.internal::ArenaImpl::SerialArena::AddCleanup
void AddCleanup(void *elem, void(*cleanup)(void *))
Definition: arena_impl.h:173
google::protobuf.internal::ArenaImpl::thread_cache_
static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_
Definition: arena_impl.h:262
google::protobuf::python::cdescriptor_pool::New
static PyObject * New(PyTypeObject *type, PyObject *args, PyObject *kwargs)
Definition: descriptor_pool.cc:177
google::protobuf.internal::ArenaImpl::Block
Definition: arena_impl.h:217
google::protobuf.internal::ArenaImpl::Options::max_block_size
size_t max_block_size
Definition: arena_impl.h:70
google::protobuf::uint64
uint64_t uint64
Definition: protobuf/src/google/protobuf/stubs/port.h:156
google::protobuf.internal::ArenaImpl::Block::Pointer
char * Pointer(size_t n)
Definition: arena_impl.h:221
google::protobuf.internal::LifecycleId
int64_t LifecycleId
Definition: arena_impl.h:58
GOOGLE_THREAD_LOCAL
#define GOOGLE_THREAD_LOCAL
Definition: protobuf/src/google/protobuf/stubs/port.h:258
google::protobuf.internal::ArenaImpl::CacheSerialArena
void CacheSerialArena(SerialArena *serial)
Definition: arena_impl.h:274
google::protobuf.internal::ArenaImpl::CleanupChunk::SizeOf
static size_t SizeOf(size_t i)
Definition: arena_impl.h:131
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
n
GLdouble n
Definition: glcorearb.h:4153
google::protobuf.internal::ArenaImpl::CleanupChunk::next
CleanupChunk * next
Definition: arena_impl.h:135
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf.internal::ArenaImpl::SerialArena::owner_
void * owner_
Definition: arena_impl.h:199
google::protobuf.internal::ArenaImpl::SerialArena::AllocateAlignedAndAddCleanup
void * AllocateAlignedAndAddCleanup(size_t n, void(*cleanup)(void *))
Definition: arena_impl.h:183
google::protobuf.internal::ArenaImpl::SerialArena::limit_
char * limit_
Definition: arena_impl.h:208
google::protobuf.internal::ArenaImpl::threads_
std::atomic< SerialArena * > threads_
Definition: arena_impl.h:285
google::protobuf.internal::ArenaImpl
Definition: arena_impl.h:66
google::protobuf.internal::ArenaImpl::Block::pos_
size_t pos_
Definition: arena_impl.h:233
common.h
google::protobuf.internal::ArenaImpl::Block::next_
Block * next_
Definition: arena_impl.h:232
google::protobuf.internal::ArenaImpl::CleanupNode::elem
void * elem
Definition: arena_impl.h:125
google::protobuf.internal::ArenaImpl::CleanupNode
Definition: arena_impl.h:124
google::protobuf.internal::ArenaImpl::SerialArena::head_
Block * head_
Definition: arena_impl.h:200
size
GLsizeiptr size
Definition: glcorearb.h:2943
google::protobuf.internal::ArenaImpl::Block::pos
size_t pos() const
Definition: arena_impl.h:227
google::protobuf.internal::ArenaImpl::Options::initial_block
char * initial_block
Definition: arena_impl.h:71
google::protobuf.internal::ArenaImpl::Block::next
Block * next() const
Definition: arena_impl.h:226
logging.h
GOOGLE_CHECK_GE
#define GOOGLE_CHECK_GE(A, B)
Definition: logging.h:161
google::protobuf.internal::ArenaImpl::SerialArena::set_next
void set_next(SerialArena *next)
Definition: arena_impl.h:191
pos_
std::list< value_type >::iterator pos_
Definition: gmock-matchers_test.cc:5240
google::protobuf.internal::ArenaImpl::Options
Definition: arena_impl.h:68
google::protobuf.internal::ArenaImpl::lifecycle_id_
LifecycleId lifecycle_id_
Definition: arena_impl.h:297
internal
Definition: any.pb.h:40
google::protobuf.internal::ArenaImpl::SerialArena::cleanup_limit_
CleanupNode * cleanup_limit_
Definition: arena_impl.h:212
google::protobuf.internal::ArenaImpl::SerialArena::owner
void * owner() const
Definition: arena_impl.h:189
next
static size_t next(const upb_table *t, size_t i)
Definition: php/ext/google/protobuf/upb.c:4889
google::protobuf.internal::ArenaImpl::Options::start_block_size
size_t start_block_size
Definition: arena_impl.h:69
google::protobuf.internal::ArenaImpl::Block::set_pos
void set_pos(size_t pos)
Definition: arena_impl.h:229
google::protobuf.internal::ArenaImpl::SerialArena
Definition: arena_impl.h:142
google
Definition: data_proto2_to_proto3_util.h:11
options_
DebugStringOptions options_
Definition: src/google/protobuf/descriptor.cc:2410
google::protobuf.internal::ArenaImpl::lifecycle_id_generator_
static std::atomic< LifecycleId > lifecycle_id_generator_
Definition: arena_impl.h:251
google::protobuf.internal::ArenaImpl::ThreadCache::last_serial_arena
SerialArena * last_serial_arena
Definition: arena_impl.h:249
GOOGLE_DCHECK_EQ
#define GOOGLE_DCHECK_EQ
Definition: logging.h:196


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48