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;
60 * static_function_deleter
62 * So you can write this:
64 * using RSA_deleter = folly::static_function_deleter<RSA, &RSA_free>;
65 * auto rsa = std::unique_ptr<RSA, RSA_deleter>(RSA_new());
66 * RSA_generate_key_ex(rsa.get(), bits, exponent, nullptr);
67 * rsa = nullptr; // calls RSA_free(rsa.get())
69 * This would be sweet as well for BIO, but unfortunately BIO_free has signature
70 * int(BIO*) while we require signature void(BIO*). So you would need to make a
73 * inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); }
74 * using BIO_deleter = folly::static_function_deleter<BIO, &BIO_free_fb>;
75 * auto buf = std::unique_ptr<BIO, BIO_deleter>(BIO_new(BIO_s_mem()));
76 * buf = nullptr; // calls BIO_free(buf.get())
79 template <typename T, void(*f)(T*)>
80 struct static_function_deleter {
81 void operator()(T* t) { f(t); }
87 * Convert unique_ptr to shared_ptr without specifying the template type
88 * parameter and letting the compiler deduce it.
90 * So you can write this:
92 * auto sptr = to_shared_ptr(getSomethingUnique<T>());
96 * auto sptr = shared_ptr<T>(getSomethingUnique<T>());
98 * Useful when `T` is long, such as:
100 * using T = foobar::cpp2::FooBarServiceAsyncClient;
102 template <typename T>
103 std::shared_ptr<T> to_shared_ptr(std::unique_ptr<T>&& ptr) {
104 return std::shared_ptr<T>(std::move(ptr));
108 * A SimpleAllocator must provide two methods:
110 * void* allocate(size_t size);
111 * void deallocate(void* ptr);
113 * which, respectively, allocate a block of size bytes (aligned to the
114 * maximum alignment required on your system), throwing std::bad_alloc
115 * if the allocation can't be satisfied, and free a previously
118 * SysAlloc resembles the standard allocator.
122 void* allocate(size_t size) {
123 void* p = ::malloc(size);
124 if (!p) throw std::bad_alloc();
127 void deallocate(void* p) {
133 * StlAllocator wraps a SimpleAllocator into a STL-compliant
134 * allocator, maintaining an instance pointer to the simple allocator
135 * object. The underlying SimpleAllocator object must outlive all
136 * instances of StlAllocator using it.
138 * But note that if you pass StlAllocator<MallocAllocator,...> to a
139 * standard container it will be larger due to the contained state
142 * @author: Tudor Bosman <tudorb@fb.com>
145 // This would be so much simpler with std::allocator_traits, but gcc 4.6.2
146 // doesn't support it.
147 template <class Alloc, class T> class StlAllocator;
149 template <class Alloc> class StlAllocator<Alloc, void> {
151 typedef void value_type;
152 typedef void* pointer;
153 typedef const void* const_pointer;
155 StlAllocator() : alloc_(nullptr) { }
156 explicit StlAllocator(Alloc* a) : alloc_(a) { }
158 Alloc* alloc() const {
162 template <class U> struct rebind {
163 typedef StlAllocator<Alloc, U> other;
166 bool operator!=(const StlAllocator<Alloc, void>& other) const {
167 return alloc_ != other.alloc_;
170 bool operator==(const StlAllocator<Alloc, void>& other) const {
171 return alloc_ == other.alloc_;
178 template <class Alloc, class T>
181 typedef T value_type;
183 typedef const T* const_pointer;
184 typedef T& reference;
185 typedef const T& const_reference;
187 typedef ptrdiff_t difference_type;
188 typedef size_t size_type;
190 StlAllocator() : alloc_(nullptr) { }
191 explicit StlAllocator(Alloc* a) : alloc_(a) { }
193 template <class U> StlAllocator(const StlAllocator<Alloc, U>& other)
194 : alloc_(other.alloc()) { }
196 T* allocate(size_t n, const void* hint = nullptr) {
197 return static_cast<T*>(alloc_->allocate(n * sizeof(T)));
200 void deallocate(T* p, size_t n) {
201 alloc_->deallocate(p);
204 size_t max_size() const {
205 return std::numeric_limits<size_t>::max();
208 T* address(T& x) const {
209 return std::addressof(x);
212 const T* address(const T& x) const {
213 return std::addressof(x);
216 template <class... Args>
217 void construct(T* p, Args&&... args) {
218 new (p) T(std::forward<Args>(args)...);
225 Alloc* alloc() const {
229 template <class U> struct rebind {
230 typedef StlAllocator<Alloc, U> other;
233 bool operator!=(const StlAllocator<Alloc, T>& other) const {
234 return alloc_ != other.alloc_;
237 bool operator==(const StlAllocator<Alloc, T>& other) const {
238 return alloc_ == other.alloc_;
246 * Helper function to obtain rebound allocators
248 * @author: Marcelo Juchem <marcelo@fb.com>
250 template <typename T, typename Allocator>
251 typename Allocator::template rebind<T>::other rebind_allocator(
252 Allocator const& allocator
254 return typename Allocator::template rebind<T>::other(allocator);
258 * Helper classes/functions for creating a unique_ptr using a custom
261 * @author: Marcelo Juchem <marcelo@fb.com>
264 // Derives from the allocator to take advantage of the empty base
265 // optimization when possible.
266 template <typename Allocator>
267 class allocator_delete
268 : private std::remove_reference<Allocator>::type
270 typedef typename std::remove_reference<Allocator>::type allocator_type;
273 typedef typename Allocator::pointer pointer;
275 allocator_delete() = default;
277 explicit allocator_delete(const allocator_type& allocator)
278 : allocator_type(allocator)
281 explicit allocator_delete(allocator_type&& allocator)
282 : allocator_type(std::move(allocator))
285 template <typename U>
286 allocator_delete(const allocator_delete<U>& other)
287 : allocator_type(other.get_allocator())
290 allocator_type& get_allocator() const {
291 return *const_cast<allocator_delete*>(this);
294 void operator()(pointer p) const {
296 const_cast<allocator_delete*>(this)->destroy(p);
297 const_cast<allocator_delete*>(this)->deallocate(p, 1);
301 template <typename T, typename Allocator>
302 class is_simple_allocator {
303 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_destroy, destroy);
305 typedef typename std::remove_const<
306 typename std::remove_reference<Allocator>::type
308 typedef typename std::remove_reference<T>::type value_type;
309 typedef value_type* pointer;
312 constexpr static bool value = !has_destroy<allocator, void(pointer)>::value
313 && !has_destroy<allocator, void(void*)>::value;
316 template <typename T, typename Allocator>
317 struct as_stl_allocator {
318 typedef typename std::conditional<
319 is_simple_allocator<T, Allocator>::value,
321 typename std::remove_reference<Allocator>::type,
322 typename std::remove_reference<T>::type
324 typename std::remove_reference<Allocator>::type
328 template <typename T, typename Allocator>
329 typename std::enable_if<
330 is_simple_allocator<T, Allocator>::value,
332 typename std::remove_reference<Allocator>::type,
333 typename std::remove_reference<T>::type
335 >::type make_stl_allocator(Allocator&& allocator) {
336 return folly::StlAllocator<
337 typename std::remove_reference<Allocator>::type,
338 typename std::remove_reference<T>::type
342 template <typename T, typename Allocator>
343 typename std::enable_if<
344 !is_simple_allocator<T, Allocator>::value,
345 typename std::remove_reference<Allocator>::type
346 >::type make_stl_allocator(Allocator&& allocator) {
347 return std::move(allocator);
351 * AllocatorUniquePtr: a unique_ptr that supports both STL-style
352 * allocators and SimpleAllocator
354 * @author: Marcelo Juchem <marcelo@fb.com>
357 template <typename T, typename Allocator>
358 struct AllocatorUniquePtr {
359 typedef std::unique_ptr<T,
360 folly::allocator_delete<
361 typename std::conditional<
362 is_simple_allocator<T, Allocator>::value,
363 folly::StlAllocator<typename std::remove_reference<Allocator>::type, T>,
364 typename std::remove_reference<Allocator>::type
371 * Functions to allocate a unique_ptr / shared_ptr, supporting both
372 * STL-style allocators and SimpleAllocator, analog to std::allocate_shared
374 * @author: Marcelo Juchem <marcelo@fb.com>
377 template <typename T, typename Allocator, typename ...Args>
378 typename AllocatorUniquePtr<T, Allocator>::type allocate_unique(
379 Allocator&& allocator, Args&&... args
381 auto stlAllocator = folly::make_stl_allocator<T>(
382 std::forward<Allocator>(allocator)
384 auto p = stlAllocator.allocate(1);
387 stlAllocator.construct(p, std::forward<Args>(args)...);
390 folly::allocator_delete<decltype(stlAllocator)>(std::move(stlAllocator))
393 stlAllocator.deallocate(p, 1);
398 template <typename T, typename Allocator, typename ...Args>
399 std::shared_ptr<T> allocate_shared(Allocator&& allocator, Args&&... args) {
400 return std::allocate_shared<T>(
401 folly::make_stl_allocator<T>(std::forward<Allocator>(allocator)),
402 std::forward<Args>(args)...
407 * IsArenaAllocator<T>::value describes whether SimpleAllocator has
408 * no-op deallocate().
410 template <class T> struct IsArenaAllocator : std::false_type { };
414 #endif /* FOLLY_MEMORY_H_ */