• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H__
34 #define GOOGLE_PROTOBUF_ARENA_H__
35 
36 
37 #include <limits>
38 #include <type_traits>
39 #include <utility>
40 #ifdef max
41 #undef max  // Visual Studio defines this macro
42 #endif
43 #if defined(_MSC_VER) && !defined(_LIBCPP_STD_VER) && !_HAS_EXCEPTIONS
44 // Work around bugs in MSVC <typeinfo> header when _HAS_EXCEPTIONS=0.
45 #include <exception>
46 #include <typeinfo>
47 namespace std {
48 using type_info = ::type_info;
49 }
50 #else
51 #include <typeinfo>
52 #endif
53 
54 #include <type_traits>
55 #include <google/protobuf/arena_impl.h>
56 #include <google/protobuf/port.h>
57 
58 #include <google/protobuf/port_def.inc>
59 
60 #ifdef SWIG
61 #error "You cannot SWIG proto headers"
62 #endif
63 
64 namespace google {
65 namespace protobuf {
66 
67 struct ArenaOptions;  // defined below
68 
69 }  // namespace protobuf
70 }  // namespace google
71 
72 namespace google {
73 namespace protobuf {
74 
75 class Arena;    // defined below
76 class Message;  // defined in message.h
77 class MessageLite;
78 template <typename Key, typename T>
79 class Map;
80 
81 namespace arena_metrics {
82 
83 void EnableArenaMetrics(ArenaOptions* options);
84 
85 }  // namespace arena_metrics
86 
87 namespace internal {
88 
89 struct ArenaStringPtr;  // defined in arenastring.h
90 class LazyField;        // defined in lazy_field.h
91 class EpsCopyInputStream;  // defined in parse_context.h
92 
93 template <typename Type>
94 class GenericTypeHandler;  // defined in repeated_field.h
95 
96 // Templated cleanup methods.
97 template <typename T>
arena_destruct_object(void * object)98 void arena_destruct_object(void* object) {
99   reinterpret_cast<T*>(object)->~T();
100 }
101 template <typename T>
arena_delete_object(void * object)102 void arena_delete_object(void* object) {
103   delete reinterpret_cast<T*>(object);
104 }
105 }  // namespace internal
106 
107 // ArenaOptions provides optional additional parameters to arena construction
108 // that control its block-allocation behavior.
109 struct ArenaOptions {
110   // This defines the size of the first block requested from the system malloc.
111   // Subsequent block sizes will increase in a geometric series up to a maximum.
112   size_t start_block_size;
113 
114   // This defines the maximum block size requested from system malloc (unless an
115   // individual arena allocation request occurs with a size larger than this
116   // maximum). Requested block sizes increase up to this value, then remain
117   // here.
118   size_t max_block_size;
119 
120   // An initial block of memory for the arena to use, or NULL for none. If
121   // provided, the block must live at least as long as the arena itself. The
122   // creator of the Arena retains ownership of the block after the Arena is
123   // destroyed.
124   char* initial_block;
125 
126   // The size of the initial block, if provided.
127   size_t initial_block_size;
128 
129   // A function pointer to an alloc method that returns memory blocks of size
130   // requested. By default, it contains a ptr to the malloc function.
131   //
132   // NOTE: block_alloc and dealloc functions are expected to behave like
133   // malloc and free, including Asan poisoning.
134   void* (*block_alloc)(size_t);
135   // A function pointer to a dealloc method that takes ownership of the blocks
136   // from the arena. By default, it contains a ptr to a wrapper function that
137   // calls free.
138   void (*block_dealloc)(void*, size_t);
139 
ArenaOptionsArenaOptions140   ArenaOptions()
141       : start_block_size(kDefaultStartBlockSize),
142         max_block_size(kDefaultMaxBlockSize),
143         initial_block(NULL),
144         initial_block_size(0),
145         block_alloc(kDefaultBlockAlloc),
146         block_dealloc(&internal::ArenaFree),
147         make_metrics_collector(nullptr) {}
148 
149   PROTOBUF_EXPORT static void* (*const kDefaultBlockAlloc)(size_t);
150 
151  private:
152   // If make_metrics_collector is not nullptr, it will be called at Arena init
153   // time. It may return a pointer to a collector instance that will be notified
154   // of interesting events related to the arena.
155   internal::ArenaMetricsCollector* (*make_metrics_collector)();
156 
157   // Constants define default starting block size and max block size for
158   // arena allocator behavior -- see descriptions above.
159   static const size_t kDefaultStartBlockSize =
160       internal::ArenaImpl::kDefaultStartBlockSize;
161   static const size_t kDefaultMaxBlockSize =
162       internal::ArenaImpl::kDefaultMaxBlockSize;
163 
164   friend void arena_metrics::EnableArenaMetrics(ArenaOptions*);
165 
166   friend class Arena;
167   friend class ArenaOptionsTestFriend;
168   friend class internal::ArenaImpl;
169 };
170 
171 // Support for non-RTTI environments. (The metrics hooks API uses type
172 // information.)
173 #if PROTOBUF_RTTI
174 #define RTTI_TYPE_ID(type) (&typeid(type))
175 #else
176 #define RTTI_TYPE_ID(type) (NULL)
177 #endif
178 
179 // Arena allocator. Arena allocation replaces ordinary (heap-based) allocation
180 // with new/delete, and improves performance by aggregating allocations into
181 // larger blocks and freeing allocations all at once. Protocol messages are
182 // allocated on an arena by using Arena::CreateMessage<T>(Arena*), below, and
183 // are automatically freed when the arena is destroyed.
184 //
185 // This is a thread-safe implementation: multiple threads may allocate from the
186 // arena concurrently. Destruction is not thread-safe and the destructing
187 // thread must synchronize with users of the arena first.
188 //
189 // An arena provides two allocation interfaces: CreateMessage<T>, which works
190 // for arena-enabled proto2 message types as well as other types that satisfy
191 // the appropriate protocol (described below), and Create<T>, which works for
192 // any arbitrary type T. CreateMessage<T> is better when the type T supports it,
193 // because this interface (i) passes the arena pointer to the created object so
194 // that its sub-objects and internal allocations can use the arena too, and (ii)
195 // elides the object's destructor call when possible. Create<T> does not place
196 // any special requirements on the type T, and will invoke the object's
197 // destructor when the arena is destroyed.
198 //
199 // The arena message allocation protocol, required by
200 // CreateMessage<T>(Arena* arena, Args&&... args), is as follows:
201 //
202 // - The type T must have (at least) two constructors: a constructor callable
203 //   with `args` (without `arena`), called when a T is allocated on the heap;
204 //   and a constructor callable with `Arena* arena, Args&&... args`, called when
205 //   a T is allocated on an arena. If the second constructor is called with a
206 //   NULL arena pointer, it must be equivalent to invoking the first
207 //   (`args`-only) constructor.
208 //
209 // - The type T must have a particular type trait: a nested type
210 //   |InternalArenaConstructable_|. This is usually a typedef to |void|. If no
211 //   such type trait exists, then the instantiation CreateMessage<T> will fail
212 //   to compile.
213 //
214 // - The type T *may* have the type trait |DestructorSkippable_|. If this type
215 //   trait is present in the type, then its destructor will not be called if and
216 //   only if it was passed a non-NULL arena pointer. If this type trait is not
217 //   present on the type, then its destructor is always called when the
218 //   containing arena is destroyed.
219 //
220 // This protocol is implemented by all arena-enabled proto2 message classes as
221 // well as protobuf container types like RepeatedPtrField and Map. The protocol
222 // is internal to protobuf and is not guaranteed to be stable. Non-proto types
223 // should not rely on this protocol.
224 class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
225  public:
226   // Default constructor with sensible default options, tuned for average
227   // use-cases.
Arena()228   inline Arena() : impl_() {}
229 
230   // Construct an arena with default options, except for the supplied
231   // initial block. It is more efficient to use this constructor
232   // instead of passing ArenaOptions if the only configuration needed
233   // by the caller is supplying an initial block.
Arena(char * initial_block,size_t initial_block_size)234   inline Arena(char* initial_block, size_t initial_block_size)
235       : impl_(initial_block, initial_block_size) {}
236 
237   // Arena constructor taking custom options. See ArenaOptions above for
238   // descriptions of the options available.
Arena(const ArenaOptions & options)239   explicit Arena(const ArenaOptions& options) : impl_(options) {}
240 
241   // Block overhead.  Use this as a guide for how much to over-allocate the
242   // initial block if you want an allocation of size N to fit inside it.
243   //
244   // WARNING: if you allocate multiple objects, it is difficult to guarantee
245   // that a series of allocations will fit in the initial block, especially if
246   // Arena changes its alignment guarantees in the future!
247   static const size_t kBlockOverhead = internal::ArenaImpl::kBlockHeaderSize +
248                                        internal::ArenaImpl::kSerialArenaSize;
249 
~Arena()250   inline ~Arena() {}
251 
252   // TODO(protobuf-team): Fix callers to use constructor and delete this method.
Init(const ArenaOptions &)253   void Init(const ArenaOptions&) {}
254 
255   // API to create proto2 message objects on the arena. If the arena passed in
256   // is NULL, then a heap allocated object is returned. Type T must be a message
257   // defined in a .proto file with cc_enable_arenas set to true, otherwise a
258   // compilation error will occur.
259   //
260   // RepeatedField and RepeatedPtrField may also be instantiated directly on an
261   // arena with this method.
262   //
263   // This function also accepts any type T that satisfies the arena message
264   // allocation protocol, documented above.
265   template <typename T, typename... Args>
CreateMessage(Arena * arena,Args &&...args)266   PROTOBUF_ALWAYS_INLINE static T* CreateMessage(Arena* arena, Args&&... args) {
267     static_assert(
268         InternalHelper<T>::is_arena_constructable::value,
269         "CreateMessage can only construct types that are ArenaConstructable");
270     // We must delegate to CreateMaybeMessage() and NOT CreateMessageInternal()
271     // because protobuf generated classes specialize CreateMaybeMessage() and we
272     // need to use that specialization for code size reasons.
273     return Arena::CreateMaybeMessage<T>(arena, std::forward<Args>(args)...);
274   }
275 
276   // API to create any objects on the arena. Note that only the object will
277   // be created on the arena; the underlying ptrs (in case of a proto2 message)
278   // will be still heap allocated. Proto messages should usually be allocated
279   // with CreateMessage<T>() instead.
280   //
281   // Note that even if T satisfies the arena message construction protocol
282   // (InternalArenaConstructable_ trait and optional DestructorSkippable_
283   // trait), as described above, this function does not follow the protocol;
284   // instead, it treats T as a black-box type, just as if it did not have these
285   // traits. Specifically, T's constructor arguments will always be only those
286   // passed to Create<T>() -- no additional arena pointer is implicitly added.
287   // Furthermore, the destructor will always be called at arena destruction time
288   // (unless the destructor is trivial). Hence, from T's point of view, it is as
289   // if the object were allocated on the heap (except that the underlying memory
290   // is obtained from the arena).
291   template <typename T, typename... Args>
Create(Arena * arena,Args &&...args)292   PROTOBUF_ALWAYS_INLINE static T* Create(Arena* arena, Args&&... args) {
293     return CreateNoMessage<T>(arena, is_arena_constructable<T>(),
294                               std::forward<Args>(args)...);
295   }
296 
297   // Create an array of object type T on the arena *without* invoking the
298   // constructor of T. If `arena` is null, then the return value should be freed
299   // with `delete[] x;` (or `::operator delete[](x);`).
300   // To ensure safe uses, this function checks at compile time
301   // (when compiled as C++11) that T is trivially default-constructible and
302   // trivially destructible.
303   template <typename T>
CreateArray(Arena * arena,size_t num_elements)304   PROTOBUF_ALWAYS_INLINE static T* CreateArray(Arena* arena,
305                                                size_t num_elements) {
306     static_assert(std::is_pod<T>::value,
307                   "CreateArray requires a trivially constructible type");
308     static_assert(std::is_trivially_destructible<T>::value,
309                   "CreateArray requires a trivially destructible type");
310     GOOGLE_CHECK_LE(num_elements, std::numeric_limits<size_t>::max() / sizeof(T))
311         << "Requested size is too large to fit into size_t.";
312     if (arena == NULL) {
313       return static_cast<T*>(::operator new[](num_elements * sizeof(T)));
314     } else {
315       return arena->CreateInternalRawArray<T>(num_elements);
316     }
317   }
318 
319   // Returns the total space allocated by the arena, which is the sum of the
320   // sizes of the underlying blocks. This method is relatively fast; a counter
321   // is kept as blocks are allocated.
SpaceAllocated()322   uint64 SpaceAllocated() const { return impl_.SpaceAllocated(); }
323   // Returns the total space used by the arena. Similar to SpaceAllocated but
324   // does not include free space and block overhead. The total space returned
325   // may not include space used by other threads executing concurrently with
326   // the call to this method.
SpaceUsed()327   uint64 SpaceUsed() const { return impl_.SpaceUsed(); }
328 
329   // Frees all storage allocated by this arena after calling destructors
330   // registered with OwnDestructor() and freeing objects registered with Own().
331   // Any objects allocated on this arena are unusable after this call. It also
332   // returns the total space used by the arena which is the sums of the sizes
333   // of the allocated blocks. This method is not thread-safe.
Reset()334   uint64 Reset() { return impl_.Reset(); }
335 
336   // Adds |object| to a list of heap-allocated objects to be freed with |delete|
337   // when the arena is destroyed or reset.
338   template <typename T>
Own(T * object)339   PROTOBUF_NOINLINE void Own(T* object) {
340     OwnInternal(object, std::is_convertible<T*, Message*>());
341   }
342 
343   // Adds |object| to a list of objects whose destructors will be manually
344   // called when the arena is destroyed or reset. This differs from Own() in
345   // that it does not free the underlying memory with |delete|; hence, it is
346   // normally only used for objects that are placement-newed into
347   // arena-allocated memory.
348   template <typename T>
OwnDestructor(T * object)349   PROTOBUF_NOINLINE void OwnDestructor(T* object) {
350     if (object != NULL) {
351       impl_.AddCleanup(object, &internal::arena_destruct_object<T>);
352     }
353   }
354 
355   // Adds a custom member function on an object to the list of destructors that
356   // will be manually called when the arena is destroyed or reset. This differs
357   // from OwnDestructor() in that any member function may be specified, not only
358   // the class destructor.
OwnCustomDestructor(void * object,void (* destruct)(void *))359   PROTOBUF_NOINLINE void OwnCustomDestructor(void* object,
360                                              void (*destruct)(void*)) {
361     impl_.AddCleanup(object, destruct);
362   }
363 
364   // Retrieves the arena associated with |value| if |value| is an arena-capable
365   // message, or NULL otherwise. If possible, the call resolves at compile time.
366   // Note that we can often devirtualize calls to `value->GetArena()` so usually
367   // calling this method is unnecessary.
368   template <typename T>
GetArena(const T * value)369   PROTOBUF_ALWAYS_INLINE static Arena* GetArena(const T* value) {
370     return GetArenaInternal(value);
371   }
372 
373   template <typename T>
374   class InternalHelper {
375     template <typename U>
376     static char DestructorSkippable(const typename U::DestructorSkippable_*);
377     template <typename U>
378     static double DestructorSkippable(...);
379 
380     typedef std::integral_constant<
381         bool, sizeof(DestructorSkippable<T>(static_cast<const T*>(0))) ==
382                       sizeof(char) ||
383                   std::is_trivially_destructible<T>::value>
384         is_destructor_skippable;
385 
386     template <typename U>
387     static char ArenaConstructable(
388         const typename U::InternalArenaConstructable_*);
389     template <typename U>
390     static double ArenaConstructable(...);
391 
392     typedef std::integral_constant<bool, sizeof(ArenaConstructable<T>(
393                                              static_cast<const T*>(0))) ==
394                                              sizeof(char)>
395         is_arena_constructable;
396 
397     template <typename U,
398               typename std::enable_if<
399                   std::is_same<Arena*, decltype(std::declval<const U>()
400                                                     .GetArena())>::value,
401                   int>::type = 0>
402     static char HasGetArena(decltype(&U::GetArena));
403     template <typename U>
404     static double HasGetArena(...);
405 
406     typedef std::integral_constant<bool, sizeof(HasGetArena<T>(nullptr)) ==
407                                              sizeof(char)>
408         has_get_arena;
409 
410     template <typename... Args>
Construct(void * ptr,Args &&...args)411     static T* Construct(void* ptr, Args&&... args) {
412       return new (ptr) T(std::forward<Args>(args)...);
413     }
414 
GetArena(const T * p)415     static Arena* GetArena(const T* p) { return p->GetArena(); }
416 
417     friend class Arena;
418   };
419 
420   // Helper typetraits that indicates support for arenas in a type T at compile
421   // time. This is public only to allow construction of higher-level templated
422   // utilities.
423   //
424   // is_arena_constructable<T>::value is true if the message type T has arena
425   // support enabled, and false otherwise.
426   //
427   // is_destructor_skippable<T>::value is true if the message type T has told
428   // the arena that it is safe to skip the destructor, and false otherwise.
429   //
430   // This is inside Arena because only Arena has the friend relationships
431   // necessary to see the underlying generated code traits.
432   template <typename T>
433   struct is_arena_constructable : InternalHelper<T>::is_arena_constructable {};
434   template <typename T>
435   struct is_destructor_skippable : InternalHelper<T>::is_destructor_skippable {
436   };
437 
438  private:
439   template <typename T>
440   struct has_get_arena : InternalHelper<T>::has_get_arena {};
441 
442   template <typename T, typename... Args>
CreateMessageInternal(Arena * arena,Args &&...args)443   PROTOBUF_ALWAYS_INLINE static T* CreateMessageInternal(Arena* arena,
444                                                          Args&&... args) {
445     static_assert(
446         InternalHelper<T>::is_arena_constructable::value,
447         "CreateMessage can only construct types that are ArenaConstructable");
448     if (arena == NULL) {
449       return new T(nullptr, std::forward<Args>(args)...);
450     } else {
451       return arena->DoCreateMessage<T>(std::forward<Args>(args)...);
452     }
453   }
454 
455   // This specialization for no arguments is necessary, because its behavior is
456   // slightly different.  When the arena pointer is nullptr, it calls T()
457   // instead of T(nullptr).
458   template <typename T>
CreateMessageInternal(Arena * arena)459   PROTOBUF_ALWAYS_INLINE static T* CreateMessageInternal(Arena* arena) {
460     static_assert(
461         InternalHelper<T>::is_arena_constructable::value,
462         "CreateMessage can only construct types that are ArenaConstructable");
463     if (arena == NULL) {
464       return new T();
465     } else {
466       return arena->DoCreateMessage<T>();
467     }
468   }
469 
470   template <typename T, typename... Args>
CreateInternal(Arena * arena,Args &&...args)471   PROTOBUF_ALWAYS_INLINE static T* CreateInternal(Arena* arena,
472                                                   Args&&... args) {
473     if (arena == NULL) {
474       return new T(std::forward<Args>(args)...);
475     } else {
476       return arena->DoCreate<T>(std::is_trivially_destructible<T>::value,
477                                 std::forward<Args>(args)...);
478     }
479   }
480 
AllocHook(const std::type_info * allocated_type,size_t n)481   inline void AllocHook(const std::type_info* allocated_type, size_t n) const {
482     impl_.RecordAlloc(allocated_type, n);
483   }
484 
485   // Allocate and also optionally call collector with the allocated type info
486   // when allocation recording is enabled.
487   template <typename T>
AllocateInternal(bool skip_explicit_ownership)488   PROTOBUF_ALWAYS_INLINE void* AllocateInternal(bool skip_explicit_ownership) {
489     const size_t n = internal::AlignUpTo8(sizeof(T));
490     // Monitor allocation if needed.
491     impl_.RecordAlloc(RTTI_TYPE_ID(T), n);
492     if (skip_explicit_ownership) {
493       return AllocateAlignedTo<alignof(T)>(sizeof(T));
494     } else {
495       if (alignof(T) <= 8) {
496         return impl_.AllocateAlignedAndAddCleanup(
497             n, &internal::arena_destruct_object<T>);
498       } else {
499         auto ptr =
500             reinterpret_cast<uintptr_t>(impl_.AllocateAlignedAndAddCleanup(
501                 sizeof(T) + alignof(T) - 8,
502                 &internal::arena_destruct_object<T>));
503         return reinterpret_cast<void*>((ptr + alignof(T) - 8) &
504                                        (~alignof(T) + 1));
505       }
506     }
507   }
508 
509   // CreateMessage<T> requires that T supports arenas, but this private method
510   // works whether or not T supports arenas. These are not exposed to user code
511   // as it can cause confusing API usages, and end up having double free in
512   // user code. These are used only internally from LazyField and Repeated
513   // fields, since they are designed to work in all mode combinations.
514   template <typename Msg, typename... Args>
DoCreateMaybeMessage(Arena * arena,std::true_type,Args &&...args)515   PROTOBUF_ALWAYS_INLINE static Msg* DoCreateMaybeMessage(Arena* arena,
516                                                           std::true_type,
517                                                           Args&&... args) {
518     return CreateMessageInternal<Msg>(arena, std::forward<Args>(args)...);
519   }
520 
521   template <typename T, typename... Args>
DoCreateMaybeMessage(Arena * arena,std::false_type,Args &&...args)522   PROTOBUF_ALWAYS_INLINE static T* DoCreateMaybeMessage(Arena* arena,
523                                                         std::false_type,
524                                                         Args&&... args) {
525     return CreateInternal<T>(arena, std::forward<Args>(args)...);
526   }
527 
528   template <typename T, typename... Args>
CreateMaybeMessage(Arena * arena,Args &&...args)529   PROTOBUF_ALWAYS_INLINE static T* CreateMaybeMessage(Arena* arena,
530                                                       Args&&... args) {
531     return DoCreateMaybeMessage<T>(arena, is_arena_constructable<T>(),
532                                    std::forward<Args>(args)...);
533   }
534 
535   template <typename T, typename... Args>
CreateNoMessage(Arena * arena,std::true_type,Args &&...args)536   PROTOBUF_ALWAYS_INLINE static T* CreateNoMessage(Arena* arena, std::true_type,
537                                                    Args&&... args) {
538     // User is constructing with Create() despite the fact that T supports arena
539     // construction.  In this case we have to delegate to CreateInternal(), and
540     // we can't use any CreateMaybeMessage() specialization that may be defined.
541     return CreateInternal<T>(arena, std::forward<Args>(args)...);
542   }
543 
544   template <typename T, typename... Args>
CreateNoMessage(Arena * arena,std::false_type,Args &&...args)545   PROTOBUF_ALWAYS_INLINE static T* CreateNoMessage(Arena* arena,
546                                                    std::false_type,
547                                                    Args&&... args) {
548     // User is constructing with Create() and the type does not support arena
549     // construction.  In this case we can delegate to CreateMaybeMessage() and
550     // use any specialization that may be available for that.
551     return CreateMaybeMessage<T>(arena, std::forward<Args>(args)...);
552   }
553 
554   // Just allocate the required size for the given type assuming the
555   // type has a trivial constructor.
556   template <typename T>
CreateInternalRawArray(size_t num_elements)557   PROTOBUF_ALWAYS_INLINE T* CreateInternalRawArray(size_t num_elements) {
558     GOOGLE_CHECK_LE(num_elements, std::numeric_limits<size_t>::max() / sizeof(T))
559         << "Requested size is too large to fit into size_t.";
560     // We count on compiler to realize that if sizeof(T) is a multiple of
561     // 8 AlignUpTo can be elided.
562     const size_t n = internal::AlignUpTo8(sizeof(T) * num_elements);
563     // Monitor allocation if needed.
564     impl_.RecordAlloc(RTTI_TYPE_ID(T), n);
565     return static_cast<T*>(AllocateAlignedTo<alignof(T)>(n));
566   }
567 
568   template <typename T, typename... Args>
DoCreate(bool skip_explicit_ownership,Args &&...args)569   PROTOBUF_ALWAYS_INLINE T* DoCreate(bool skip_explicit_ownership,
570                                      Args&&... args) {
571     return new (AllocateInternal<T>(skip_explicit_ownership))
572         T(std::forward<Args>(args)...);
573   }
574   template <typename T, typename... Args>
DoCreateMessage(Args &&...args)575   PROTOBUF_ALWAYS_INLINE T* DoCreateMessage(Args&&... args) {
576     return InternalHelper<T>::Construct(
577         AllocateInternal<T>(InternalHelper<T>::is_destructor_skippable::value),
578         this, std::forward<Args>(args)...);
579   }
580 
581   // CreateInArenaStorage is used to implement map field. Without it,
582   // Map need to call generated message's protected arena constructor,
583   // which needs to declare Map as friend of generated message.
584   template <typename T, typename... Args>
CreateInArenaStorage(T * ptr,Arena * arena,Args &&...args)585   static void CreateInArenaStorage(T* ptr, Arena* arena, Args&&... args) {
586     CreateInArenaStorageInternal(ptr, arena,
587                                  typename is_arena_constructable<T>::type(),
588                                  std::forward<Args>(args)...);
589     RegisterDestructorInternal(
590         ptr, arena,
591         typename InternalHelper<T>::is_destructor_skippable::type());
592   }
593 
594   template <typename T, typename... Args>
CreateInArenaStorageInternal(T * ptr,Arena * arena,std::true_type,Args &&...args)595   static void CreateInArenaStorageInternal(T* ptr, Arena* arena,
596                                            std::true_type, Args&&... args) {
597     InternalHelper<T>::Construct(ptr, arena, std::forward<Args>(args)...);
598   }
599   template <typename T, typename... Args>
CreateInArenaStorageInternal(T * ptr,Arena *,std::false_type,Args &&...args)600   static void CreateInArenaStorageInternal(T* ptr, Arena* /* arena */,
601                                            std::false_type, Args&&... args) {
602     new (ptr) T(std::forward<Args>(args)...);
603   }
604 
605   template <typename T>
RegisterDestructorInternal(T *,Arena *,std::true_type)606   static void RegisterDestructorInternal(T* /* ptr */, Arena* /* arena */,
607                                          std::true_type) {}
608   template <typename T>
RegisterDestructorInternal(T * ptr,Arena * arena,std::false_type)609   static void RegisterDestructorInternal(T* ptr, Arena* arena,
610                                          std::false_type) {
611     arena->OwnDestructor(ptr);
612   }
613 
614   // These implement Own(), which registers an object for deletion (destructor
615   // call and operator delete()). The second parameter has type 'true_type' if T
616   // is a subtype of Message and 'false_type' otherwise. Collapsing
617   // all template instantiations to one for generic Message reduces code size,
618   // using the virtual destructor instead.
619   template <typename T>
OwnInternal(T * object,std::true_type)620   PROTOBUF_ALWAYS_INLINE void OwnInternal(T* object, std::true_type) {
621     if (object != NULL) {
622       impl_.AddCleanup(object, &internal::arena_delete_object<Message>);
623     }
624   }
625   template <typename T>
OwnInternal(T * object,std::false_type)626   PROTOBUF_ALWAYS_INLINE void OwnInternal(T* object, std::false_type) {
627     if (object != NULL) {
628       impl_.AddCleanup(object, &internal::arena_delete_object<T>);
629     }
630   }
631 
632   // Implementation for GetArena(). Only message objects with
633   // InternalArenaConstructable_ tags can be associated with an arena, and such
634   // objects must implement a GetArena() method.
635   template <typename T, typename std::enable_if<
636                             is_arena_constructable<T>::value, int>::type = 0>
GetArenaInternal(const T * value)637   PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) {
638     return InternalHelper<T>::GetArena(value);
639   }
640   template <typename T,
641             typename std::enable_if<!is_arena_constructable<T>::value &&
642                                         has_get_arena<T>::value,
643                                     int>::type = 0>
GetArenaInternal(const T * value)644   PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) {
645     return value->GetArena();
646   }
647   template <typename T,
648             typename std::enable_if<!is_arena_constructable<T>::value &&
649                                         !has_get_arena<T>::value,
650                                     int>::type = 0>
GetArenaInternal(const T * value)651   PROTOBUF_ALWAYS_INLINE static Arena* GetArenaInternal(const T* value) {
652     (void)value;
653     return nullptr;
654   }
655 
656   // For friends of arena.
AllocateAligned(size_t n)657   void* AllocateAligned(size_t n) {
658     return AllocateAlignedNoHook(internal::AlignUpTo8(n));
659   }
660   template<size_t Align>
AllocateAlignedTo(size_t n)661   void* AllocateAlignedTo(size_t n) {
662     static_assert(Align > 0, "Alignment must be greater than 0");
663     static_assert((Align & (Align - 1)) == 0, "Alignment must be power of two");
664     if (Align <= 8) return AllocateAligned(n);
665     // TODO(b/151247138): if the pointer would have been aligned already,
666     // this is wasting space. We should pass the alignment down.
667     uintptr_t ptr = reinterpret_cast<uintptr_t>(AllocateAligned(n + Align - 8));
668     ptr = (ptr + Align - 1) & (~Align + 1);
669     return reinterpret_cast<void*>(ptr);
670   }
671 
672   void* AllocateAlignedNoHook(size_t n);
673 
674   internal::ArenaImpl impl_;
675 
676   template <typename Type>
677   friend class internal::GenericTypeHandler;
678   friend struct internal::ArenaStringPtr;  // For AllocateAligned.
679   friend class internal::LazyField;        // For CreateMaybeMessage.
680   friend class internal::EpsCopyInputStream;  // For parser performance
681   friend class MessageLite;
682   template <typename Key, typename T>
683   friend class Map;
684 };
685 
686 // Defined above for supporting environments without RTTI.
687 #undef RTTI_TYPE_ID
688 
689 }  // namespace protobuf
690 }  // namespace google
691 
692 #include <google/protobuf/port_undef.inc>
693 
694 #endif  // GOOGLE_PROTOBUF_ARENA_H__
695