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)
39 template<typename T, typename... Args>
40 std::unique_ptr<T> make_unique(Args&&... args) {
41 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
45 * Wrap a SimpleAllocator into a STL-compliant allocator.
47 * The SimpleAllocator must provide two methods:
48 * void* allocate(size_t size);
49 * void deallocate(void* ptr);
50 * which, respectively, allocate a block of size bytes (aligned to the maximum
51 * alignment required on your system), throwing std::bad_alloc if the
52 * allocation can't be satisfied, and free a previously allocated block.
54 * Note that the following allocator resembles the standard allocator
57 * class MallocAllocator {
59 * void* allocate(size_t size) {
60 * void* p = malloc(size);
61 * if (!p) throw std::bad_alloc();
64 * void deallocate(void* p) {
69 * author: Tudor Bosman <tudorb@fb.com>
72 // This would be so much simpler with std::allocator_traits, but gcc 4.6.2
74 template <class Alloc, class T> class StlAllocator;
76 template <class Alloc> class StlAllocator<Alloc, void> {
78 typedef void value_type;
79 typedef void* pointer;
80 typedef const void* const_pointer;
81 template <class U> struct rebind {
82 typedef StlAllocator<Alloc, U> other;
86 template <class Alloc, class T>
91 typedef const T* const_pointer;
93 typedef const T& const_reference;
95 typedef ptrdiff_t difference_type;
96 typedef size_t size_type;
98 StlAllocator() : alloc_(nullptr) { }
99 explicit StlAllocator(Alloc* alloc) : alloc_(alloc) { }
101 template <class U> StlAllocator(const StlAllocator<Alloc, U>& other)
102 : alloc_(other.alloc()) { }
104 T* allocate(size_t n, const void* hint = nullptr) {
105 return static_cast<T*>(alloc_->allocate(n * sizeof(T)));
108 void deallocate(T* p, size_t n) {
109 alloc_->deallocate(p);
112 size_t max_size() const {
113 return std::numeric_limits<size_t>::max();
116 T* address(T& x) const {
117 return std::addressof(x);
120 const T* address(const T& x) const {
121 return std::addressof(x);
124 template <class... Args>
125 void construct(T* p, Args&&... args) {
126 new (p) T(std::forward<Args>(args)...);
133 Alloc* alloc() const {
137 template <class U> struct rebind {
138 typedef StlAllocator<Alloc, U> other;
141 bool operator!=(const StlAllocator<Alloc, T>& other) const {
142 return alloc_ != other.alloc_;
145 bool operator==(const StlAllocator<Alloc, T>& other) const {
146 return alloc_ == other.alloc_;
154 * Helper classes/functions for creating a unique_ptr using a custom allocator
156 * @author: Marcelo Juchem <marcelo@fb.com>
159 // A deleter implementation based on std::default_delete,
160 // which uses a custom allocator to free memory
161 template <typename Allocator>
162 class allocator_delete {
163 typedef typename std::remove_reference<Allocator>::type allocator_type;
166 allocator_delete() = default;
168 explicit allocator_delete(const allocator_type& allocator):
169 allocator_(allocator)
172 explicit allocator_delete(allocator_type&& allocator):
173 allocator_(std::move(allocator))
176 template <typename U>
177 allocator_delete(const allocator_delete<U>& other):
178 allocator_(other.get_allocator())
181 allocator_type& get_allocator() const {
185 void operator()(typename allocator_type::pointer p) const {
190 allocator_.destroy(p);
191 allocator_.deallocate(p, 1);
195 mutable allocator_type allocator_;
198 template <typename T, typename Allocator>
199 class is_simple_allocator {
200 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_destroy, destroy);
202 typedef typename std::remove_const<
203 typename std::remove_reference<Allocator>::type
205 typedef typename std::remove_reference<T>::type value_type;
206 typedef value_type* pointer;
209 constexpr static bool value = !has_destroy<allocator, void(pointer)>::value
210 && !has_destroy<allocator, void(void*)>::value;
213 template <typename T, typename Allocator>
214 struct as_stl_allocator {
215 typedef typename std::conditional<
216 is_simple_allocator<T, Allocator>::value,
218 typename std::remove_reference<Allocator>::type,
219 typename std::remove_reference<T>::type
221 typename std::remove_reference<Allocator>::type
225 template <typename T, typename Allocator>
226 typename std::enable_if<
227 is_simple_allocator<T, Allocator>::value,
229 typename std::remove_reference<Allocator>::type,
230 typename std::remove_reference<T>::type
232 >::type make_stl_allocator(Allocator&& allocator) {
233 return folly::StlAllocator<
234 typename std::remove_reference<Allocator>::type,
235 typename std::remove_reference<T>::type
239 template <typename T, typename Allocator>
240 typename std::enable_if<
241 !is_simple_allocator<T, Allocator>::value,
242 typename std::remove_reference<Allocator>::type
243 >::type make_stl_allocator(Allocator&& allocator) {
244 return std::move(allocator);
248 * AllocatorUniquePtr: a unique_ptr that supports both STL-style
249 * allocators and SimpleAllocator
251 * @author: Marcelo Juchem <marcelo@fb.com>
254 template <typename T, typename Allocator>
255 struct AllocatorUniquePtr {
256 typedef std::unique_ptr<T,
257 folly::allocator_delete<
258 typename std::conditional<
259 is_simple_allocator<T, Allocator>::value,
260 folly::StlAllocator<typename std::remove_reference<Allocator>::type, T>,
261 typename std::remove_reference<Allocator>::type
268 * Functions to allocate a unique_ptr / shared_ptr, supporting both
269 * STL-style allocators and SimpleAllocator, analog to std::allocate_shared
271 * @author: Marcelo Juchem <marcelo@fb.com>
274 template <typename T, typename Allocator, typename ...Args>
275 typename AllocatorUniquePtr<T, Allocator>::type allocate_unique(
276 Allocator&& allocator, Args&&... args
278 auto stlAllocator = folly::make_stl_allocator<T>(
279 std::forward<Allocator>(allocator)
281 auto p = stlAllocator.allocate(1);
284 stlAllocator.construct(p, std::forward<Args>(args)...);
287 folly::allocator_delete<decltype(stlAllocator)>(std::move(stlAllocator))
290 stlAllocator.deallocate(p, 1);
295 template <typename T, typename Allocator, typename ...Args>
296 std::shared_ptr<T> allocate_shared(Allocator&& allocator, Args&&... args) {
297 return std::allocate_shared<T>(
298 folly::make_stl_allocator<T>(std::forward<Allocator>(allocator)),
299 std::forward<Args>(args)...
305 #endif /* FOLLY_MEMORY_H_ */