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;
82 StlAllocator() : alloc_(nullptr) { }
83 explicit StlAllocator(Alloc* alloc) : alloc_(alloc) { }
85 Alloc* alloc() const {
89 template <class U> struct rebind {
90 typedef StlAllocator<Alloc, U> other;
93 bool operator!=(const StlAllocator<Alloc, void>& other) const {
94 return alloc_ != other.alloc_;
97 bool operator==(const StlAllocator<Alloc, void>& other) const {
98 return alloc_ == other.alloc_;
105 template <class Alloc, class T>
108 typedef T value_type;
110 typedef const T* const_pointer;
111 typedef T& reference;
112 typedef const T& const_reference;
114 typedef ptrdiff_t difference_type;
115 typedef size_t size_type;
117 StlAllocator() : alloc_(nullptr) { }
118 explicit StlAllocator(Alloc* alloc) : alloc_(alloc) { }
120 template <class U> StlAllocator(const StlAllocator<Alloc, U>& other)
121 : alloc_(other.alloc()) { }
123 T* allocate(size_t n, const void* hint = nullptr) {
124 return static_cast<T*>(alloc_->allocate(n * sizeof(T)));
127 void deallocate(T* p, size_t n) {
128 alloc_->deallocate(p);
131 size_t max_size() const {
132 return std::numeric_limits<size_t>::max();
135 T* address(T& x) const {
136 return std::addressof(x);
139 const T* address(const T& x) const {
140 return std::addressof(x);
143 template <class... Args>
144 void construct(T* p, Args&&... args) {
145 new (p) T(std::forward<Args>(args)...);
152 Alloc* alloc() const {
156 template <class U> struct rebind {
157 typedef StlAllocator<Alloc, U> other;
160 bool operator!=(const StlAllocator<Alloc, T>& other) const {
161 return alloc_ != other.alloc_;
164 bool operator==(const StlAllocator<Alloc, T>& other) const {
165 return alloc_ == other.alloc_;
173 * Helper function to obtain rebound allocators
175 * @author: Marcelo Juchem <marcelo@fb.com>
177 template <typename T, typename Allocator>
178 typename Allocator::template rebind<T>::other rebind_allocator(
179 Allocator const &allocator
181 return typename Allocator::template rebind<T>::other(allocator);
185 * Helper classes/functions for creating a unique_ptr using a custom allocator
187 * @author: Marcelo Juchem <marcelo@fb.com>
190 // A deleter implementation based on std::default_delete,
191 // which uses a custom allocator to free memory
192 template <typename Allocator>
193 class allocator_delete {
194 typedef typename std::remove_reference<Allocator>::type allocator_type;
197 allocator_delete() = default;
199 explicit allocator_delete(const allocator_type& allocator):
200 allocator_(allocator)
203 explicit allocator_delete(allocator_type&& allocator):
204 allocator_(std::move(allocator))
207 template <typename U>
208 allocator_delete(const allocator_delete<U>& other):
209 allocator_(other.get_allocator())
212 allocator_type& get_allocator() const {
216 void operator()(typename allocator_type::pointer p) const {
221 allocator_.destroy(p);
222 allocator_.deallocate(p, 1);
226 mutable allocator_type allocator_;
229 template <typename T, typename Allocator>
230 class is_simple_allocator {
231 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_destroy, destroy);
233 typedef typename std::remove_const<
234 typename std::remove_reference<Allocator>::type
236 typedef typename std::remove_reference<T>::type value_type;
237 typedef value_type* pointer;
240 constexpr static bool value = !has_destroy<allocator, void(pointer)>::value
241 && !has_destroy<allocator, void(void*)>::value;
244 template <typename T, typename Allocator>
245 struct as_stl_allocator {
246 typedef typename std::conditional<
247 is_simple_allocator<T, Allocator>::value,
249 typename std::remove_reference<Allocator>::type,
250 typename std::remove_reference<T>::type
252 typename std::remove_reference<Allocator>::type
256 template <typename T, typename Allocator>
257 typename std::enable_if<
258 is_simple_allocator<T, Allocator>::value,
260 typename std::remove_reference<Allocator>::type,
261 typename std::remove_reference<T>::type
263 >::type make_stl_allocator(Allocator&& allocator) {
264 return folly::StlAllocator<
265 typename std::remove_reference<Allocator>::type,
266 typename std::remove_reference<T>::type
270 template <typename T, typename Allocator>
271 typename std::enable_if<
272 !is_simple_allocator<T, Allocator>::value,
273 typename std::remove_reference<Allocator>::type
274 >::type make_stl_allocator(Allocator&& allocator) {
275 return std::move(allocator);
279 * AllocatorUniquePtr: a unique_ptr that supports both STL-style
280 * allocators and SimpleAllocator
282 * @author: Marcelo Juchem <marcelo@fb.com>
285 template <typename T, typename Allocator>
286 struct AllocatorUniquePtr {
287 typedef std::unique_ptr<T,
288 folly::allocator_delete<
289 typename std::conditional<
290 is_simple_allocator<T, Allocator>::value,
291 folly::StlAllocator<typename std::remove_reference<Allocator>::type, T>,
292 typename std::remove_reference<Allocator>::type
299 * Functions to allocate a unique_ptr / shared_ptr, supporting both
300 * STL-style allocators and SimpleAllocator, analog to std::allocate_shared
302 * @author: Marcelo Juchem <marcelo@fb.com>
305 template <typename T, typename Allocator, typename ...Args>
306 typename AllocatorUniquePtr<T, Allocator>::type allocate_unique(
307 Allocator&& allocator, Args&&... args
309 auto stlAllocator = folly::make_stl_allocator<T>(
310 std::forward<Allocator>(allocator)
312 auto p = stlAllocator.allocate(1);
315 stlAllocator.construct(p, std::forward<Args>(args)...);
318 folly::allocator_delete<decltype(stlAllocator)>(std::move(stlAllocator))
321 stlAllocator.deallocate(p, 1);
326 template <typename T, typename Allocator, typename ...Args>
327 std::shared_ptr<T> allocate_shared(Allocator&& allocator, Args&&... args) {
328 return std::allocate_shared<T>(
329 folly::make_stl_allocator<T>(std::forward<Allocator>(allocator)),
330 std::forward<Args>(args)...
336 #endif /* FOLLY_MEMORY_H_ */