X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FMemory.h;h=ce0ff05d3290605f6dd08ba28ad2caae4aca47b4;hb=70230b7a106820807c8d74fcf3d21ebab3442fbb;hp=d24422190250cf4bdf7c05426a185f9d159ca4e5;hpb=512aa17590988db15d6f303528a45dfdcb9503de;p=folly.git diff --git a/folly/Memory.h b/folly/Memory.h index d2442219..ce0ff05d 100644 --- a/folly/Memory.h +++ b/folly/Memory.h @@ -17,6 +17,7 @@ #pragma once #include +#include #include #include @@ -24,6 +25,7 @@ #include #include #include +#include #include namespace folly { @@ -43,21 +45,21 @@ namespace folly { #else -template +template typename std::enable_if::value, std::unique_ptr>::type make_unique(Args&&... args) { return std::unique_ptr(new T(std::forward(args)...)); } // Allows 'make_unique(10)'. (N3690 s20.9.1.4 p3-4) -template +template typename std::enable_if::value, std::unique_ptr>::type make_unique(const size_t n) { return std::unique_ptr(new typename std::remove_extent::type[n]()); } // Disallows 'make_unique()'. (N3690 s20.9.1.4 p5) -template +template typename std::enable_if< std::extent::value != 0, std::unique_ptr>::type make_unique(Args&&...) = delete; @@ -137,7 +139,12 @@ std::weak_ptr to_weak_ptr(const std::shared_ptr& ptr) { return std::weak_ptr(ptr); } -using SysBufferDeleter = static_function_deleter; +struct SysBufferDeleter { + void operator()(void* p) const { + ::free(p); + } +}; + using SysBufferUniquePtr = std::unique_ptr; inline SysBufferUniquePtr allocate_sys_buffer(size_t size) { return SysBufferUniquePtr(::malloc(size)); @@ -160,7 +167,9 @@ class SysAlloc { public: void* allocate(size_t size) { void* p = ::malloc(size); - if (!p) throw std::bad_alloc(); + if (!p) { + throw std::bad_alloc(); + } return p; } void deallocate(void* p) { @@ -306,7 +315,7 @@ class allocator_delete { typedef typename std::remove_reference::type allocator_type; -public: + public: typedef typename Allocator::pointer pointer; allocator_delete() = default; @@ -329,31 +338,31 @@ public: } void operator()(pointer p) const { - if (!p) return; + if (!p) { + return; + } const_cast(this)->destroy(p); const_cast(this)->deallocate(p, 1); } }; -template -class is_simple_allocator { - FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_destroy, destroy); +namespace detail { - typedef typename std::remove_const< - typename std::remove_reference::type - >::type allocator; - typedef typename std::remove_reference::type value_type; - typedef value_type* pointer; +FOLLY_CREATE_MEMBER_INVOKE_TRAITS(destroy_invoke_traits, destroy); -public: - constexpr static bool value = !has_destroy::value - && !has_destroy::value; -}; +} // namespace detail + +template +using is_simple_allocator = + Negation>; template struct as_stl_allocator { typedef typename std::conditional< - is_simple_allocator::value, + is_simple_allocator< + typename std::remove_reference::type, + typename std::remove_reference::type + >::value, folly::StlAllocator< typename std::remove_reference::type, typename std::remove_reference::type @@ -364,7 +373,10 @@ struct as_stl_allocator { template typename std::enable_if< - is_simple_allocator::value, + is_simple_allocator< + typename std::remove_reference::type, + typename std::remove_reference::type + >::value, folly::StlAllocator< typename std::remove_reference::type, typename std::remove_reference::type @@ -378,7 +390,10 @@ typename std::enable_if< template typename std::enable_if< - !is_simple_allocator::value, + !is_simple_allocator< + typename std::remove_reference::type, + typename std::remove_reference::type + >::value, typename std::remove_reference::type >::type make_stl_allocator(Allocator&& allocator) { return std::move(allocator); @@ -396,7 +411,10 @@ struct AllocatorUniquePtr { typedef std::unique_ptr::value, + is_simple_allocator< + typename std::remove_reference::type, + typename std::remove_reference::type + >::value, folly::StlAllocator::type, T>, typename std::remove_reference::type >::type @@ -480,7 +498,7 @@ template struct IsArenaAllocator : std::false_type { }; */ template class enable_shared_from_this : public std::enable_shared_from_this { -public: + public: constexpr enable_shared_from_this() noexcept = default; std::weak_ptr weak_from_this() noexcept { @@ -491,7 +509,7 @@ public: return weak_from_this_(this); } -private: + private: // Uses SFINAE to detect and call // std::enable_shared_from_this::weak_from_this() if available. Falls // back to std::enable_shared_from_this::shared_from_this() otherwise. @@ -535,4 +553,4 @@ private: #endif -} // namespace folly +} // namespace folly