2 * Copyright 2013 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 std::unique_ptr<T, Dp> make_unique(Args&&... args) {
42 return std::unique_ptr<T, Dp>(new T(std::forward<Args>(args)...));
46 * StlAllocator wraps a SimpleAllocator into a STL-compliant
47 * allocator, maintaining an instance pointer to the simple allocator
48 * object. The underlying SimpleAllocator object must outlive all
49 * instances of StlAllocator using it.
51 * A SimpleAllocator must provide two methods:
53 * void* allocate(size_t size);
54 * void deallocate(void* ptr);
56 * which, respectively, allocate a block of size bytes (aligned to the
57 * maximum alignment required on your system), throwing std::bad_alloc
58 * if the allocation can't be satisfied, and free a previously
61 * Note that the following allocator resembles the standard allocator
64 * class MallocAllocator {
66 * void* allocate(size_t size) {
67 * void* p = malloc(size);
68 * if (!p) throw std::bad_alloc();
71 * void deallocate(void* p) {
76 * But note that if you pass StlAllocator<MallocAllocator,...> to a
77 * standard container it will be larger due to the contained state
80 * author: Tudor Bosman <tudorb@fb.com>
83 // This would be so much simpler with std::allocator_traits, but gcc 4.6.2
84 // doesn't support it.
85 template <class Alloc, class T> class StlAllocator;
87 template <class Alloc> class StlAllocator<Alloc, void> {
89 typedef void value_type;
90 typedef void* pointer;
91 typedef const void* const_pointer;
93 StlAllocator() : alloc_(nullptr) { }
94 explicit StlAllocator(Alloc* a) : alloc_(a) { }
96 Alloc* alloc() const {
100 template <class U> struct rebind {
101 typedef StlAllocator<Alloc, U> other;
104 bool operator!=(const StlAllocator<Alloc, void>& other) const {
105 return alloc_ != other.alloc_;
108 bool operator==(const StlAllocator<Alloc, void>& other) const {
109 return alloc_ == other.alloc_;
116 template <class Alloc, class T>
119 typedef T value_type;
121 typedef const T* const_pointer;
122 typedef T& reference;
123 typedef const T& const_reference;
125 typedef ptrdiff_t difference_type;
126 typedef size_t size_type;
128 StlAllocator() : alloc_(nullptr) { }
129 explicit StlAllocator(Alloc* a) : alloc_(a) { }
131 template <class U> StlAllocator(const StlAllocator<Alloc, U>& other)
132 : alloc_(other.alloc()) { }
134 T* allocate(size_t n, const void* hint = nullptr) {
135 return static_cast<T*>(alloc_->allocate(n * sizeof(T)));
138 void deallocate(T* p, size_t n) {
139 alloc_->deallocate(p);
142 size_t max_size() const {
143 return std::numeric_limits<size_t>::max();
146 T* address(T& x) const {
147 return std::addressof(x);
150 const T* address(const T& x) const {
151 return std::addressof(x);
154 template <class... Args>
155 void construct(T* p, Args&&... args) {
156 new (p) T(std::forward<Args>(args)...);
163 Alloc* alloc() const {
167 template <class U> struct rebind {
168 typedef StlAllocator<Alloc, U> other;
171 bool operator!=(const StlAllocator<Alloc, T>& other) const {
172 return alloc_ != other.alloc_;
175 bool operator==(const StlAllocator<Alloc, T>& other) const {
176 return alloc_ == other.alloc_;
184 * Helper function to obtain rebound allocators
186 * @author: Marcelo Juchem <marcelo@fb.com>
188 template <typename T, typename Allocator>
189 typename Allocator::template rebind<T>::other rebind_allocator(
190 Allocator const& allocator
192 return typename Allocator::template rebind<T>::other(allocator);
196 * Helper classes/functions for creating a unique_ptr using a custom
199 * @author: Marcelo Juchem <marcelo@fb.com>
202 // Derives from the allocator to take advantage of the empty base
203 // optimization when possible.
204 template <typename Allocator>
205 class allocator_delete
206 : private std::remove_reference<Allocator>::type
208 typedef typename std::remove_reference<Allocator>::type allocator_type;
211 typedef typename Allocator::pointer pointer;
213 allocator_delete() = default;
215 explicit allocator_delete(const allocator_type& allocator)
216 : allocator_type(allocator)
219 explicit allocator_delete(allocator_type&& allocator)
220 : allocator_type(std::move(allocator))
223 template <typename U>
224 allocator_delete(const allocator_delete<U>& other)
225 : allocator_type(other.get_allocator())
228 allocator_type& get_allocator() const {
229 return *const_cast<allocator_delete*>(this);
232 void operator()(pointer p) const {
234 const_cast<allocator_delete*>(this)->destroy(p);
235 const_cast<allocator_delete*>(this)->deallocate(p, 1);
239 template <typename T, typename Allocator>
240 class is_simple_allocator {
241 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_destroy, destroy);
243 typedef typename std::remove_const<
244 typename std::remove_reference<Allocator>::type
246 typedef typename std::remove_reference<T>::type value_type;
247 typedef value_type* pointer;
250 constexpr static bool value = !has_destroy<allocator, void(pointer)>::value
251 && !has_destroy<allocator, void(void*)>::value;
254 template <typename T, typename Allocator>
255 struct as_stl_allocator {
256 typedef typename std::conditional<
257 is_simple_allocator<T, Allocator>::value,
259 typename std::remove_reference<Allocator>::type,
260 typename std::remove_reference<T>::type
262 typename std::remove_reference<Allocator>::type
266 template <typename T, typename Allocator>
267 typename std::enable_if<
268 is_simple_allocator<T, Allocator>::value,
270 typename std::remove_reference<Allocator>::type,
271 typename std::remove_reference<T>::type
273 >::type make_stl_allocator(Allocator&& allocator) {
274 return folly::StlAllocator<
275 typename std::remove_reference<Allocator>::type,
276 typename std::remove_reference<T>::type
280 template <typename T, typename Allocator>
281 typename std::enable_if<
282 !is_simple_allocator<T, Allocator>::value,
283 typename std::remove_reference<Allocator>::type
284 >::type make_stl_allocator(Allocator&& allocator) {
285 return std::move(allocator);
289 * AllocatorUniquePtr: a unique_ptr that supports both STL-style
290 * allocators and SimpleAllocator
292 * @author: Marcelo Juchem <marcelo@fb.com>
295 template <typename T, typename Allocator>
296 struct AllocatorUniquePtr {
297 typedef std::unique_ptr<T,
298 folly::allocator_delete<
299 typename std::conditional<
300 is_simple_allocator<T, Allocator>::value,
301 folly::StlAllocator<typename std::remove_reference<Allocator>::type, T>,
302 typename std::remove_reference<Allocator>::type
309 * Functions to allocate a unique_ptr / shared_ptr, supporting both
310 * STL-style allocators and SimpleAllocator, analog to std::allocate_shared
312 * @author: Marcelo Juchem <marcelo@fb.com>
315 template <typename T, typename Allocator, typename ...Args>
316 typename AllocatorUniquePtr<T, Allocator>::type allocate_unique(
317 Allocator&& allocator, Args&&... args
319 auto stlAllocator = folly::make_stl_allocator<T>(
320 std::forward<Allocator>(allocator)
322 auto p = stlAllocator.allocate(1);
325 stlAllocator.construct(p, std::forward<Args>(args)...);
328 folly::allocator_delete<decltype(stlAllocator)>(std::move(stlAllocator))
331 stlAllocator.deallocate(p, 1);
336 template <typename T, typename Allocator, typename ...Args>
337 std::shared_ptr<T> allocate_shared(Allocator&& allocator, Args&&... args) {
338 return std::allocate_shared<T>(
339 folly::make_stl_allocator<T>(std::forward<Allocator>(allocator)),
340 std::forward<Args>(args)...
346 #endif /* FOLLY_MEMORY_H_ */