2 * Copyright 2015 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;
62 * Convert unique_ptr to shared_ptr without specifying the template type
63 * parameter and letting the compiler deduce it.
65 * So you can write this:
67 * auto sptr = to_shared_ptr(getSomethingUnique<T>());
71 * auto sptr = shared_ptr<T>(getSomethingUnique<T>());
73 * Useful when `T` is long, such as:
75 * using T = foobar::cpp2::FooBarServiceAsyncClient;
78 std::shared_ptr<T> to_shared_ptr(std::unique_ptr<T>&& ptr) {
79 return std::shared_ptr<T>(std::move(ptr));
83 * A SimpleAllocator must provide two methods:
85 * void* allocate(size_t size);
86 * void deallocate(void* ptr);
88 * which, respectively, allocate a block of size bytes (aligned to the
89 * maximum alignment required on your system), throwing std::bad_alloc
90 * if the allocation can't be satisfied, and free a previously
93 * SysAlloc resembles the standard allocator.
97 void* allocate(size_t size) {
98 void* p = ::malloc(size);
99 if (!p) throw std::bad_alloc();
102 void deallocate(void* p) {
108 * StlAllocator wraps a SimpleAllocator into a STL-compliant
109 * allocator, maintaining an instance pointer to the simple allocator
110 * object. The underlying SimpleAllocator object must outlive all
111 * instances of StlAllocator using it.
113 * But note that if you pass StlAllocator<MallocAllocator,...> to a
114 * standard container it will be larger due to the contained state
117 * @author: Tudor Bosman <tudorb@fb.com>
120 // This would be so much simpler with std::allocator_traits, but gcc 4.6.2
121 // doesn't support it.
122 template <class Alloc, class T> class StlAllocator;
124 template <class Alloc> class StlAllocator<Alloc, void> {
126 typedef void value_type;
127 typedef void* pointer;
128 typedef const void* const_pointer;
130 StlAllocator() : alloc_(nullptr) { }
131 explicit StlAllocator(Alloc* a) : alloc_(a) { }
133 Alloc* alloc() const {
137 template <class U> struct rebind {
138 typedef StlAllocator<Alloc, U> other;
141 bool operator!=(const StlAllocator<Alloc, void>& other) const {
142 return alloc_ != other.alloc_;
145 bool operator==(const StlAllocator<Alloc, void>& other) const {
146 return alloc_ == other.alloc_;
153 template <class Alloc, class T>
156 typedef T value_type;
158 typedef const T* const_pointer;
159 typedef T& reference;
160 typedef const T& const_reference;
162 typedef ptrdiff_t difference_type;
163 typedef size_t size_type;
165 StlAllocator() : alloc_(nullptr) { }
166 explicit StlAllocator(Alloc* a) : alloc_(a) { }
168 template <class U> StlAllocator(const StlAllocator<Alloc, U>& other)
169 : alloc_(other.alloc()) { }
171 T* allocate(size_t n, const void* hint = nullptr) {
172 return static_cast<T*>(alloc_->allocate(n * sizeof(T)));
175 void deallocate(T* p, size_t n) {
176 alloc_->deallocate(p);
179 size_t max_size() const {
180 return std::numeric_limits<size_t>::max();
183 T* address(T& x) const {
184 return std::addressof(x);
187 const T* address(const T& x) const {
188 return std::addressof(x);
191 template <class... Args>
192 void construct(T* p, Args&&... args) {
193 new (p) T(std::forward<Args>(args)...);
200 Alloc* alloc() const {
204 template <class U> struct rebind {
205 typedef StlAllocator<Alloc, U> other;
208 bool operator!=(const StlAllocator<Alloc, T>& other) const {
209 return alloc_ != other.alloc_;
212 bool operator==(const StlAllocator<Alloc, T>& other) const {
213 return alloc_ == other.alloc_;
221 * Helper function to obtain rebound allocators
223 * @author: Marcelo Juchem <marcelo@fb.com>
225 template <typename T, typename Allocator>
226 typename Allocator::template rebind<T>::other rebind_allocator(
227 Allocator const& allocator
229 return typename Allocator::template rebind<T>::other(allocator);
233 * Helper classes/functions for creating a unique_ptr using a custom
236 * @author: Marcelo Juchem <marcelo@fb.com>
239 // Derives from the allocator to take advantage of the empty base
240 // optimization when possible.
241 template <typename Allocator>
242 class allocator_delete
243 : private std::remove_reference<Allocator>::type
245 typedef typename std::remove_reference<Allocator>::type allocator_type;
248 typedef typename Allocator::pointer pointer;
250 allocator_delete() = default;
252 explicit allocator_delete(const allocator_type& allocator)
253 : allocator_type(allocator)
256 explicit allocator_delete(allocator_type&& allocator)
257 : allocator_type(std::move(allocator))
260 template <typename U>
261 allocator_delete(const allocator_delete<U>& other)
262 : allocator_type(other.get_allocator())
265 allocator_type& get_allocator() const {
266 return *const_cast<allocator_delete*>(this);
269 void operator()(pointer p) const {
271 const_cast<allocator_delete*>(this)->destroy(p);
272 const_cast<allocator_delete*>(this)->deallocate(p, 1);
276 template <typename T, typename Allocator>
277 class is_simple_allocator {
278 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_destroy, destroy);
280 typedef typename std::remove_const<
281 typename std::remove_reference<Allocator>::type
283 typedef typename std::remove_reference<T>::type value_type;
284 typedef value_type* pointer;
287 constexpr static bool value = !has_destroy<allocator, void(pointer)>::value
288 && !has_destroy<allocator, void(void*)>::value;
291 template <typename T, typename Allocator>
292 struct as_stl_allocator {
293 typedef typename std::conditional<
294 is_simple_allocator<T, Allocator>::value,
296 typename std::remove_reference<Allocator>::type,
297 typename std::remove_reference<T>::type
299 typename std::remove_reference<Allocator>::type
303 template <typename T, typename Allocator>
304 typename std::enable_if<
305 is_simple_allocator<T, Allocator>::value,
307 typename std::remove_reference<Allocator>::type,
308 typename std::remove_reference<T>::type
310 >::type make_stl_allocator(Allocator&& allocator) {
311 return folly::StlAllocator<
312 typename std::remove_reference<Allocator>::type,
313 typename std::remove_reference<T>::type
317 template <typename T, typename Allocator>
318 typename std::enable_if<
319 !is_simple_allocator<T, Allocator>::value,
320 typename std::remove_reference<Allocator>::type
321 >::type make_stl_allocator(Allocator&& allocator) {
322 return std::move(allocator);
326 * AllocatorUniquePtr: a unique_ptr that supports both STL-style
327 * allocators and SimpleAllocator
329 * @author: Marcelo Juchem <marcelo@fb.com>
332 template <typename T, typename Allocator>
333 struct AllocatorUniquePtr {
334 typedef std::unique_ptr<T,
335 folly::allocator_delete<
336 typename std::conditional<
337 is_simple_allocator<T, Allocator>::value,
338 folly::StlAllocator<typename std::remove_reference<Allocator>::type, T>,
339 typename std::remove_reference<Allocator>::type
346 * Functions to allocate a unique_ptr / shared_ptr, supporting both
347 * STL-style allocators and SimpleAllocator, analog to std::allocate_shared
349 * @author: Marcelo Juchem <marcelo@fb.com>
352 template <typename T, typename Allocator, typename ...Args>
353 typename AllocatorUniquePtr<T, Allocator>::type allocate_unique(
354 Allocator&& allocator, Args&&... args
356 auto stlAllocator = folly::make_stl_allocator<T>(
357 std::forward<Allocator>(allocator)
359 auto p = stlAllocator.allocate(1);
362 stlAllocator.construct(p, std::forward<Args>(args)...);
365 folly::allocator_delete<decltype(stlAllocator)>(std::move(stlAllocator))
368 stlAllocator.deallocate(p, 1);
373 template <typename T, typename Allocator, typename ...Args>
374 std::shared_ptr<T> allocate_shared(Allocator&& allocator, Args&&... args) {
375 return std::allocate_shared<T>(
376 folly::make_stl_allocator<T>(std::forward<Allocator>(allocator)),
377 std::forward<Args>(args)...
382 * IsArenaAllocator<T>::value describes whether SimpleAllocator has
383 * no-op deallocate().
385 template <class T> struct IsArenaAllocator : std::false_type { };
389 #endif /* FOLLY_MEMORY_H_ */