llvm-project/libcxx/include/memory

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

2810 lines
86 KiB
Plaintext
Raw Normal View History

2010-05-12 03:42:16 +08:00
// -*- C++ -*-
//===-------------------------- memory ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2010-05-12 03:42:16 +08:00
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP_MEMORY
#define _LIBCPP_MEMORY
/*
memory synopsis
namespace std
{
struct allocator_arg_t { };
inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
2010-05-12 03:42:16 +08:00
template <class T, class Alloc> struct uses_allocator;
template <class Ptr>
struct pointer_traits
{
typedef Ptr pointer;
typedef <details> element_type;
typedef <details> difference_type;
2010-05-12 03:42:16 +08:00
template <class U> using rebind = <details>;
2010-05-12 03:42:16 +08:00
static pointer pointer_to(<details>);
};
template <class T>
struct pointer_traits<T*>
{
typedef T* pointer;
typedef T element_type;
typedef ptrdiff_t difference_type;
template <class U> using rebind = U*;
static pointer pointer_to(<details>) noexcept; // constexpr in C++20
};
template <class T> constexpr T* to_address(T* p) noexcept; // C++20
template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
2010-05-12 03:42:16 +08:00
template <class Alloc>
struct allocator_traits
{
typedef Alloc allocator_type;
typedef typename allocator_type::value_type
value_type;
typedef Alloc::pointer | value_type* pointer;
typedef Alloc::const_pointer
| pointer_traits<pointer>::rebind<const value_type>
const_pointer;
typedef Alloc::void_pointer
| pointer_traits<pointer>::rebind<void>
void_pointer;
typedef Alloc::const_void_pointer
| pointer_traits<pointer>::rebind<const void>
const_void_pointer;
typedef Alloc::difference_type
2010-11-18 09:40:00 +08:00
| pointer_traits<pointer>::difference_type
difference_type;
typedef Alloc::size_type
| make_unsigned<difference_type>::type
size_type;
2010-05-12 03:42:16 +08:00
typedef Alloc::propagate_on_container_copy_assignment
| false_type propagate_on_container_copy_assignment;
typedef Alloc::propagate_on_container_move_assignment
| false_type propagate_on_container_move_assignment;
typedef Alloc::propagate_on_container_swap
| false_type propagate_on_container_swap;
typedef Alloc::is_always_equal
| is_empty is_always_equal;
2010-05-12 03:42:16 +08:00
template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>;
2010-05-12 03:42:16 +08:00
template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20
static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
2010-05-12 03:42:16 +08:00
static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
2010-05-12 03:42:16 +08:00
template <class T, class... Args>
static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
2010-05-12 03:42:16 +08:00
template <class T>
static void destroy(allocator_type& a, T* p); // constexpr in C++20
2010-05-12 03:42:16 +08:00
static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
2010-05-12 03:42:16 +08:00
};
template <>
class allocator<void> // deprecated in C++17, removed in C++20
2010-05-12 03:42:16 +08:00
{
public:
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template <class _Up> struct rebind {typedef allocator<_Up> other;};
};
template <class T>
class allocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer; // deprecated in C++17, removed in C++20
typedef const T* const_pointer; // deprecated in C++17, removed in C++20
typedef typename add_lvalue_reference<T>::type
reference; // deprecated in C++17, removed in C++20
typedef typename add_lvalue_reference<const T>::type
const_reference; // deprecated in C++17, removed in C++20
2010-05-12 03:42:16 +08:00
typedef T value_type;
template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
typedef true_type propagate_on_container_move_assignment;
typedef true_type is_always_equal;
2010-05-12 03:42:16 +08:00
constexpr allocator() noexcept; // constexpr in C++20
constexpr allocator(const allocator&) noexcept; // constexpr in C++20
template <class U>
constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
~allocator(); // constexpr in C++20
pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20
const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20
T* allocate(size_t n); // constexpr in C++20
void deallocate(T* p, size_t n) noexcept; // constexpr in C++20
size_type max_size() const noexcept; // deprecated in C++17, removed in C++20
template<class U, class... Args>
void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20
template <class U>
void destroy(U* p); // deprecated in C++17, removed in C++20
2010-05-12 03:42:16 +08:00
};
template <class T, class U>
bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
2010-05-12 03:42:16 +08:00
template <class T, class U>
bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
2010-05-12 03:42:16 +08:00
template <class OutputIterator, class T>
class raw_storage_iterator
: public iterator<output_iterator_tag,
T, // purposefully not C++03
ptrdiff_t, // purposefully not C++03
T*, // purposefully not C++03
raw_storage_iterator&> // purposefully not C++03
{
public:
explicit raw_storage_iterator(OutputIterator x);
raw_storage_iterator& operator*();
raw_storage_iterator& operator=(const T& element);
raw_storage_iterator& operator++();
raw_storage_iterator operator++(int);
};
template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
template <class T> void return_temporary_buffer(T* p) noexcept;
template <class T> T* addressof(T& r) noexcept;
template <class T> T* addressof(const T&& r) noexcept = delete;
2010-05-12 03:42:16 +08:00
template <class InputIterator, class ForwardIterator>
ForwardIterator
uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
template <class InputIterator, class Size, class ForwardIterator>
ForwardIterator
uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
2010-05-12 03:42:16 +08:00
template <class ForwardIterator, class T>
void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
template <class ForwardIterator, class Size, class T>
2010-11-19 00:13:03 +08:00
ForwardIterator
uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
2010-05-12 03:42:16 +08:00
template <class T, class ...Args>
constexpr T* construct_at(T* location, Args&& ...args); // since C++20
template <class T>
void destroy_at(T* location); // constexpr in C++20
template <class ForwardIterator>
void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
template <class ForwardIterator, class Size>
ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
template <class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
template <class InputIterator, class Size, class ForwardIterator>
pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
template <class ForwardIterator>
void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class Size>
ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
template <class ForwardIterator>
void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class Size>
ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
2010-05-12 03:42:16 +08:00
template<class X>
class auto_ptr // deprecated in C++11, removed in C++17
2010-05-12 03:42:16 +08:00
{
public:
typedef X element_type;
explicit auto_ptr(X* p =0) throw();
auto_ptr(auto_ptr&) throw();
template<class Y> auto_ptr(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr&) throw();
template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr_ref<X> r) throw();
~auto_ptr() throw();
typename add_lvalue_reference<X>::type operator*() const throw();
X* operator->() const throw();
X* get() const throw();
X* release() throw();
void reset(X* p =0) throw();
auto_ptr(auto_ptr_ref<X>) throw();
template<class Y> operator auto_ptr_ref<Y>() throw();
template<class Y> operator auto_ptr<Y>() throw();
};
2010-08-20 02:39:17 +08:00
template <class T>
struct default_delete
{
constexpr default_delete() noexcept = default;
template <class U> default_delete(const default_delete<U>&) noexcept;
2010-08-20 02:39:17 +08:00
void operator()(T*) const noexcept;
2010-08-20 02:39:17 +08:00
};
template <class T>
struct default_delete<T[]>
{
constexpr default_delete() noexcept = default;
void operator()(T*) const noexcept;
2010-08-20 02:39:17 +08:00
template <class U> void operator()(U*) const = delete;
};
template <class T, class D = default_delete<T>>
class unique_ptr
{
public:
typedef see below pointer;
typedef T element_type;
typedef D deleter_type;
// constructors
constexpr unique_ptr() noexcept;
explicit unique_ptr(pointer p) noexcept;
unique_ptr(pointer p, see below d1) noexcept;
unique_ptr(pointer p, see below d2) noexcept;
unique_ptr(unique_ptr&& u) noexcept;
unique_ptr(nullptr_t) noexcept : unique_ptr() { }
2010-08-20 02:39:17 +08:00
template <class U, class E>
unique_ptr(unique_ptr<U, E>&& u) noexcept;
2010-08-20 02:39:17 +08:00
template <class U>
unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
2010-08-20 02:39:17 +08:00
// destructor
~unique_ptr();
// assignment
unique_ptr& operator=(unique_ptr&& u) noexcept;
template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
unique_ptr& operator=(nullptr_t) noexcept;
2010-08-20 02:39:17 +08:00
// observers
typename add_lvalue_reference<T>::type operator*() const;
pointer operator->() const noexcept;
pointer get() const noexcept;
deleter_type& get_deleter() noexcept;
const deleter_type& get_deleter() const noexcept;
explicit operator bool() const noexcept;
2010-08-20 02:39:17 +08:00
// modifiers
pointer release() noexcept;
void reset(pointer p = pointer()) noexcept;
void swap(unique_ptr& u) noexcept;
2010-08-20 02:39:17 +08:00
};
template <class T, class D>
class unique_ptr<T[], D>
{
public:
typedef implementation-defined pointer;
typedef T element_type;
typedef D deleter_type;
// constructors
constexpr unique_ptr() noexcept;
explicit unique_ptr(pointer p) noexcept;
unique_ptr(pointer p, see below d) noexcept;
unique_ptr(pointer p, see below d) noexcept;
unique_ptr(unique_ptr&& u) noexcept;
unique_ptr(nullptr_t) noexcept : unique_ptr() { }
2010-08-20 02:39:17 +08:00
// destructor
~unique_ptr();
2010-08-20 02:39:17 +08:00
// assignment
unique_ptr& operator=(unique_ptr&& u) noexcept;
unique_ptr& operator=(nullptr_t) noexcept;
2010-08-20 02:39:17 +08:00
// observers
T& operator[](size_t i) const;
pointer get() const noexcept;
deleter_type& get_deleter() noexcept;
const deleter_type& get_deleter() const noexcept;
explicit operator bool() const noexcept;
2010-08-20 02:39:17 +08:00
// modifiers
pointer release() noexcept;
void reset(pointer p = pointer()) noexcept;
void reset(nullptr_t) noexcept;
template <class U> void reset(U) = delete;
void swap(unique_ptr& u) noexcept;
2010-08-20 02:39:17 +08:00
};
template <class T, class D>
void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
2010-08-20 02:39:17 +08:00
template <class T1, class D1, class T2, class D2>
bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T1, class D1, class T2, class D2>
bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
template <class T, class D>
bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
template <class T, class D>
bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
template <class T, class D>
bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
template <class T, class D>
bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
template <class T, class D>
bool operator<(const unique_ptr<T, D>& x, nullptr_t);
template <class T, class D>
bool operator<(nullptr_t, const unique_ptr<T, D>& y);
template <class T, class D>
bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
template <class T, class D>
bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
template <class T, class D>
bool operator>(const unique_ptr<T, D>& x, nullptr_t);
template <class T, class D>
bool operator>(nullptr_t, const unique_ptr<T, D>& y);
template <class T, class D>
bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
template <class T, class D>
bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
2010-08-20 02:39:17 +08:00
class bad_weak_ptr
: public std::exception
{
bad_weak_ptr() noexcept;
2010-08-20 02:39:17 +08:00
};
template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
template<class T> unique_ptr<T> make_unique(size_t n); // C++14
template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
template<class E, class T, class Y, class D>
basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
2010-08-20 02:39:17 +08:00
template<class T>
class shared_ptr
{
public:
typedef T element_type;
typedef weak_ptr<T> weak_type; // C++17
2010-08-20 02:39:17 +08:00
// constructors:
constexpr shared_ptr() noexcept;
2010-08-20 02:39:17 +08:00
template<class Y> explicit shared_ptr(Y* p);
template<class Y, class D> shared_ptr(Y* p, D d);
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
template <class D> shared_ptr(nullptr_t p, D d);
template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
shared_ptr(const shared_ptr& r) noexcept;
template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
shared_ptr(shared_ptr&& r) noexcept;
template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
2010-08-20 02:39:17 +08:00
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
2010-08-20 02:39:17 +08:00
template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
shared_ptr(nullptr_t) : shared_ptr() { }
// destructor:
~shared_ptr();
// assignment:
shared_ptr& operator=(const shared_ptr& r) noexcept;
template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
shared_ptr& operator=(shared_ptr&& r) noexcept;
2010-08-20 02:39:17 +08:00
template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
2010-08-20 02:39:17 +08:00
template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
// modifiers:
void swap(shared_ptr& r) noexcept;
void reset() noexcept;
2010-08-20 02:39:17 +08:00
template<class Y> void reset(Y* p);
template<class Y, class D> void reset(Y* p, D d);
template<class Y, class D, class A> void reset(Y* p, D d, A a);
// observers:
T* get() const noexcept;
T& operator*() const noexcept;
T* operator->() const noexcept;
long use_count() const noexcept;
bool unique() const noexcept;
explicit operator bool() const noexcept;
template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
2010-08-20 02:39:17 +08:00
};
template<class T>
shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
template<class T, class D>
shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
2010-08-20 02:39:17 +08:00
// shared_ptr comparisons:
template<class T, class U>
bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
2010-08-20 02:39:17 +08:00
template<class T, class U>
bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
2010-08-20 02:39:17 +08:00
template<class T, class U>
bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
2010-08-20 02:39:17 +08:00
template<class T, class U>
bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
2010-08-20 02:39:17 +08:00
template<class T, class U>
bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
2010-08-20 02:39:17 +08:00
template<class T, class U>
bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
template <class T>
bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
template <class T>
bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
template <class T>
bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
template <class T>
bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
template <class T>
bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
template <class T>
bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
template <class T>
bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
template <class T>
bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
template <class T>
bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
template <class T>
bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
template <class T>
bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
template <class T>
bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
2010-08-20 02:39:17 +08:00
// shared_ptr specialized algorithms:
template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
2010-08-20 02:39:17 +08:00
// shared_ptr casts:
template<class T, class U>
shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
2010-08-20 02:39:17 +08:00
template<class T, class U>
shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
2010-08-20 02:39:17 +08:00
template<class T, class U>
shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
2010-08-20 02:39:17 +08:00
// shared_ptr I/O:
template<class E, class T, class Y>
basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
// shared_ptr get_deleter:
template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
2010-08-20 02:39:17 +08:00
template<class T, class... Args>
shared_ptr<T> make_shared(Args&&... args);
template<class T, class A, class... Args>
shared_ptr<T> allocate_shared(const A& a, Args&&... args);
template<class T>
class weak_ptr
{
public:
typedef T element_type;
// constructors
constexpr weak_ptr() noexcept;
template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
weak_ptr(weak_ptr const& r) noexcept;
template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
weak_ptr(weak_ptr&& r) noexcept; // C++14
template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
2010-08-20 02:39:17 +08:00
// destructor
~weak_ptr();
// assignment
weak_ptr& operator=(weak_ptr const& r) noexcept;
template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
2010-08-20 02:39:17 +08:00
// modifiers
void swap(weak_ptr& r) noexcept;
void reset() noexcept;
2010-08-20 02:39:17 +08:00
// observers
long use_count() const noexcept;
bool expired() const noexcept;
shared_ptr<T> lock() const noexcept;
template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
2010-08-20 02:39:17 +08:00
};
template<class T>
weak_ptr(shared_ptr<T>) -> weak_ptr<T>;
2010-08-20 02:39:17 +08:00
// weak_ptr specialized algorithms:
template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
2010-08-20 02:39:17 +08:00
// class owner_less:
template<class T> struct owner_less;
template<class T>
struct owner_less<shared_ptr<T> >
2010-08-20 02:39:17 +08:00
: binary_function<shared_ptr<T>, shared_ptr<T>, bool>
{
typedef bool result_type;
bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
2010-08-20 02:39:17 +08:00
};
template<class T>
struct owner_less<weak_ptr<T> >
2010-08-20 02:39:17 +08:00
: binary_function<weak_ptr<T>, weak_ptr<T>, bool>
{
typedef bool result_type;
bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
};
template <> // Added in C++14
struct owner_less<void>
{
template <class _Tp, class _Up>
bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
template <class _Tp, class _Up>
bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
template <class _Tp, class _Up>
bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
template <class _Tp, class _Up>
bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
typedef void is_transparent;
2010-08-20 02:39:17 +08:00
};
template<class T>
class enable_shared_from_this
{
protected:
constexpr enable_shared_from_this() noexcept;
enable_shared_from_this(enable_shared_from_this const&) noexcept;
enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
2010-08-20 02:39:17 +08:00
~enable_shared_from_this();
public:
shared_ptr<T> shared_from_this();
shared_ptr<T const> shared_from_this() const;
};
template<class T>
bool atomic_is_lock_free(const shared_ptr<T>* p);
template<class T>
shared_ptr<T> atomic_load(const shared_ptr<T>* p);
template<class T>
shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
template<class T>
void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
template<class T>
void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
template<class T>
shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
template<class T>
shared_ptr<T>
atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
template<class T>
bool
atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
template<class T>
bool
atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
template<class T>
bool
atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
shared_ptr<T> w, memory_order success,
memory_order failure);
template<class T>
bool
atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
shared_ptr<T> w, memory_order success,
memory_order failure);
// Hash support
template <class T> struct hash;
template <class T, class D> struct hash<unique_ptr<T, D> >;
template <class T> struct hash<shared_ptr<T> >;
template <class T, class Alloc>
inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
2010-08-20 02:39:17 +08:00
// Pointer safety
enum class pointer_safety { relaxed, preferred, strict };
void declare_reachable(void *p);
template <class T> T *undeclare_reachable(T *p);
void declare_no_pointers(char *p, size_t n);
void undeclare_no_pointers(char *p, size_t n);
pointer_safety get_pointer_safety() noexcept;
2010-08-20 02:39:17 +08:00
2010-05-12 03:42:16 +08:00
void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
} // std
*/
#include <__config>
#include <__availability>
2010-05-12 03:42:16 +08:00
#include <type_traits>
#include <typeinfo>
#include <compare>
2010-05-12 03:42:16 +08:00
#include <cstddef>
#include <cstdint>
#include <new>
#include <utility>
#include <iterator>
#include <__functional_base>
#include <iosfwd>
#include <tuple>
#include <stdexcept>
#include <cstring>
#include <__memory/allocator.h>
#include <__memory/allocator_traits.h>
#include <__memory/auto_ptr.h>
#include <__memory/base.h>
#include <__memory/compressed_pair.h>
#include <__memory/pointer_traits.h>
#include <__memory/raw_storage_iterator.h>
#include <__memory/temporary_buffer.h>
#include <__memory/uninitialized_algorithms.h>
#include <__memory/unique_ptr.h>
#include <__memory/utilities.h>
#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
# include <atomic>
#endif
#include <version>
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2010-05-12 03:42:16 +08:00
#pragma GCC system_header
#endif
2010-05-12 03:42:16 +08:00
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
2010-05-12 03:42:16 +08:00
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _ValueType>
inline _LIBCPP_INLINE_VISIBILITY
_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
#if !defined(_LIBCPP_HAS_NO_THREADS) && \
defined(__ATOMIC_RELAXED) && \
(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
return __atomic_load_n(__value, __ATOMIC_RELAXED);
#else
return *__value;
#endif
}
template <class _ValueType>
inline _LIBCPP_INLINE_VISIBILITY
_ValueType __libcpp_acquire_load(_ValueType const* __value) {
#if !defined(_LIBCPP_HAS_NO_THREADS) && \
defined(__ATOMIC_ACQUIRE) && \
(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
#else
return *__value;
#endif
}
template <class _Alloc, class _Ptr>
_LIBCPP_INLINE_VISIBILITY
void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
static_assert(__is_cpp17_move_insertable<_Alloc>::value,
"The specified type does not meet the requirements of Cpp17MoveInsertable");
typedef allocator_traits<_Alloc> _Traits;
for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
_Traits::construct(__a, _VSTD::__to_address(__begin2),
#ifdef _LIBCPP_NO_EXCEPTIONS
_VSTD::move(*__begin1)
#else
_VSTD::move_if_noexcept(*__begin1)
#endif
);
}
}
template <class _Alloc, class _Tp, typename enable_if<
(__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
is_trivially_move_constructible<_Tp>::value
>::type>
_LIBCPP_INLINE_VISIBILITY
void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
ptrdiff_t _Np = __end1 - __begin1;
if (_Np > 0) {
_VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
__begin2 += _Np;
}
}
template <class _Alloc, class _Iter, class _Ptr>
_LIBCPP_INLINE_VISIBILITY
void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
typedef allocator_traits<_Alloc> _Traits;
for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
_Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
}
}
template <class _Alloc, class _Source, class _Dest,
class _RawSource = typename remove_const<_Source>::type,
class _RawDest = typename remove_const<_Dest>::type,
class =
typename enable_if<
is_trivially_copy_constructible<_Dest>::value &&
is_same<_RawSource, _RawDest>::value &&
(__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
>::type>
_LIBCPP_INLINE_VISIBILITY
void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
ptrdiff_t _Np = __end1 - __begin1;
if (_Np > 0) {
_VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
__begin2 += _Np;
}
}
template <class _Alloc, class _Ptr>
_LIBCPP_INLINE_VISIBILITY
void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
static_assert(__is_cpp17_move_insertable<_Alloc>::value,
"The specified type does not meet the requirements of Cpp17MoveInsertable");
typedef allocator_traits<_Alloc> _Traits;
while (__end1 != __begin1) {
_Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
#ifdef _LIBCPP_NO_EXCEPTIONS
_VSTD::move(*--__end1)
#else
_VSTD::move_if_noexcept(*--__end1)
#endif
);
--__end2;
}
}
template <class _Alloc, class _Tp, class = typename enable_if<
(__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
is_trivially_move_constructible<_Tp>::value
>::type>
_LIBCPP_INLINE_VISIBILITY
void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
ptrdiff_t _Np = __end1 - __begin1;
__end2 -= _Np;
if (_Np > 0)
_VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
}
2010-05-12 03:42:16 +08:00
struct __destruct_n
{
private:
size_t __size_;
2010-05-12 03:42:16 +08:00
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
{for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
2010-05-12 03:42:16 +08:00
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{}
_LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
{++__size_;}
_LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{}
_LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
{__size_ = __s;}
_LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{}
public:
_LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
: __size_(__s) {}
2010-05-12 03:42:16 +08:00
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
{__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2010-05-12 03:42:16 +08:00
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
{__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2010-05-12 03:42:16 +08:00
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
{__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
2010-05-12 03:42:16 +08:00
};
template <class _Alloc>
class __allocator_destructor
{
typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
2010-05-12 03:42:16 +08:00
public:
typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
2010-05-12 03:42:16 +08:00
private:
_Alloc& __alloc_;
size_type __s_;
public:
_LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
_NOEXCEPT
2010-05-12 03:42:16 +08:00
: __alloc_(__a), __s_(__s) {}
_LIBCPP_INLINE_VISIBILITY
void operator()(pointer __p) _NOEXCEPT
{__alloc_traits::deallocate(__alloc_, __p, __s_);}
2010-05-12 03:42:16 +08:00
};
// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
// should be sufficient for thread safety.
// See https://llvm.org/PR22803
#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
&& defined(__ATOMIC_RELAXED) \
&& defined(__ATOMIC_ACQ_REL)
# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
#elif defined(_LIBCPP_COMPILER_GCC)
# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
#endif
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY _Tp
__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
{
#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
#else
return __t += 1;
#endif
}
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY _Tp
__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
{
#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
#else
return __t -= 1;
#endif
}
class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
2010-05-12 03:42:16 +08:00
: public std::exception
{
public:
bad_weak_ptr() _NOEXCEPT = default;
bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default;
virtual ~bad_weak_ptr() _NOEXCEPT;
virtual const char* what() const _NOEXCEPT;
2010-05-12 03:42:16 +08:00
};
_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
void __throw_bad_weak_ptr()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw bad_weak_ptr();
#else
_VSTD::abort();
#endif
}
template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
2010-05-12 03:42:16 +08:00
class _LIBCPP_TYPE_VIS __shared_count
2010-05-12 03:42:16 +08:00
{
__shared_count(const __shared_count&);
__shared_count& operator=(const __shared_count&);
protected:
long __shared_owners_;
virtual ~__shared_count();
private:
virtual void __on_zero_shared() _NOEXCEPT = 0;
2010-05-12 03:42:16 +08:00
public:
_LIBCPP_INLINE_VISIBILITY
explicit __shared_count(long __refs = 0) _NOEXCEPT
2010-05-12 03:42:16 +08:00
: __shared_owners_(__refs) {}
#if defined(_LIBCPP_BUILDING_LIBRARY) && \
defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
void __add_shared() _NOEXCEPT;
bool __release_shared() _NOEXCEPT;
#else
_LIBCPP_INLINE_VISIBILITY
void __add_shared() _NOEXCEPT {
__libcpp_atomic_refcount_increment(__shared_owners_);
}
_LIBCPP_INLINE_VISIBILITY
bool __release_shared() _NOEXCEPT {
if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
__on_zero_shared();
return true;
}
return false;
}
#endif
_LIBCPP_INLINE_VISIBILITY
long use_count() const _NOEXCEPT {
return __libcpp_relaxed_load(&__shared_owners_) + 1;
}
2010-05-12 03:42:16 +08:00
};
class _LIBCPP_TYPE_VIS __shared_weak_count
2010-05-12 03:42:16 +08:00
: private __shared_count
{
long __shared_weak_owners_;
public:
_LIBCPP_INLINE_VISIBILITY
explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
2010-05-12 03:42:16 +08:00
: __shared_count(__refs),
__shared_weak_owners_(__refs) {}
protected:
virtual ~__shared_weak_count();
public:
#if defined(_LIBCPP_BUILDING_LIBRARY) && \
defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
void __add_shared() _NOEXCEPT;
void __add_weak() _NOEXCEPT;
void __release_shared() _NOEXCEPT;
#else
_LIBCPP_INLINE_VISIBILITY
void __add_shared() _NOEXCEPT {
__shared_count::__add_shared();
}
_LIBCPP_INLINE_VISIBILITY
void __add_weak() _NOEXCEPT {
__libcpp_atomic_refcount_increment(__shared_weak_owners_);
}
_LIBCPP_INLINE_VISIBILITY
void __release_shared() _NOEXCEPT {
if (__shared_count::__release_shared())
__release_weak();
}
#endif
void __release_weak() _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
long use_count() const _NOEXCEPT {return __shared_count::use_count();}
__shared_weak_count* lock() _NOEXCEPT;
2010-05-12 03:42:16 +08:00
virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
2010-05-12 03:42:16 +08:00
private:
virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
2010-05-12 03:42:16 +08:00
};
template <class _Tp, class _Dp, class _Alloc>
class __shared_ptr_pointer
: public __shared_weak_count
{
__compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
public:
_LIBCPP_INLINE_VISIBILITY
2010-05-12 03:42:16 +08:00
__shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
: __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
2010-05-12 03:42:16 +08:00
#ifndef _LIBCPP_NO_RTTI
virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
#endif
2010-05-12 03:42:16 +08:00
private:
virtual void __on_zero_shared() _NOEXCEPT;
virtual void __on_zero_shared_weak() _NOEXCEPT;
2010-05-12 03:42:16 +08:00
};
#ifndef _LIBCPP_NO_RTTI
2010-05-12 03:42:16 +08:00
template <class _Tp, class _Dp, class _Alloc>
const void*
__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
2010-05-12 03:42:16 +08:00
}
#endif // _LIBCPP_NO_RTTI
2010-05-12 03:42:16 +08:00
template <class _Tp, class _Dp, class _Alloc>
void
__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
__data_.first().second()(__data_.first().first());
__data_.first().second().~_Dp();
}
template <class _Tp, class _Dp, class _Alloc>
void
__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
typedef allocator_traits<_Al> _ATraits;
Add support for "fancy" pointers to shared_ptr. Fixes PR20616 Summary: This patch add support for "fancy pointers/allocators" as well as fixing support for shared_pointer and "minimal" allocators. Fancy pointers are class types that meet the NullablePointer requirements. In our case they are created by fancy allocators. `support/min_allocator.h` is an archetype for these types. There are three types of changes made in this patch: 1. `_Alloc::template rebind<T>::other` -> `__allocator_traits_rebind<_Alloc, T>::type`. This change was made because allocators don't need a rebind template. `__allocator_traits_rebind` is used instead of `allocator_traits::rebind` because use of `allocator_traits::rebind` requires a workaround for when template aliases are unavailable. 2. `a.deallocate(this, 1)` -> `a.deallocate(pointer_traits<self>::pointer_to(*this), 1)`. This change change is made because fancy pointers aren't always constructible from raw pointers. 3. `p.get()` -> `addressof(*p.get())`. Fancy pointers aren't actually a pointer. When we need a "real" pointer we take the address of dereferencing the fancy pointer. This should give us the actual raw pointer. Test Plan: Tests were added using `support/min_allocator.h` to each affected shared_ptr overload and creation function. These tests can only be executed in C++11 or greater since min_allocator is only available then. A extra test was added for the non-variadic versions of allocate_shared. Reviewers: danalbert, mclow.lists Reviewed By: mclow.lists Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D4859 llvm-svn: 220469
2014-10-23 12:12:28 +08:00
typedef pointer_traits<typename _ATraits::pointer> _PTraits;
_Al __a(__data_.second());
2010-05-12 03:42:16 +08:00
__data_.second().~_Alloc();
Add support for "fancy" pointers to shared_ptr. Fixes PR20616 Summary: This patch add support for "fancy pointers/allocators" as well as fixing support for shared_pointer and "minimal" allocators. Fancy pointers are class types that meet the NullablePointer requirements. In our case they are created by fancy allocators. `support/min_allocator.h` is an archetype for these types. There are three types of changes made in this patch: 1. `_Alloc::template rebind<T>::other` -> `__allocator_traits_rebind<_Alloc, T>::type`. This change was made because allocators don't need a rebind template. `__allocator_traits_rebind` is used instead of `allocator_traits::rebind` because use of `allocator_traits::rebind` requires a workaround for when template aliases are unavailable. 2. `a.deallocate(this, 1)` -> `a.deallocate(pointer_traits<self>::pointer_to(*this), 1)`. This change change is made because fancy pointers aren't always constructible from raw pointers. 3. `p.get()` -> `addressof(*p.get())`. Fancy pointers aren't actually a pointer. When we need a "real" pointer we take the address of dereferencing the fancy pointer. This should give us the actual raw pointer. Test Plan: Tests were added using `support/min_allocator.h` to each affected shared_ptr overload and creation function. These tests can only be executed in C++11 or greater since min_allocator is only available then. A extra test was added for the non-variadic versions of allocate_shared. Reviewers: danalbert, mclow.lists Reviewed By: mclow.lists Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D4859 llvm-svn: 220469
2014-10-23 12:12:28 +08:00
__a.deallocate(_PTraits::pointer_to(*this), 1);
2010-05-12 03:42:16 +08:00
}
template <class _Tp, class _Alloc>
struct __shared_ptr_emplace
: __shared_weak_count
2010-05-12 03:42:16 +08:00
{
template<class ..._Args>
_LIBCPP_HIDE_FROM_ABI
explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
: __storage_(_VSTD::move(__a))
{
#if _LIBCPP_STD_VER > 17
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
_TpAlloc __tmp(*__get_alloc());
allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
#else
::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
#endif
}
_LIBCPP_HIDE_FROM_ABI
_Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); }
_LIBCPP_HIDE_FROM_ABI
_Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); }
2010-05-12 03:42:16 +08:00
private:
virtual void __on_zero_shared() _NOEXCEPT {
#if _LIBCPP_STD_VER > 17
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
_TpAlloc __tmp(*__get_alloc());
allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
#else
__get_elem()->~_Tp();
#endif
}
2010-05-12 03:42:16 +08:00
virtual void __on_zero_shared_weak() _NOEXCEPT {
using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type;
using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer;
_ControlBlockAlloc __tmp(*__get_alloc());
__storage_.~_Storage();
allocator_traits<_ControlBlockAlloc>::deallocate(__tmp,
pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1);
}
2010-05-12 03:42:16 +08:00
// This class implements the control block for non-array shared pointers created
// through `std::allocate_shared` and `std::make_shared`.
//
// In previous versions of the library, we used a compressed pair to store
// both the _Alloc and the _Tp. This implies using EBO, which is incompatible
// with Allocator construction for _Tp. To allow implementing P0674 in C++20,
// we now use a properly aligned char buffer while making sure that we maintain
// the same layout that we had when we used a compressed pair.
using _CompressedPair = __compressed_pair<_Alloc, _Tp>;
struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
char __blob_[sizeof(_CompressedPair)];
_LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) {
::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a));
}
_LIBCPP_HIDE_FROM_ABI ~_Storage() {
__get_alloc()->~_Alloc();
}
_Alloc* __get_alloc() _NOEXCEPT {
_CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair);
_Alloc *__alloc = reinterpret_cast<_Alloc*>(__first);
return __alloc;
}
_LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT {
_CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
_Tp *__elem = reinterpret_cast<_Tp*>(__second);
return __elem;
}
};
static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
static_assert(sizeof(_Storage) == sizeof(_CompressedPair), "");
_Storage __storage_;
};
2010-05-12 03:42:16 +08:00
struct __shared_ptr_dummy_rebind_allocator_type;
template <>
class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
{
public:
template <class _Other>
struct rebind
{
typedef allocator<_Other> other;
};
};
template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
2010-05-12 03:42:16 +08:00
template<class _Tp, class _Up>
struct __compatible_with
#if _LIBCPP_STD_VER > 14
: is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {};
#else
: is_convertible<_Tp*, _Up*> {};
#endif // _LIBCPP_STD_VER > 14
template <class _Dp, class _Pt,
class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))>
static true_type __well_formed_deleter_test(int);
template <class, class>
static false_type __well_formed_deleter_test(...);
template <class _Dp, class _Pt>
struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {};
template<class _Dp, class _Tp, class _Yp>
struct __shared_ptr_deleter_ctor_reqs
{
static const bool value = __compatible_with<_Tp, _Yp>::value &&
is_move_constructible<_Dp>::value &&
__well_formed_deleter<_Dp, _Tp*>::value;
};
#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
#else
# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI
#endif
2010-05-12 03:42:16 +08:00
template<class _Tp>
class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr
2010-05-12 03:42:16 +08:00
{
public:
#if _LIBCPP_STD_VER > 14
typedef weak_ptr<_Tp> weak_type;
typedef remove_extent_t<_Tp> element_type;
#else
typedef _Tp element_type;
#endif
2010-05-12 03:42:16 +08:00
private:
element_type* __ptr_;
__shared_weak_count* __cntrl_;
struct __nat {int __for_bool_;};
public:
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
template<class _Yp>
explicit shared_ptr(_Yp* __p,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat());
template<class _Yp, class _Dp>
shared_ptr(_Yp* __p, _Dp __d,
typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
template<class _Yp, class _Dp, class _Alloc>
shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat());
2010-05-12 03:42:16 +08:00
template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
shared_ptr(const shared_ptr& __r) _NOEXCEPT;
2010-05-12 03:42:16 +08:00
template<class _Yp>
_LIBCPP_INLINE_VISIBILITY
2010-05-12 03:42:16 +08:00
shared_ptr(const shared_ptr<_Yp>& __r,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
_NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
shared_ptr(shared_ptr&& __r) _NOEXCEPT;
template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat())
_NOEXCEPT;
2010-05-12 03:42:16 +08:00
template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
template<class _Yp>
shared_ptr(auto_ptr<_Yp>&& __r,
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
#endif
template <class _Yp, class _Dp>
shared_ptr(unique_ptr<_Yp, _Dp>&&,
typename enable_if
<
!is_lvalue_reference<_Dp>::value &&
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
__nat
>::type = __nat());
template <class _Yp, class _Dp>
shared_ptr(unique_ptr<_Yp, _Dp>&&,
typename enable_if
<
is_lvalue_reference<_Dp>::value &&
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
__nat
>::type = __nat());
2010-05-12 03:42:16 +08:00
~shared_ptr();
_LIBCPP_INLINE_VISIBILITY
shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
template<class _Yp>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
shared_ptr&
>::type
_LIBCPP_INLINE_VISIBILITY
operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
template<class _Yp>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
shared_ptr&
>::type
_LIBCPP_INLINE_VISIBILITY
operator=(shared_ptr<_Yp>&& __r);
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
template<class _Yp>
_LIBCPP_INLINE_VISIBILITY
typename enable_if
<
!is_array<_Yp>::value &&
is_convertible<_Yp*, element_type*>::value,
shared_ptr
>::type&
operator=(auto_ptr<_Yp>&& __r);
2010-05-12 03:42:16 +08:00
#endif
template <class _Yp, class _Dp>
typename enable_if
<
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
shared_ptr&
>::type
_LIBCPP_INLINE_VISIBILITY
operator=(unique_ptr<_Yp, _Dp>&& __r);
2010-05-12 03:42:16 +08:00
_LIBCPP_INLINE_VISIBILITY
void swap(shared_ptr& __r) _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
void reset() _NOEXCEPT;
template<class _Yp>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
void
>::type
_LIBCPP_INLINE_VISIBILITY
reset(_Yp* __p);
template<class _Yp, class _Dp>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
void
>::type
_LIBCPP_INLINE_VISIBILITY
reset(_Yp* __p, _Dp __d);
template<class _Yp, class _Dp, class _Alloc>
typename enable_if
<
__compatible_with<_Yp, element_type>::value,
void
>::type
_LIBCPP_INLINE_VISIBILITY
reset(_Yp* __p, _Dp __d, _Alloc __a);
2010-05-12 03:42:16 +08:00
_LIBCPP_INLINE_VISIBILITY
element_type* get() const _NOEXCEPT {return __ptr_;}
_LIBCPP_INLINE_VISIBILITY
typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
{return *__ptr_;}
_LIBCPP_INLINE_VISIBILITY
element_type* operator->() const _NOEXCEPT
{
static_assert(!_VSTD::is_array<_Tp>::value,
"std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
return __ptr_;
}
_LIBCPP_INLINE_VISIBILITY
long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
_LIBCPP_INLINE_VISIBILITY
bool unique() const _NOEXCEPT {return use_count() == 1;}
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
template <class _Up>
_LIBCPP_INLINE_VISIBILITY
bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __cntrl_ < __p.__cntrl_;}
template <class _Up>
_LIBCPP_INLINE_VISIBILITY
bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __cntrl_ < __p.__cntrl_;}
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
_LIBCPP_INLINE_VISIBILITY
bool
__owner_equivalent(const shared_ptr& __p) const
{return __cntrl_ == __p.__cntrl_;}
2010-05-12 03:42:16 +08:00
#if _LIBCPP_STD_VER > 14
typename add_lvalue_reference<element_type>::type
_LIBCPP_INLINE_VISIBILITY
operator[](ptrdiff_t __i) const
{
static_assert(_VSTD::is_array<_Tp>::value,
"std::shared_ptr<T>::operator[] is only valid when T is an array type.");
return __ptr_[__i];
}
#endif
#ifndef _LIBCPP_NO_RTTI
2010-05-12 03:42:16 +08:00
template <class _Dp>
_LIBCPP_INLINE_VISIBILITY
_Dp* __get_deleter() const _NOEXCEPT
{return static_cast<_Dp*>(__cntrl_
? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
: nullptr);}
#endif // _LIBCPP_NO_RTTI
2010-05-12 03:42:16 +08:00
template<class _Yp, class _CntrlBlk>
static shared_ptr<_Tp>
__create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT
{
shared_ptr<_Tp> __r;
__r.__ptr_ = __p;
__r.__cntrl_ = __cntrl;
__r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
return __r;
}
2010-05-12 03:42:16 +08:00
private:
template <class _Yp, bool = is_function<_Yp>::value>
struct __shared_ptr_default_allocator
{
typedef allocator<_Yp> type;
};
template <class _Yp>
struct __shared_ptr_default_allocator<_Yp, true>
{
typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
};
2010-05-12 03:42:16 +08:00
template <class _Yp, class _OrigPtr>
_LIBCPP_INLINE_VISIBILITY
typename enable_if<is_convertible<_OrigPtr*,
const enable_shared_from_this<_Yp>*
>::value,
void>::type
__enable_weak_this(const enable_shared_from_this<_Yp>* __e,
_OrigPtr* __ptr) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
typedef typename remove_cv<_Yp>::type _RawYp;
if (__e && __e->__weak_this_.expired())
{
__e->__weak_this_ = shared_ptr<_RawYp>(*this,
const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
}
2010-05-12 03:42:16 +08:00
}
_LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
2010-05-12 03:42:16 +08:00
template <class, class _Yp>
struct __shared_ptr_default_delete
: default_delete<_Yp> {};
template <class _Yp, class _Un, size_t _Sz>
struct __shared_ptr_default_delete<_Yp[_Sz], _Un>
: default_delete<_Yp[]> {};
template <class _Yp, class _Un>
struct __shared_ptr_default_delete<_Yp[], _Un>
: default_delete<_Yp[]> {};
template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
2010-05-12 03:42:16 +08:00
};
#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
template<class _Tp>
shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>;
template<class _Tp, class _Dp>
shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
#endif
2010-05-12 03:42:16 +08:00
template<class _Tp>
inline
_LIBCPP_CONSTEXPR
shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
: __ptr_(nullptr),
__cntrl_(nullptr)
2010-05-12 03:42:16 +08:00
{
}
template<class _Tp>
inline
_LIBCPP_CONSTEXPR
shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
: __ptr_(nullptr),
__cntrl_(nullptr)
2010-05-12 03:42:16 +08:00
{
}
template<class _Tp>
template<class _Yp>
shared_ptr<_Tp>::shared_ptr(_Yp* __p,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
2010-05-12 03:42:16 +08:00
: __ptr_(__p)
{
unique_ptr<_Yp> __hold(__p);
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
__cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
2010-05-12 03:42:16 +08:00
__hold.release();
__enable_weak_this(__p, __p);
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
template<class _Yp, class _Dp>
shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
2010-05-12 03:42:16 +08:00
: __ptr_(__p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_NO_EXCEPTIONS
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
#ifndef _LIBCPP_CXX03_LANG
__cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
#else
__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
#endif // not _LIBCPP_CXX03_LANG
__enable_weak_this(__p, __p);
2010-05-12 03:42:16 +08:00
#ifndef _LIBCPP_NO_EXCEPTIONS
}
catch (...)
{
__d(__p);
throw;
}
#endif // _LIBCPP_NO_EXCEPTIONS
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
template<class _Dp>
shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
: __ptr_(nullptr)
2010-05-12 03:42:16 +08:00
{
#ifndef _LIBCPP_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_NO_EXCEPTIONS
typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
#ifndef _LIBCPP_CXX03_LANG
__cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT());
#else
__cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
#endif // not _LIBCPP_CXX03_LANG
2010-05-12 03:42:16 +08:00
#ifndef _LIBCPP_NO_EXCEPTIONS
}
catch (...)
{
__d(__p);
throw;
}
#endif // _LIBCPP_NO_EXCEPTIONS
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
template<class _Yp, class _Dp, class _Alloc>
shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type)
2010-05-12 03:42:16 +08:00
: __ptr_(__p)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_NO_EXCEPTIONS
2010-05-12 03:42:16 +08:00
typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Add support for "fancy" pointers to shared_ptr. Fixes PR20616 Summary: This patch add support for "fancy pointers/allocators" as well as fixing support for shared_pointer and "minimal" allocators. Fancy pointers are class types that meet the NullablePointer requirements. In our case they are created by fancy allocators. `support/min_allocator.h` is an archetype for these types. There are three types of changes made in this patch: 1. `_Alloc::template rebind<T>::other` -> `__allocator_traits_rebind<_Alloc, T>::type`. This change was made because allocators don't need a rebind template. `__allocator_traits_rebind` is used instead of `allocator_traits::rebind` because use of `allocator_traits::rebind` requires a workaround for when template aliases are unavailable. 2. `a.deallocate(this, 1)` -> `a.deallocate(pointer_traits<self>::pointer_to(*this), 1)`. This change change is made because fancy pointers aren't always constructible from raw pointers. 3. `p.get()` -> `addressof(*p.get())`. Fancy pointers aren't actually a pointer. When we need a "real" pointer we take the address of dereferencing the fancy pointer. This should give us the actual raw pointer. Test Plan: Tests were added using `support/min_allocator.h` to each affected shared_ptr overload and creation function. These tests can only be executed in C++11 or greater since min_allocator is only available then. A extra test was added for the non-variadic versions of allocate_shared. Reviewers: danalbert, mclow.lists Reviewed By: mclow.lists Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D4859 llvm-svn: 220469
2014-10-23 12:12:28 +08:00
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
2010-05-12 03:42:16 +08:00
typedef __allocator_destructor<_A2> _D2;
_A2 __a2(__a);
unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
::new ((void*)_VSTD::addressof(*__hold2.get()))
#ifndef _LIBCPP_CXX03_LANG
_CntrlBlk(__p, _VSTD::move(__d), __a);
#else
_CntrlBlk(__p, __d, __a);
#endif // not _LIBCPP_CXX03_LANG
Add support for "fancy" pointers to shared_ptr. Fixes PR20616 Summary: This patch add support for "fancy pointers/allocators" as well as fixing support for shared_pointer and "minimal" allocators. Fancy pointers are class types that meet the NullablePointer requirements. In our case they are created by fancy allocators. `support/min_allocator.h` is an archetype for these types. There are three types of changes made in this patch: 1. `_Alloc::template rebind<T>::other` -> `__allocator_traits_rebind<_Alloc, T>::type`. This change was made because allocators don't need a rebind template. `__allocator_traits_rebind` is used instead of `allocator_traits::rebind` because use of `allocator_traits::rebind` requires a workaround for when template aliases are unavailable. 2. `a.deallocate(this, 1)` -> `a.deallocate(pointer_traits<self>::pointer_to(*this), 1)`. This change change is made because fancy pointers aren't always constructible from raw pointers. 3. `p.get()` -> `addressof(*p.get())`. Fancy pointers aren't actually a pointer. When we need a "real" pointer we take the address of dereferencing the fancy pointer. This should give us the actual raw pointer. Test Plan: Tests were added using `support/min_allocator.h` to each affected shared_ptr overload and creation function. These tests can only be executed in C++11 or greater since min_allocator is only available then. A extra test was added for the non-variadic versions of allocate_shared. Reviewers: danalbert, mclow.lists Reviewed By: mclow.lists Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D4859 llvm-svn: 220469
2014-10-23 12:12:28 +08:00
__cntrl_ = _VSTD::addressof(*__hold2.release());
__enable_weak_this(__p, __p);
2010-05-12 03:42:16 +08:00
#ifndef _LIBCPP_NO_EXCEPTIONS
}
catch (...)
{
__d(__p);
throw;
}
#endif // _LIBCPP_NO_EXCEPTIONS
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
template<class _Dp, class _Alloc>
shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
: __ptr_(nullptr)
2010-05-12 03:42:16 +08:00
{
#ifndef _LIBCPP_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_NO_EXCEPTIONS
2010-05-12 03:42:16 +08:00
typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Add support for "fancy" pointers to shared_ptr. Fixes PR20616 Summary: This patch add support for "fancy pointers/allocators" as well as fixing support for shared_pointer and "minimal" allocators. Fancy pointers are class types that meet the NullablePointer requirements. In our case they are created by fancy allocators. `support/min_allocator.h` is an archetype for these types. There are three types of changes made in this patch: 1. `_Alloc::template rebind<T>::other` -> `__allocator_traits_rebind<_Alloc, T>::type`. This change was made because allocators don't need a rebind template. `__allocator_traits_rebind` is used instead of `allocator_traits::rebind` because use of `allocator_traits::rebind` requires a workaround for when template aliases are unavailable. 2. `a.deallocate(this, 1)` -> `a.deallocate(pointer_traits<self>::pointer_to(*this), 1)`. This change change is made because fancy pointers aren't always constructible from raw pointers. 3. `p.get()` -> `addressof(*p.get())`. Fancy pointers aren't actually a pointer. When we need a "real" pointer we take the address of dereferencing the fancy pointer. This should give us the actual raw pointer. Test Plan: Tests were added using `support/min_allocator.h` to each affected shared_ptr overload and creation function. These tests can only be executed in C++11 or greater since min_allocator is only available then. A extra test was added for the non-variadic versions of allocate_shared. Reviewers: danalbert, mclow.lists Reviewed By: mclow.lists Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D4859 llvm-svn: 220469
2014-10-23 12:12:28 +08:00
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
2010-05-12 03:42:16 +08:00
typedef __allocator_destructor<_A2> _D2;
_A2 __a2(__a);
unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
::new ((void*)_VSTD::addressof(*__hold2.get()))
#ifndef _LIBCPP_CXX03_LANG
_CntrlBlk(__p, _VSTD::move(__d), __a);
#else
_CntrlBlk(__p, __d, __a);
#endif // not _LIBCPP_CXX03_LANG
Add support for "fancy" pointers to shared_ptr. Fixes PR20616 Summary: This patch add support for "fancy pointers/allocators" as well as fixing support for shared_pointer and "minimal" allocators. Fancy pointers are class types that meet the NullablePointer requirements. In our case they are created by fancy allocators. `support/min_allocator.h` is an archetype for these types. There are three types of changes made in this patch: 1. `_Alloc::template rebind<T>::other` -> `__allocator_traits_rebind<_Alloc, T>::type`. This change was made because allocators don't need a rebind template. `__allocator_traits_rebind` is used instead of `allocator_traits::rebind` because use of `allocator_traits::rebind` requires a workaround for when template aliases are unavailable. 2. `a.deallocate(this, 1)` -> `a.deallocate(pointer_traits<self>::pointer_to(*this), 1)`. This change change is made because fancy pointers aren't always constructible from raw pointers. 3. `p.get()` -> `addressof(*p.get())`. Fancy pointers aren't actually a pointer. When we need a "real" pointer we take the address of dereferencing the fancy pointer. This should give us the actual raw pointer. Test Plan: Tests were added using `support/min_allocator.h` to each affected shared_ptr overload and creation function. These tests can only be executed in C++11 or greater since min_allocator is only available then. A extra test was added for the non-variadic versions of allocate_shared. Reviewers: danalbert, mclow.lists Reviewed By: mclow.lists Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D4859 llvm-svn: 220469
2014-10-23 12:12:28 +08:00
__cntrl_ = _VSTD::addressof(*__hold2.release());
2010-05-12 03:42:16 +08:00
#ifndef _LIBCPP_NO_EXCEPTIONS
}
catch (...)
{
__d(__p);
throw;
}
#endif // _LIBCPP_NO_EXCEPTIONS
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
template<class _Yp>
inline
shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
2010-05-12 03:42:16 +08:00
: __ptr_(__p),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_)
__cntrl_->__add_shared();
}
template<class _Tp>
inline
shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_)
__cntrl_->__add_shared();
}
template<class _Tp>
template<class _Yp>
inline
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
_NOEXCEPT
2010-05-12 03:42:16 +08:00
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_)
__cntrl_->__add_shared();
}
template<class _Tp>
inline
shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
__r.__ptr_ = nullptr;
__r.__cntrl_ = nullptr;
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
template<class _Yp>
inline
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type)
_NOEXCEPT
2010-05-12 03:42:16 +08:00
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
__r.__ptr_ = nullptr;
__r.__cntrl_ = nullptr;
2010-05-12 03:42:16 +08:00
}
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2010-05-12 03:42:16 +08:00
template<class _Tp>
template<class _Yp>
shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
2010-05-12 03:42:16 +08:00
: __ptr_(__r.get())
{
typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
__cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
__enable_weak_this(__r.get(), __r.get());
2010-05-12 03:42:16 +08:00
__r.release();
}
#endif
2010-05-12 03:42:16 +08:00
template<class _Tp>
template <class _Yp, class _Dp>
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
typename enable_if
<
!is_lvalue_reference<_Dp>::value &&
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
__nat
>::type)
2010-05-12 03:42:16 +08:00
: __ptr_(__r.get())
{
#if _LIBCPP_STD_VER > 11
if (__ptr_ == nullptr)
__cntrl_ = nullptr;
else
#endif
{
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk;
__cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
__enable_weak_this(__r.get(), __r.get());
}
2010-05-12 03:42:16 +08:00
__r.release();
}
template<class _Tp>
template <class _Yp, class _Dp>
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
typename enable_if
<
is_lvalue_reference<_Dp>::value &&
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
__nat
>::type)
2010-05-12 03:42:16 +08:00
: __ptr_(__r.get())
{
#if _LIBCPP_STD_VER > 11
if (__ptr_ == nullptr)
__cntrl_ = nullptr;
else
#endif
{
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
reference_wrapper<typename remove_reference<_Dp>::type>,
_AllocT > _CntrlBlk;
__cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT());
__enable_weak_this(__r.get(), __r.get());
}
2010-05-12 03:42:16 +08:00
__r.release();
}
template<class _Tp>
shared_ptr<_Tp>::~shared_ptr()
{
if (__cntrl_)
__cntrl_->__release_shared();
}
template<class _Tp>
inline
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>&
shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
shared_ptr(__r).swap(*this);
return *this;
}
template<class _Tp>
template<class _Yp>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
shared_ptr<_Tp>&
>::type
shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
shared_ptr(__r).swap(*this);
return *this;
}
template<class _Tp>
inline
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>&
shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
shared_ptr(_VSTD::move(__r)).swap(*this);
2010-05-12 03:42:16 +08:00
return *this;
}
template<class _Tp>
template<class _Yp>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
shared_ptr<_Tp>&
>::type
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
{
shared_ptr(_VSTD::move(__r)).swap(*this);
2010-05-12 03:42:16 +08:00
return *this;
}
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2010-05-12 03:42:16 +08:00
template<class _Tp>
template<class _Yp>
inline
typename enable_if
<
!is_array<_Yp>::value &&
is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
shared_ptr<_Tp>
>::type&
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
{
shared_ptr(_VSTD::move(__r)).swap(*this);
2010-05-12 03:42:16 +08:00
return *this;
}
#endif
2010-05-12 03:42:16 +08:00
template<class _Tp>
template <class _Yp, class _Dp>
inline
typename enable_if
<
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
typename shared_ptr<_Tp>::element_type*>::value,
shared_ptr<_Tp>&
>::type
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
{
shared_ptr(_VSTD::move(__r)).swap(*this);
2010-05-12 03:42:16 +08:00
return *this;
}
template<class _Tp>
inline
2010-05-12 03:42:16 +08:00
void
shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
_VSTD::swap(__ptr_, __r.__ptr_);
_VSTD::swap(__cntrl_, __r.__cntrl_);
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
inline
2010-05-12 03:42:16 +08:00
void
shared_ptr<_Tp>::reset() _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
shared_ptr().swap(*this);
}
template<class _Tp>
template<class _Yp>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
void
>::type
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::reset(_Yp* __p)
{
shared_ptr(__p).swap(*this);
}
template<class _Tp>
template<class _Yp, class _Dp>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
void
>::type
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
{
shared_ptr(__p, __d).swap(*this);
}
template<class _Tp>
template<class _Yp, class _Dp, class _Alloc>
inline
typename enable_if
<
__compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value,
void
>::type
2010-05-12 03:42:16 +08:00
shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
{
shared_ptr(__p, __d, __a).swap(*this);
}
//
// std::allocate_shared and std::make_shared
//
template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args)
2010-05-12 03:42:16 +08:00
{
using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
__allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...);
auto __control_block = __guard.__release_ptr();
return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block));
2010-05-12 03:42:16 +08:00
}
template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> >
_LIBCPP_HIDE_FROM_ABI
shared_ptr<_Tp> make_shared(_Args&& ...__args)
{
return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
}
2010-05-12 03:42:16 +08:00
template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
return __x.get() == __y.get();
}
template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
return !(__x == __y);
}
template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
#if _LIBCPP_STD_VER <= 11
typedef typename common_type<_Tp*, _Up*>::type _Vp;
return less<_Vp>()(__x.get(), __y.get());
#else
return less<>()(__x.get(), __y.get());
#endif
}
template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
{
return __y < __x;
}
template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
{
return !(__y < __x);
}
template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
{
return !(__x < __y);
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
{
return !__x;
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
{
return !__x;
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
{
return static_cast<bool>(__x);
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
{
return static_cast<bool>(__x);
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
{
return less<_Tp*>()(__x.get(), nullptr);
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
{
return less<_Tp*>()(nullptr, __x.get());
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
{
return nullptr < __x;
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
{
return __x < nullptr;
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
{
return !(nullptr < __x);
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
{
return !(__x < nullptr);
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
{
return !(__x < nullptr);
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
{
return !(nullptr < __x);
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
__x.swap(__y);
}
template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
shared_ptr<_Tp>
static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
return shared_ptr<_Tp>(__r,
static_cast<
typename shared_ptr<_Tp>::element_type*>(__r.get()));
2010-05-12 03:42:16 +08:00
}
template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
shared_ptr<_Tp>
dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
typedef typename shared_ptr<_Tp>::element_type _ET;
_ET* __p = dynamic_cast<_ET*>(__r.get());
2010-05-12 03:42:16 +08:00
return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
}
template<class _Tp, class _Up>
shared_ptr<_Tp>
const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
typedef typename shared_ptr<_Tp>::element_type _RTp;
return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
2010-05-12 03:42:16 +08:00
}
template<class _Tp, class _Up>
shared_ptr<_Tp>
reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
{
return shared_ptr<_Tp>(__r,
reinterpret_cast<
typename shared_ptr<_Tp>::element_type*>(__r.get()));
}
#ifndef _LIBCPP_NO_RTTI
2010-05-12 03:42:16 +08:00
template<class _Dp, class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_Dp*
get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
return __p.template __get_deleter<_Dp>();
}
#endif // _LIBCPP_NO_RTTI
2010-05-12 03:42:16 +08:00
template<class _Tp>
class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr
2010-05-12 03:42:16 +08:00
{
public:
typedef _Tp element_type;
2010-05-12 03:42:16 +08:00
private:
element_type* __ptr_;
__shared_weak_count* __cntrl_;
public:
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
_NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
weak_ptr(weak_ptr const& __r) _NOEXCEPT;
template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
_NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
weak_ptr(weak_ptr&& __r) _NOEXCEPT;
template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
_NOEXCEPT;
~weak_ptr();
_LIBCPP_INLINE_VISIBILITY
weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
template<class _Yp>
typename enable_if
<
is_convertible<_Yp*, element_type*>::value,
weak_ptr&
>::type
_LIBCPP_INLINE_VISIBILITY
operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
template<class _Yp>
typename enable_if
<
is_convertible<_Yp*, element_type*>::value,
weak_ptr&
>::type
_LIBCPP_INLINE_VISIBILITY
operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
template<class _Yp>
typename enable_if
<
is_convertible<_Yp*, element_type*>::value,
weak_ptr&
>::type
_LIBCPP_INLINE_VISIBILITY
operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
void swap(weak_ptr& __r) _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
void reset() _NOEXCEPT;
_LIBCPP_INLINE_VISIBILITY
long use_count() const _NOEXCEPT
{return __cntrl_ ? __cntrl_->use_count() : 0;}
_LIBCPP_INLINE_VISIBILITY
bool expired() const _NOEXCEPT
{return __cntrl_ == nullptr || __cntrl_->use_count() == 0;}
shared_ptr<_Tp> lock() const _NOEXCEPT;
template<class _Up>
_LIBCPP_INLINE_VISIBILITY
bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __cntrl_ < __r.__cntrl_;}
template<class _Up>
_LIBCPP_INLINE_VISIBILITY
bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __cntrl_ < __r.__cntrl_;}
template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
2010-05-12 03:42:16 +08:00
};
#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
template<class _Tp>
weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
#endif
2010-05-12 03:42:16 +08:00
template<class _Tp>
inline
_LIBCPP_CONSTEXPR
weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
: __ptr_(nullptr),
__cntrl_(nullptr)
2010-05-12 03:42:16 +08:00
{
}
template<class _Tp>
inline
weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_)
__cntrl_->__add_weak();
}
template<class _Tp>
template<class _Yp>
inline
2010-05-12 03:42:16 +08:00
weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
_NOEXCEPT
2010-05-12 03:42:16 +08:00
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_)
__cntrl_->__add_weak();
}
template<class _Tp>
template<class _Yp>
inline
2010-05-12 03:42:16 +08:00
weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
_NOEXCEPT
2010-05-12 03:42:16 +08:00
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_)
__cntrl_->__add_weak();
}
template<class _Tp>
inline
weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
__r.__ptr_ = nullptr;
__r.__cntrl_ = nullptr;
}
template<class _Tp>
template<class _Yp>
inline
weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
_NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
__r.__ptr_ = nullptr;
__r.__cntrl_ = nullptr;
}
2010-05-12 03:42:16 +08:00
template<class _Tp>
weak_ptr<_Tp>::~weak_ptr()
{
if (__cntrl_)
__cntrl_->__release_weak();
}
template<class _Tp>
inline
2010-05-12 03:42:16 +08:00
weak_ptr<_Tp>&
weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
weak_ptr(__r).swap(*this);
return *this;
}
template<class _Tp>
template<class _Yp>
inline
typename enable_if
<
is_convertible<_Yp*, _Tp*>::value,
weak_ptr<_Tp>&
>::type
weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
weak_ptr(__r).swap(*this);
return *this;
}
template<class _Tp>
inline
2010-05-12 03:42:16 +08:00
weak_ptr<_Tp>&
weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
{
weak_ptr(_VSTD::move(__r)).swap(*this);
return *this;
}
template<class _Tp>
template<class _Yp>
inline
typename enable_if
<
is_convertible<_Yp*, _Tp*>::value,
weak_ptr<_Tp>&
>::type
weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
{
weak_ptr(_VSTD::move(__r)).swap(*this);
return *this;
}
template<class _Tp>
template<class _Yp>
inline
typename enable_if
<
is_convertible<_Yp*, _Tp*>::value,
weak_ptr<_Tp>&
>::type
weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
weak_ptr(__r).swap(*this);
return *this;
}
template<class _Tp>
inline
2010-05-12 03:42:16 +08:00
void
weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
_VSTD::swap(__ptr_, __r.__ptr_);
_VSTD::swap(__cntrl_, __r.__cntrl_);
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
void
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
__x.swap(__y);
}
template<class _Tp>
inline
2010-05-12 03:42:16 +08:00
void
weak_ptr<_Tp>::reset() _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
weak_ptr().swap(*this);
}
template<class _Tp>
template<class _Yp>
shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
2010-05-12 03:42:16 +08:00
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
{
if (__cntrl_ == nullptr)
__throw_bad_weak_ptr();
2010-05-12 03:42:16 +08:00
}
template<class _Tp>
shared_ptr<_Tp>
weak_ptr<_Tp>::lock() const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{
shared_ptr<_Tp> __r;
__r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
if (__r.__cntrl_)
__r.__ptr_ = __ptr_;
return __r;
}
#if _LIBCPP_STD_VER > 14
template <class _Tp = void> struct owner_less;
#else
template <class _Tp> struct owner_less;
#endif
2010-05-12 03:42:16 +08:00
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
2010-05-12 03:42:16 +08:00
: binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
{
typedef bool result_type;
_LIBCPP_INLINE_VISIBILITY
bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __x.owner_before(__y);}
_LIBCPP_INLINE_VISIBILITY
bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __x.owner_before(__y);}
_LIBCPP_INLINE_VISIBILITY
bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __x.owner_before(__y);}
};
2010-05-12 03:42:16 +08:00
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
2010-05-12 03:42:16 +08:00
: binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
{
typedef bool result_type;
_LIBCPP_INLINE_VISIBILITY
bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __x.owner_before(__y);}
_LIBCPP_INLINE_VISIBILITY
bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __x.owner_before(__y);}
_LIBCPP_INLINE_VISIBILITY
bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
2010-05-12 03:42:16 +08:00
{return __x.owner_before(__y);}
};
#if _LIBCPP_STD_VER > 14
template <>
struct _LIBCPP_TEMPLATE_VIS owner_less<void>
{
template <class _Tp, class _Up>
_LIBCPP_INLINE_VISIBILITY
bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
{return __x.owner_before(__y);}
template <class _Tp, class _Up>
_LIBCPP_INLINE_VISIBILITY
bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
{return __x.owner_before(__y);}
template <class _Tp, class _Up>
_LIBCPP_INLINE_VISIBILITY
bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
{return __x.owner_before(__y);}
template <class _Tp, class _Up>
_LIBCPP_INLINE_VISIBILITY
bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
{return __x.owner_before(__y);}
typedef void is_transparent;
};
#endif
2010-05-12 03:42:16 +08:00
template<class _Tp>
class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
2010-05-12 03:42:16 +08:00
{
mutable weak_ptr<_Tp> __weak_this_;
protected:
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
enable_shared_from_this() _NOEXCEPT {}
_LIBCPP_INLINE_VISIBILITY
enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
_LIBCPP_INLINE_VISIBILITY
enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
{return *this;}
_LIBCPP_INLINE_VISIBILITY
2010-05-12 03:42:16 +08:00
~enable_shared_from_this() {}
public:
_LIBCPP_INLINE_VISIBILITY
shared_ptr<_Tp> shared_from_this()
{return shared_ptr<_Tp>(__weak_this_);}
_LIBCPP_INLINE_VISIBILITY
shared_ptr<_Tp const> shared_from_this() const
{return shared_ptr<const _Tp>(__weak_this_);}
2010-05-12 03:42:16 +08:00
#if _LIBCPP_STD_VER > 14
_LIBCPP_INLINE_VISIBILITY
weak_ptr<_Tp> weak_from_this() _NOEXCEPT
{ return __weak_this_; }
_LIBCPP_INLINE_VISIBILITY
weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
{ return __weak_this_; }
#endif // _LIBCPP_STD_VER > 14
2010-05-12 03:42:16 +08:00
template <class _Up> friend class shared_ptr;
};
2010-06-04 00:42:57 +08:00
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
2010-06-04 00:42:57 +08:00
{
typedef shared_ptr<_Tp> argument_type;
typedef size_t result_type;
_LIBCPP_INLINE_VISIBILITY
result_type operator()(const argument_type& __ptr) const _NOEXCEPT
2010-06-04 00:42:57 +08:00
{
return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get());
2010-06-04 00:42:57 +08:00
}
};
template<class _CharT, class _Traits, class _Yp>
inline _LIBCPP_INLINE_VISIBILITY
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
class _LIBCPP_TYPE_VIS __sp_mut
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
{
void* __lx;
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
public:
void lock() _NOEXCEPT;
void unlock() _NOEXCEPT;
private:
_LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
__sp_mut(const __sp_mut&);
__sp_mut& operator=(const __sp_mut&);
friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
};
_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
__sp_mut& __get_sp_mut(const void*);
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
atomic_is_lock_free(const shared_ptr<_Tp>*)
{
return false;
}
template <class _Tp>
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
shared_ptr<_Tp>
atomic_load(const shared_ptr<_Tp>* __p)
{
__sp_mut& __m = __get_sp_mut(__p);
__m.lock();
shared_ptr<_Tp> __q = *__p;
__m.unlock();
return __q;
}
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
shared_ptr<_Tp>
atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
{
return atomic_load(__p);
}
template <class _Tp>
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
void
atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
{
__sp_mut& __m = __get_sp_mut(__p);
__m.lock();
__p->swap(__r);
__m.unlock();
}
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
void
atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
{
atomic_store(__p, __r);
}
template <class _Tp>
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
shared_ptr<_Tp>
atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
{
__sp_mut& __m = __get_sp_mut(__p);
__m.lock();
__p->swap(__r);
__m.unlock();
return __r;
}
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
shared_ptr<_Tp>
atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
{
return atomic_exchange(__p, __r);
}
template <class _Tp>
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
bool
atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
{
shared_ptr<_Tp> __temp;
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
__sp_mut& __m = __get_sp_mut(__p);
__m.lock();
if (__p->__owner_equivalent(*__v))
{
_VSTD::swap(__temp, *__p);
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
*__p = __w;
__m.unlock();
return true;
}
_VSTD::swap(__temp, *__v);
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
*__v = *__p;
__m.unlock();
return false;
}
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
bool
atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
{
return atomic_compare_exchange_strong(__p, __v, __w);
}
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
bool
atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
shared_ptr<_Tp> __w, memory_order, memory_order)
{
return atomic_compare_exchange_strong(__p, __v, __w);
}
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
bool
atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
shared_ptr<_Tp> __w, memory_order, memory_order)
{
return atomic_compare_exchange_weak(__p, __v, __w);
}
#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Implement [util.smartptr.shared.atomic]. This is the last unimplemented section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-30 09:40:57 +08:00
//enum class
#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
# ifndef _LIBCPP_CXX03_LANG
enum class pointer_safety : unsigned char {
relaxed,
preferred,
strict
};
# endif
#else
struct _LIBCPP_TYPE_VIS pointer_safety
2010-05-12 03:42:16 +08:00
{
enum __lx
2010-05-12 03:42:16 +08:00
{
relaxed,
preferred,
strict
};
__lx __v_;
2010-05-12 03:42:16 +08:00
_LIBCPP_INLINE_VISIBILITY
pointer_safety() : __v_() {}
_LIBCPP_INLINE_VISIBILITY
pointer_safety(__lx __v) : __v_(__v) {}
_LIBCPP_INLINE_VISIBILITY
2010-05-12 03:42:16 +08:00
operator int() const {return __v_;}
};
#endif
#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
defined(_LIBCPP_BUILDING_LIBRARY)
_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
#else
// This function is only offered in C++03 under ABI v1.
# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
inline _LIBCPP_INLINE_VISIBILITY
pointer_safety get_pointer_safety() _NOEXCEPT {
return pointer_safety::relaxed;
}
# endif
#endif
2010-05-12 03:42:16 +08:00
_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
2010-05-12 03:42:16 +08:00
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_Tp*
2010-05-12 03:42:16 +08:00
undeclare_reachable(_Tp* __p)
{
return static_cast<_Tp*>(__undeclare_reachable(__p));
}
_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
2010-05-12 03:42:16 +08:00
// --- Helper for container swap --
template <typename _Alloc>
_LIBCPP_INLINE_VISIBILITY
void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
#if _LIBCPP_STD_VER >= 14
_NOEXCEPT
#else
_NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
#endif
{
using _VSTD::swap;
swap(__a1, __a2);
}
template <typename _Alloc>
inline _LIBCPP_INLINE_VISIBILITY
void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
template <typename _Alloc>
inline _LIBCPP_INLINE_VISIBILITY
void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
#if _LIBCPP_STD_VER >= 14
_NOEXCEPT
#else
_NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
#endif
{
_VSTD::__swap_allocator(__a1, __a2,
integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
}
template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
struct __noexcept_move_assign_container : public integral_constant<bool,
_Traits::propagate_on_container_move_assignment::value
#if _LIBCPP_STD_VER > 14
|| _Traits::is_always_equal::value
#else
&& is_nothrow_move_assignable<_Alloc>::value
#endif
> {};
template <class _Tp, class _Alloc>
struct __temp_value {
typedef allocator_traits<_Alloc> _Traits;
typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
_Alloc &__a;
_Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
_Tp & get() { return *__addr(); }
template<class... _Args>
_LIBCPP_NO_CFI
__temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
_Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
_VSTD::forward<_Args>(__args)...);
}
~__temp_value() { _Traits::destroy(__a, __addr()); }
};
template<typename _Alloc, typename = void, typename = void>
struct __is_allocator : false_type {};
template<typename _Alloc>
struct __is_allocator<_Alloc,
typename __void_t<typename _Alloc::value_type>::type,
typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
>
: true_type {};
// __builtin_new_allocator -- A non-templated helper for allocating and
// deallocating memory using __builtin_operator_new and
// __builtin_operator_delete. It should be used in preference to
// `std::allocator<T>` to avoid additional instantiations.
struct __builtin_new_allocator {
struct __builtin_new_deleter {
typedef void* pointer_type;
_LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
: __size_(__size), __align_(__align) {}
void operator()(void* p) const _NOEXCEPT {
_VSTD::__libcpp_deallocate(p, __size_, __align_);
}
private:
size_t __size_;
size_t __align_;
};
typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
static __holder_t __allocate_bytes(size_t __s, size_t __align) {
return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
__builtin_new_deleter(__s, __align));
}
static void __deallocate_bytes(void* __p, size_t __s,
size_t __align) _NOEXCEPT {
_VSTD::__libcpp_deallocate(__p, __s, __align);
}
template <class _Tp>
_LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
static __holder_t __allocate_type(size_t __n) {
return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
}
template <class _Tp>
_LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
__deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
}
};
2010-05-12 03:42:16 +08:00
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
# include <__pstl_memory>
#endif
2010-05-12 03:42:16 +08:00
#endif // _LIBCPP_MEMORY