2 * Copyright 2016 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.
19 #include <folly/Traits.h>
32 * For exception safety and consistency with make_shared. Erase me when
33 * we have std::make_unique().
35 * @author Louis Brandy (ldbrandy@fb.com)
36 * @author Xu Ning (xning@fb.com)
39 #if __cplusplus >= 201402L || \
40 (defined __cpp_lib_make_unique && __cpp_lib_make_unique >= 201304L) || \
41 (defined(_MSC_VER) && _MSC_VER >= 1900)
43 /* using override */ using std::make_unique;
47 template<typename T, typename... Args>
48 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
49 make_unique(Args&&... args) {
50 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
53 // Allows 'make_unique<T[]>(10)'. (N3690 s20.9.1.4 p3-4)
55 typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T>>::type
56 make_unique(const size_t n) {
57 return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
60 // Disallows 'make_unique<T[10]>()'. (N3690 s20.9.1.4 p5)
61 template<typename T, typename... Args>
62 typename std::enable_if<
63 std::extent<T>::value != 0, std::unique_ptr<T>>::type
64 make_unique(Args&&...) = delete;
69 * static_function_deleter
71 * So you can write this:
73 * using RSA_deleter = folly::static_function_deleter<RSA, &RSA_free>;
74 * auto rsa = std::unique_ptr<RSA, RSA_deleter>(RSA_new());
75 * RSA_generate_key_ex(rsa.get(), bits, exponent, nullptr);
76 * rsa = nullptr; // calls RSA_free(rsa.get())
78 * This would be sweet as well for BIO, but unfortunately BIO_free has signature
79 * int(BIO*) while we require signature void(BIO*). So you would need to make a
82 * inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); }
83 * using BIO_deleter = folly::static_function_deleter<BIO, &BIO_free_fb>;
84 * auto buf = std::unique_ptr<BIO, BIO_deleter>(BIO_new(BIO_s_mem()));
85 * buf = nullptr; // calls BIO_free(buf.get())
88 template <typename T, void(*f)(T*)>
89 struct static_function_deleter {
90 void operator()(T* t) const {
98 * Convert unique_ptr to shared_ptr without specifying the template type
99 * parameter and letting the compiler deduce it.
101 * So you can write this:
103 * auto sptr = to_shared_ptr(getSomethingUnique<T>());
107 * auto sptr = shared_ptr<T>(getSomethingUnique<T>());
109 * Useful when `T` is long, such as:
111 * using T = foobar::FooBarAsyncClient;
113 template <typename T, typename D>
114 std::shared_ptr<T> to_shared_ptr(std::unique_ptr<T, D>&& ptr) {
115 return std::shared_ptr<T>(std::move(ptr));
121 * Make a weak_ptr and return it from a shared_ptr without specifying the
122 * template type parameter and letting the compiler deduce it.
124 * So you can write this:
126 * auto wptr = to_weak_ptr(getSomethingShared<T>());
130 * auto wptr = weak_ptr<T>(getSomethingShared<T>());
132 * Useful when `T` is long, such as:
134 * using T = foobar::FooBarAsyncClient;
136 template <typename T>
137 std::weak_ptr<T> to_weak_ptr(const std::shared_ptr<T>& ptr) {
138 return std::weak_ptr<T>(ptr);
141 using SysBufferDeleter = static_function_deleter<void, ::free>;
142 using SysBufferUniquePtr = std::unique_ptr<void, SysBufferDeleter>;
143 inline SysBufferUniquePtr allocate_sys_buffer(size_t size) {
144 return SysBufferUniquePtr(::malloc(size));
148 * A SimpleAllocator must provide two methods:
150 * void* allocate(size_t size);
151 * void deallocate(void* ptr);
153 * which, respectively, allocate a block of size bytes (aligned to the
154 * maximum alignment required on your system), throwing std::bad_alloc
155 * if the allocation can't be satisfied, and free a previously
158 * SysAlloc resembles the standard allocator.
162 void* allocate(size_t size) {
163 void* p = ::malloc(size);
164 if (!p) throw std::bad_alloc();
167 void deallocate(void* p) {
173 * StlAllocator wraps a SimpleAllocator into a STL-compliant
174 * allocator, maintaining an instance pointer to the simple allocator
175 * object. The underlying SimpleAllocator object must outlive all
176 * instances of StlAllocator using it.
178 * But note that if you pass StlAllocator<MallocAllocator,...> to a
179 * standard container it will be larger due to the contained state
182 * @author: Tudor Bosman <tudorb@fb.com>
185 // This would be so much simpler with std::allocator_traits, but gcc 4.6.2
186 // doesn't support it.
187 template <class Alloc, class T> class StlAllocator;
189 template <class Alloc> class StlAllocator<Alloc, void> {
191 typedef void value_type;
192 typedef void* pointer;
193 typedef const void* const_pointer;
195 StlAllocator() : alloc_(nullptr) { }
196 explicit StlAllocator(Alloc* a) : alloc_(a) { }
198 Alloc* alloc() const {
202 template <class U> struct rebind {
203 typedef StlAllocator<Alloc, U> other;
206 bool operator!=(const StlAllocator<Alloc, void>& other) const {
207 return alloc_ != other.alloc_;
210 bool operator==(const StlAllocator<Alloc, void>& other) const {
211 return alloc_ == other.alloc_;
218 template <class Alloc, class T>
221 typedef T value_type;
223 typedef const T* const_pointer;
224 typedef T& reference;
225 typedef const T& const_reference;
227 typedef ptrdiff_t difference_type;
228 typedef size_t size_type;
230 StlAllocator() : alloc_(nullptr) { }
231 explicit StlAllocator(Alloc* a) : alloc_(a) { }
233 template <class U> StlAllocator(const StlAllocator<Alloc, U>& other)
234 : alloc_(other.alloc()) { }
236 T* allocate(size_t n, const void* /* hint */ = nullptr) {
237 return static_cast<T*>(alloc_->allocate(n * sizeof(T)));
240 void deallocate(T* p, size_t /* n */) { alloc_->deallocate(p); }
242 size_t max_size() const {
243 return std::numeric_limits<size_t>::max();
246 T* address(T& x) const {
247 return std::addressof(x);
250 const T* address(const T& x) const {
251 return std::addressof(x);
254 template <class... Args>
255 void construct(T* p, Args&&... args) {
256 new (p) T(std::forward<Args>(args)...);
263 Alloc* alloc() const {
267 template <class U> struct rebind {
268 typedef StlAllocator<Alloc, U> other;
271 bool operator!=(const StlAllocator<Alloc, T>& other) const {
272 return alloc_ != other.alloc_;
275 bool operator==(const StlAllocator<Alloc, T>& other) const {
276 return alloc_ == other.alloc_;
284 * Helper function to obtain rebound allocators
286 * @author: Marcelo Juchem <marcelo@fb.com>
288 template <typename T, typename Allocator>
289 typename Allocator::template rebind<T>::other rebind_allocator(
290 Allocator const& allocator
292 return typename Allocator::template rebind<T>::other(allocator);
296 * Helper classes/functions for creating a unique_ptr using a custom
299 * @author: Marcelo Juchem <marcelo@fb.com>
302 // Derives from the allocator to take advantage of the empty base
303 // optimization when possible.
304 template <typename Allocator>
305 class allocator_delete
306 : private std::remove_reference<Allocator>::type
308 typedef typename std::remove_reference<Allocator>::type allocator_type;
311 typedef typename Allocator::pointer pointer;
313 allocator_delete() = default;
315 explicit allocator_delete(const allocator_type& allocator)
316 : allocator_type(allocator)
319 explicit allocator_delete(allocator_type&& allocator)
320 : allocator_type(std::move(allocator))
323 template <typename U>
324 allocator_delete(const allocator_delete<U>& other)
325 : allocator_type(other.get_allocator())
328 allocator_type& get_allocator() const {
329 return *const_cast<allocator_delete*>(this);
332 void operator()(pointer p) const {
334 const_cast<allocator_delete*>(this)->destroy(p);
335 const_cast<allocator_delete*>(this)->deallocate(p, 1);
339 template <typename T, typename Allocator>
340 class is_simple_allocator {
341 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_destroy, destroy);
343 typedef typename std::remove_const<
344 typename std::remove_reference<Allocator>::type
346 typedef typename std::remove_reference<T>::type value_type;
347 typedef value_type* pointer;
350 constexpr static bool value = !has_destroy<allocator, void(pointer)>::value
351 && !has_destroy<allocator, void(void*)>::value;
354 template <typename T, typename Allocator>
355 struct as_stl_allocator {
356 typedef typename std::conditional<
357 is_simple_allocator<T, Allocator>::value,
359 typename std::remove_reference<Allocator>::type,
360 typename std::remove_reference<T>::type
362 typename std::remove_reference<Allocator>::type
366 template <typename T, typename Allocator>
367 typename std::enable_if<
368 is_simple_allocator<T, Allocator>::value,
370 typename std::remove_reference<Allocator>::type,
371 typename std::remove_reference<T>::type
373 >::type make_stl_allocator(Allocator&& allocator) {
374 return folly::StlAllocator<
375 typename std::remove_reference<Allocator>::type,
376 typename std::remove_reference<T>::type
380 template <typename T, typename Allocator>
381 typename std::enable_if<
382 !is_simple_allocator<T, Allocator>::value,
383 typename std::remove_reference<Allocator>::type
384 >::type make_stl_allocator(Allocator&& allocator) {
385 return std::move(allocator);
389 * AllocatorUniquePtr: a unique_ptr that supports both STL-style
390 * allocators and SimpleAllocator
392 * @author: Marcelo Juchem <marcelo@fb.com>
395 template <typename T, typename Allocator>
396 struct AllocatorUniquePtr {
397 typedef std::unique_ptr<T,
398 folly::allocator_delete<
399 typename std::conditional<
400 is_simple_allocator<T, Allocator>::value,
401 folly::StlAllocator<typename std::remove_reference<Allocator>::type, T>,
402 typename std::remove_reference<Allocator>::type
409 * Functions to allocate a unique_ptr / shared_ptr, supporting both
410 * STL-style allocators and SimpleAllocator, analog to std::allocate_shared
412 * @author: Marcelo Juchem <marcelo@fb.com>
415 template <typename T, typename Allocator, typename ...Args>
416 typename AllocatorUniquePtr<T, Allocator>::type allocate_unique(
417 Allocator&& allocator, Args&&... args
419 auto stlAllocator = folly::make_stl_allocator<T>(
420 std::forward<Allocator>(allocator)
422 auto p = stlAllocator.allocate(1);
425 stlAllocator.construct(p, std::forward<Args>(args)...);
428 folly::allocator_delete<decltype(stlAllocator)>(std::move(stlAllocator))
431 stlAllocator.deallocate(p, 1);
436 template <typename T, typename Allocator, typename ...Args>
437 std::shared_ptr<T> allocate_shared(Allocator&& allocator, Args&&... args) {
438 return std::allocate_shared<T>(
439 folly::make_stl_allocator<T>(std::forward<Allocator>(allocator)),
440 std::forward<Args>(args)...
445 * IsArenaAllocator<T>::value describes whether SimpleAllocator has
446 * no-op deallocate().
448 template <class T> struct IsArenaAllocator : std::false_type { };