• 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_IMPL_H__
34 #define GOOGLE_PROTOBUF_ARENA_IMPL_H__
35 
36 #include <atomic>
37 #include <limits>
38 
39 #include <google/protobuf/stubs/common.h>
40 #include <google/protobuf/stubs/logging.h>
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 
AlignUpTo8(size_t n)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 {
69     size_t start_block_size;
70     size_t max_block_size;
71     char* initial_block;
72     size_t initial_block_size;
73     void* (*block_alloc)(size_t);
74     void (*block_dealloc)(void*, size_t);
75 
76     template <typename O>
OptionsOptions77     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>
ArenaImpl(const O & options)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 {
SizeOfCleanupChunk131     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 
AllocateAligned(size_t n)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 
AddCleanup(void * elem,void (* cleanup)(void *))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 
AllocateAlignedAndAddCleanup(size_t n,void (* cleanup)(void *))183     void* AllocateAlignedAndAddCleanup(size_t n, void (*cleanup)(void*)) {
184       void* ret = AllocateAligned(n);
185       AddCleanup(ret, cleanup);
186       return ret;
187     }
188 
owner()189     void* owner() const { return owner_; }
next()190     SerialArena* next() const { return next_; }
set_next(SerialArena * 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_.
211     CleanupNode* cleanup_ptr_;
212     CleanupNode* cleanup_limit_;
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 
Pointer(size_t n)221     char* Pointer(size_t n) {
222       GOOGLE_DCHECK(n <= size_);
223       return reinterpret_cast<char*>(this) + n;
224     }
225 
next()226     Block* next() const { return next_; }
pos()227     size_t pos() const { return pos_; }
size()228     size_t size() const { return size_; }
set_pos(size_t pos)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.
ThreadCacheThreadCache243     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.
248     LifecycleId last_lifecycle_id_seen;
249     SerialArena* last_serial_arena;
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
262   static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_;
thread_cache()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 
CacheSerialArena(SerialArena * serial)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 
299   Options options_;
300 
301   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArenaImpl);
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__
327