2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_MEMORY_H_
18 #define FOLLY_MEMORY_H_
20 #include <folly/Traits.h>
33 * For exception safety and consistency with make_shared. Erase me when
34 * we have std::make_unique().
36 * @author Louis Brandy (ldbrandy@fb.com)
37 * @author Xu Ning (xning@fb.com)
40 template<typename T, typename Dp = std::default_delete<T>, typename... Args>
41 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T, Dp>>::type
42 make_unique(Args&&... args) {
43 return std::unique_ptr<T, Dp>(new T(std::forward<Args>(args)...));
46 // Allows 'make_unique<T[]>(10)'. (N3690 s20.9.1.4 p3-4)
47 template<typename T, typename Dp = std::default_delete<T>>
48 typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T, Dp>>::type
49 make_unique(const size_t n) {
50 return std::unique_ptr<T, Dp>(new typename std::remove_extent<T>::type[n]());
53 // Disallows 'make_unique<T[10]>()'. (N3690 s20.9.1.4 p5)
54 template<typename T, typename Dp = std::default_delete<T>, typename... Args>
55 typename std::enable_if<
56 std::extent<T>::value != 0, std::unique_ptr<T, Dp>>::type
57 make_unique(Args&&...) = delete;
60 * A SimpleAllocator must provide two methods:
62 * void* allocate(size_t size);
63 * void deallocate(void* ptr);
65 * which, respectively, allocate a block of size bytes (aligned to the
66 * maximum alignment required on your system), throwing std::bad_alloc
67 * if the allocation can't be satisfied, and free a previously
70 * SysAlloc resembles the standard allocator.
74 void* allocate(size_t size) {
75 void* p = ::malloc(size);
76 if (!p) throw std::bad_alloc();
79 void deallocate(void* p) {
85 * StlAllocator wraps a SimpleAllocator into a STL-compliant
86 * allocator, maintaining an instance pointer to the simple allocator
87 * object. The underlying SimpleAllocator object must outlive all
88 * instances of StlAllocator using it.
90 * But note that if you pass StlAllocator<MallocAllocator,...> to a
91 * standard container it will be larger due to the contained state
94 * @author: Tudor Bosman <tudorb@fb.com>
97 // This would be so much simpler with std::allocator_traits, but gcc 4.6.2
98 // doesn't support it.
99 template <class Alloc, class T> class StlAllocator;
101 template <class Alloc> class StlAllocator<Alloc, void> {
103 typedef void value_type;
104 typedef void* pointer;
105 typedef const void* const_pointer;
107 StlAllocator() : alloc_(nullptr) { }
108 explicit StlAllocator(Alloc* a) : alloc_(a) { }
110 Alloc* alloc() const {
114 template <class U> struct rebind {
115 typedef StlAllocator<Alloc, U> other;
118 bool operator!=(const StlAllocator<Alloc, void>& other) const {
119 return alloc_ != other.alloc_;
122 bool operator==(const StlAllocator<Alloc, void>& other) const {
123 return alloc_ == other.alloc_;
130 template <class Alloc, class T>
133 typedef T value_type;
135 typedef const T* const_pointer;
136 typedef T& reference;
137 typedef const T& const_reference;
139 typedef ptrdiff_t difference_type;
140 typedef size_t size_type;
142 StlAllocator() : alloc_(nullptr) { }
143 explicit StlAllocator(Alloc* a) : alloc_(a) { }
145 template <class U> StlAllocator(const StlAllocator<Alloc, U>& other)
146 : alloc_(other.alloc()) { }
148 T* allocate(size_t n, const void* hint = nullptr) {
149 return static_cast<T*>(alloc_->allocate(n * sizeof(T)));
152 void deallocate(T* p, size_t n) {
153 alloc_->deallocate(p);
156 size_t max_size() const {
157 return std::numeric_limits<size_t>::max();
160 T* address(T& x) const {
161 return std::addressof(x);
164 const T* address(const T& x) const {
165 return std::addressof(x);
168 template <class... Args>
169 void construct(T* p, Args&&... args) {
170 new (p) T(std::forward<Args>(args)...);
177 Alloc* alloc() const {
181 template <class U> struct rebind {
182 typedef StlAllocator<Alloc, U> other;
185 bool operator!=(const StlAllocator<Alloc, T>& other) const {
186 return alloc_ != other.alloc_;
189 bool operator==(const StlAllocator<Alloc, T>& other) const {
190 return alloc_ == other.alloc_;
198 * Helper function to obtain rebound allocators
200 * @author: Marcelo Juchem <marcelo@fb.com>
202 template <typename T, typename Allocator>
203 typename Allocator::template rebind<T>::other rebind_allocator(
204 Allocator const& allocator
206 return typename Allocator::template rebind<T>::other(allocator);
210 * Helper classes/functions for creating a unique_ptr using a custom
213 * @author: Marcelo Juchem <marcelo@fb.com>
216 // Derives from the allocator to take advantage of the empty base
217 // optimization when possible.
218 template <typename Allocator>
219 class allocator_delete
220 : private std::remove_reference<Allocator>::type
222 typedef typename std::remove_reference<Allocator>::type allocator_type;
225 typedef typename Allocator::pointer pointer;
227 allocator_delete() = default;
229 explicit allocator_delete(const allocator_type& allocator)
230 : allocator_type(allocator)
233 explicit allocator_delete(allocator_type&& allocator)
234 : allocator_type(std::move(allocator))
237 template <typename U>
238 allocator_delete(const allocator_delete<U>& other)
239 : allocator_type(other.get_allocator())
242 allocator_type& get_allocator() const {
243 return *const_cast<allocator_delete*>(this);
246 void operator()(pointer p) const {
248 const_cast<allocator_delete*>(this)->destroy(p);
249 const_cast<allocator_delete*>(this)->deallocate(p, 1);
253 template <typename T, typename Allocator>
254 class is_simple_allocator {
255 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_destroy, destroy);
257 typedef typename std::remove_const<
258 typename std::remove_reference<Allocator>::type
260 typedef typename std::remove_reference<T>::type value_type;
261 typedef value_type* pointer;
264 constexpr static bool value = !has_destroy<allocator, void(pointer)>::value
265 && !has_destroy<allocator, void(void*)>::value;
268 template <typename T, typename Allocator>
269 struct as_stl_allocator {
270 typedef typename std::conditional<
271 is_simple_allocator<T, Allocator>::value,
273 typename std::remove_reference<Allocator>::type,
274 typename std::remove_reference<T>::type
276 typename std::remove_reference<Allocator>::type
280 template <typename T, typename Allocator>
281 typename std::enable_if<
282 is_simple_allocator<T, Allocator>::value,
284 typename std::remove_reference<Allocator>::type,
285 typename std::remove_reference<T>::type
287 >::type make_stl_allocator(Allocator&& allocator) {
288 return folly::StlAllocator<
289 typename std::remove_reference<Allocator>::type,
290 typename std::remove_reference<T>::type
294 template <typename T, typename Allocator>
295 typename std::enable_if<
296 !is_simple_allocator<T, Allocator>::value,
297 typename std::remove_reference<Allocator>::type
298 >::type make_stl_allocator(Allocator&& allocator) {
299 return std::move(allocator);
303 * AllocatorUniquePtr: a unique_ptr that supports both STL-style
304 * allocators and SimpleAllocator
306 * @author: Marcelo Juchem <marcelo@fb.com>
309 template <typename T, typename Allocator>
310 struct AllocatorUniquePtr {
311 typedef std::unique_ptr<T,
312 folly::allocator_delete<
313 typename std::conditional<
314 is_simple_allocator<T, Allocator>::value,
315 folly::StlAllocator<typename std::remove_reference<Allocator>::type, T>,
316 typename std::remove_reference<Allocator>::type
323 * Functions to allocate a unique_ptr / shared_ptr, supporting both
324 * STL-style allocators and SimpleAllocator, analog to std::allocate_shared
326 * @author: Marcelo Juchem <marcelo@fb.com>
329 template <typename T, typename Allocator, typename ...Args>
330 typename AllocatorUniquePtr<T, Allocator>::type allocate_unique(
331 Allocator&& allocator, Args&&... args
333 auto stlAllocator = folly::make_stl_allocator<T>(
334 std::forward<Allocator>(allocator)
336 auto p = stlAllocator.allocate(1);
339 stlAllocator.construct(p, std::forward<Args>(args)...);
342 folly::allocator_delete<decltype(stlAllocator)>(std::move(stlAllocator))
345 stlAllocator.deallocate(p, 1);
350 template <typename T, typename Allocator, typename ...Args>
351 std::shared_ptr<T> allocate_shared(Allocator&& allocator, Args&&... args) {
352 return std::allocate_shared<T>(
353 folly::make_stl_allocator<T>(std::forward<Allocator>(allocator)),
354 std::forward<Args>(args)...
359 * IsArenaAllocator<T>::value describes whether SimpleAllocator has
360 * no-op deallocate().
362 template <class T> struct IsArenaAllocator : std::false_type { };
366 #endif /* FOLLY_MEMORY_H_ */