Remove Stream
[folly.git] / folly / Arena.h
1 /*
2  * Copyright 2012 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 #ifndef FOLLY_ARENA_H_
18 #define FOLLY_ARENA_H_
19
20 #include <cassert>
21 #include <utility>
22 #include <limits>
23 #include <boost/intrusive/slist.hpp>
24
25 #include "folly/Likely.h"
26 #include "folly/Malloc.h"
27
28 namespace folly {
29
30 /**
31  * Simple arena: allocate memory which gets freed when the arena gets
32  * destroyed.
33  *
34  * The arena itself allocates memory using a custom allocator which provides
35  * the following interface (same as required by StlAllocator in StlAllocator.h)
36  *
37  *   void* allocate(size_t size);
38  *      Allocate a block of size bytes, properly aligned to the maximum
39  *      alignment required on your system; throw std::bad_alloc if the
40  *      allocation can't be satisfied.
41  *
42  *   void deallocate(void* ptr);
43  *      Deallocate a previously allocated block.
44  *
45  * You may also specialize ArenaAllocatorTraits for your allocator type to
46  * provide:
47  *
48  *   size_t goodSize(const Allocator& alloc, size_t size) const;
49  *      Return a size (>= the provided size) that is considered "good" for your
50  *      allocator (for example, if your allocator allocates memory in 4MB
51  *      chunks, size should be rounded up to 4MB).  The provided value is
52  *      guaranteed to be rounded up to a multiple of the maximum alignment
53  *      required on your system; the returned value must be also.
54  *
55  * An implementation that uses malloc() / free() is defined below, see
56  * SysAlloc / SysArena.
57  */
58 template <class Alloc> struct ArenaAllocatorTraits;
59 template <class Alloc>
60 class Arena {
61  public:
62   explicit Arena(const Alloc& alloc,
63                  size_t minBlockSize = kDefaultMinBlockSize)
64     : allocAndSize_(alloc, minBlockSize),
65       ptr_(nullptr),
66       end_(nullptr),
67       totalAllocatedSize_(0) {
68   }
69
70   ~Arena();
71
72   void* allocate(size_t size) {
73     size = roundUp(size);
74
75     if (LIKELY(end_ - ptr_ >= size)) {
76       // Fast path: there's enough room in the current block
77       char* r = ptr_;
78       ptr_ += size;
79       assert(isAligned(r));
80       return r;
81     }
82
83     // Not enough room in the current block
84     void* r = allocateSlow(size);
85     assert(isAligned(r));
86     return r;
87   }
88
89   void deallocate(void* p) {
90     // Deallocate? Never!
91   }
92
93   // Transfer ownership of all memory allocated from "other" to "this".
94   void merge(Arena&& other);
95
96   // Gets the total memory used by the arena
97   size_t totalSize() const {
98     return totalAllocatedSize_ + sizeof(Arena);
99   }
100
101  private:
102   // not copyable
103   Arena(const Arena&) = delete;
104   Arena& operator=(const Arena&) = delete;
105
106   // movable
107   Arena(Arena&&) = default;
108   Arena& operator=(Arena&&) = default;
109
110   struct Block;
111   typedef boost::intrusive::slist_member_hook<
112     boost::intrusive::tag<Arena>> BlockLink;
113
114   struct Block {
115     BlockLink link;
116
117     // Allocate a block with at least size bytes of storage.
118     // If allowSlack is true, allocate more than size bytes if convenient
119     // (via ArenaAllocatorTraits::goodSize()) as we'll try to pack small
120     // allocations in this block.
121     static std::pair<Block*, size_t> allocate(
122         Alloc& alloc, size_t size, bool allowSlack);
123     void deallocate(Alloc& alloc);
124
125     char* start() {
126       return reinterpret_cast<char*>(this + 1);
127     }
128
129    private:
130     Block() { }
131     ~Block() { }
132   } __attribute__((aligned));
133   // This should be alignas(std::max_align_t) but neither alignas nor
134   // max_align_t are supported by gcc 4.6.2.
135
136  public:
137   static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block);
138
139  private:
140   static constexpr size_t maxAlign = alignof(Block);
141   static constexpr bool isAligned(uintptr_t address) {
142     return (address & (maxAlign - 1)) == 0;
143   }
144   static bool isAligned(void* p) {
145     return isAligned(reinterpret_cast<uintptr_t>(p));
146   }
147
148   // Round up size so it's properly aligned
149   static constexpr size_t roundUp(size_t size) {
150     return (size + maxAlign - 1) & ~(maxAlign - 1);
151   }
152
153   // cache_last<true> makes the list keep a pointer to the last element, so we
154   // have push_back() and constant time splice_after()
155   typedef boost::intrusive::slist<
156     Block,
157     boost::intrusive::member_hook<Block, BlockLink, &Block::link>,
158     boost::intrusive::constant_time_size<false>,
159     boost::intrusive::cache_last<true>> BlockList;
160
161   void* allocateSlow(size_t size);
162
163   // Empty member optimization: package Alloc with a non-empty member
164   // in case Alloc is empty (as it is in the case of SysAlloc).
165   struct AllocAndSize : public Alloc {
166     explicit AllocAndSize(const Alloc& a, size_t s)
167       : Alloc(a), minBlockSize(s) {
168     }
169
170     size_t minBlockSize;
171   };
172
173   size_t minBlockSize() const {
174     return allocAndSize_.minBlockSize;
175   }
176   Alloc& alloc() { return allocAndSize_; }
177   const Alloc& alloc() const { return allocAndSize_; }
178
179   AllocAndSize allocAndSize_;
180   BlockList blocks_;
181   char* ptr_;
182   char* end_;
183   size_t totalAllocatedSize_;
184 };
185
186 /**
187  * By default, don't pad the given size.
188  */
189 template <class Alloc>
190 struct ArenaAllocatorTraits {
191   static size_t goodSize(const Alloc& alloc, size_t size) {
192     return size;
193   }
194 };
195
196 /**
197  * Arena-compatible allocator that calls malloc() and free(); see
198  * goodMallocSize() in Malloc.h for goodSize().
199  */
200 class SysAlloc {
201  public:
202   void* allocate(size_t size) {
203     return checkedMalloc(size);
204   }
205
206   void deallocate(void* p) {
207     free(p);
208   }
209 };
210
211 template <>
212 struct ArenaAllocatorTraits<SysAlloc> {
213   static size_t goodSize(const SysAlloc& alloc, size_t size) {
214     return goodMallocSize(size);
215   }
216 };
217
218 /**
219  * Arena that uses the system allocator (malloc / free)
220  */
221 class SysArena : public Arena<SysAlloc> {
222  public:
223   explicit SysArena(size_t minBlockSize = kDefaultMinBlockSize)
224     : Arena<SysAlloc>(SysAlloc(), minBlockSize) {
225   }
226 };
227
228 }  // namespace folly
229
230 #include "folly/Arena-inl.h"
231
232 #endif /* FOLLY_ARENA_H_ */