cf010f3d4375d9f9c3700f010d8111f5f06ed300
[folly.git] / folly / Arena.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18 #define FOLLY_ARENA_H_
19
20 #include <cassert>
21 #include <limits>
22 #include <stdexcept>
23 #include <utility>
24
25 #include <boost/intrusive/slist.hpp>
26
27 #include <folly/Conv.h>
28 #include <folly/Likely.h>
29 #include <folly/Malloc.h>
30 #include <folly/Memory.h>
31
32 namespace folly {
33
34 /**
35  * Simple arena: allocate memory which gets freed when the arena gets
36  * destroyed.
37  *
38  * The arena itself allocates memory using a custom allocator which provides
39  * the following interface (same as required by StlAllocator in StlAllocator.h)
40  *
41  *   void* allocate(size_t size);
42  *      Allocate a block of size bytes, properly aligned to the maximum
43  *      alignment required on your system; throw std::bad_alloc if the
44  *      allocation can't be satisfied.
45  *
46  *   void deallocate(void* ptr);
47  *      Deallocate a previously allocated block.
48  *
49  * You may also specialize ArenaAllocatorTraits for your allocator type to
50  * provide:
51  *
52  *   size_t goodSize(const Allocator& alloc, size_t size) const;
53  *      Return a size (>= the provided size) that is considered "good" for your
54  *      allocator (for example, if your allocator allocates memory in 4MB
55  *      chunks, size should be rounded up to 4MB).  The provided value is
56  *      guaranteed to be rounded up to a multiple of the maximum alignment
57  *      required on your system; the returned value must be also.
58  *
59  * An implementation that uses malloc() / free() is defined below, see SysArena.
60  */
61 template <class Alloc> struct ArenaAllocatorTraits;
62 template <class Alloc>
63 class Arena {
64  public:
65   explicit Arena(const Alloc& alloc,
66                  size_t minBlockSize = kDefaultMinBlockSize,
67                  size_t sizeLimit = kNoSizeLimit,
68                  size_t maxAlign = kDefaultMaxAlign)
69     : allocAndSize_(alloc, minBlockSize)
70     , ptr_(nullptr)
71     , end_(nullptr)
72     , totalAllocatedSize_(0)
73     , bytesUsed_(0)
74     , sizeLimit_(sizeLimit)
75     , maxAlign_(maxAlign) {
76     if ((maxAlign_ & (maxAlign_ - 1)) || maxAlign_ > alignof(Block)) {
77       throw std::invalid_argument(
78           folly::to<std::string>("Invalid maxAlign: ", maxAlign_));
79     }
80   }
81
82   ~Arena();
83
84   void* allocate(size_t size) {
85     size = roundUp(size);
86     bytesUsed_ += size;
87
88     assert(ptr_ <= end_);
89     if (LIKELY((size_t)(end_ - ptr_) >= size)) {
90       // Fast path: there's enough room in the current block
91       char* r = ptr_;
92       ptr_ += size;
93       assert(isAligned(r));
94       return r;
95     }
96
97     // Not enough room in the current block
98     void* r = allocateSlow(size);
99     assert(isAligned(r));
100     return r;
101   }
102
103   void deallocate(void* /* p */) {
104     // Deallocate? Never!
105   }
106
107   // Transfer ownership of all memory allocated from "other" to "this".
108   void merge(Arena&& other);
109
110   // Gets the total memory used by the arena
111   size_t totalSize() const {
112     return totalAllocatedSize_ + sizeof(Arena);
113   }
114
115   // Gets the total number of "used" bytes, i.e. bytes that the arena users
116   // allocated via the calls to `allocate`. Doesn't include fragmentation, e.g.
117   // if block size is 4KB and you allocate 2 objects of 3KB in size,
118   // `bytesUsed()` will be 6KB, while `totalSize()` will be 8KB+.
119   size_t bytesUsed() const {
120     return bytesUsed_;
121   }
122
123   // not copyable
124   Arena(const Arena&) = delete;
125   Arena& operator=(const Arena&) = delete;
126
127   // movable
128   Arena(Arena&&) = default;
129   Arena& operator=(Arena&&) = default;
130
131  private:
132   struct Block;
133   typedef boost::intrusive::slist_member_hook<
134     boost::intrusive::tag<Arena>> BlockLink;
135
136   struct FOLLY_ALIGNED_MAX Block {
137     BlockLink link;
138
139     // Allocate a block with at least size bytes of storage.
140     // If allowSlack is true, allocate more than size bytes if convenient
141     // (via ArenaAllocatorTraits::goodSize()) as we'll try to pack small
142     // allocations in this block.
143     static std::pair<Block*, size_t> allocate(
144         Alloc& alloc, size_t size, bool allowSlack);
145     void deallocate(Alloc& alloc);
146
147     char* start() {
148       return reinterpret_cast<char*>(this + 1);
149     }
150
151    private:
152     Block() = default;
153     ~Block() = default;
154   };
155
156  public:
157   static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block);
158   static constexpr size_t kNoSizeLimit = 0;
159   static constexpr size_t kDefaultMaxAlign = alignof(Block);
160   static constexpr size_t kBlockOverhead = sizeof(Block);
161
162  private:
163   bool isAligned(uintptr_t address) const {
164     return (address & (maxAlign_ - 1)) == 0;
165   }
166   bool isAligned(void* p) const {
167     return isAligned(reinterpret_cast<uintptr_t>(p));
168   }
169
170   // Round up size so it's properly aligned
171   size_t roundUp(size_t size) const {
172     return (size + maxAlign_ - 1) & ~(maxAlign_ - 1);
173   }
174
175   // cache_last<true> makes the list keep a pointer to the last element, so we
176   // have push_back() and constant time splice_after()
177   typedef boost::intrusive::slist<
178     Block,
179     boost::intrusive::member_hook<Block, BlockLink, &Block::link>,
180     boost::intrusive::constant_time_size<false>,
181     boost::intrusive::cache_last<true>> BlockList;
182
183   void* allocateSlow(size_t size);
184
185   // Empty member optimization: package Alloc with a non-empty member
186   // in case Alloc is empty (as it is in the case of SysAlloc).
187   struct AllocAndSize : public Alloc {
188     explicit AllocAndSize(const Alloc& a, size_t s)
189       : Alloc(a), minBlockSize(s) {
190     }
191
192     size_t minBlockSize;
193   };
194
195   size_t minBlockSize() const {
196     return allocAndSize_.minBlockSize;
197   }
198   Alloc& alloc() { return allocAndSize_; }
199   const Alloc& alloc() const { return allocAndSize_; }
200
201   AllocAndSize allocAndSize_;
202   BlockList blocks_;
203   char* ptr_;
204   char* end_;
205   size_t totalAllocatedSize_;
206   size_t bytesUsed_;
207   const size_t sizeLimit_;
208   const size_t maxAlign_;
209 };
210
211 template <class Alloc>
212 struct IsArenaAllocator<Arena<Alloc>> : std::true_type { };
213
214 /**
215  * By default, don't pad the given size.
216  */
217 template <class Alloc>
218 struct ArenaAllocatorTraits {
219   static size_t goodSize(const Alloc& /* alloc */, size_t size) { return size; }
220 };
221
222 template <>
223 struct ArenaAllocatorTraits<SysAlloc> {
224   static size_t goodSize(const SysAlloc& /* alloc */, size_t size) {
225     return goodMallocSize(size);
226   }
227 };
228
229 /**
230  * Arena that uses the system allocator (malloc / free)
231  */
232 class SysArena : public Arena<SysAlloc> {
233  public:
234   explicit SysArena(size_t minBlockSize = kDefaultMinBlockSize,
235                     size_t sizeLimit = kNoSizeLimit,
236                     size_t maxAlign = kDefaultMaxAlign)
237     : Arena<SysAlloc>(SysAlloc(), minBlockSize, sizeLimit, maxAlign) {
238   }
239 };
240
241 template <>
242 struct IsArenaAllocator<SysArena> : std::true_type { };
243
244 }  // namespace folly
245
246 #include <folly/Arena-inl.h>