mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-31 05:07:21 +02:00
Further simplification of ms_allocator
This commit is contained in:
@ -25,17 +25,17 @@ namespace boost {
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
D1 d1;
|
||||
A1 a1(allocator, size);
|
||||
A1 a1(allocator, size, &p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
||||
boost::detail::as_init(allocator, p2, n1);
|
||||
#else
|
||||
boost::detail::ms_init(p2, n1);
|
||||
#endif
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -53,17 +53,17 @@ namespace boost {
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
D1 d1;
|
||||
A1 a1(allocator);
|
||||
A1 a1(allocator, &p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
||||
boost::detail::as_init(allocator, p2, N);
|
||||
#else
|
||||
boost::detail::ms_init(p2, N);
|
||||
#endif
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -85,17 +85,17 @@ namespace boost {
|
||||
T2* p2 = 0;
|
||||
T3* p3 = reinterpret_cast<T3*>(&value);
|
||||
D1 d1;
|
||||
A1 a1(allocator, size);
|
||||
A1 a1(allocator, size, &p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
||||
boost::detail::as_init<T2, A, M>(allocator, p2, n1, p3);
|
||||
#else
|
||||
boost::detail::ms_init<T2, M>(p2, n1, p3);
|
||||
#endif
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -117,17 +117,17 @@ namespace boost {
|
||||
T2* p2 = 0;
|
||||
T3* p3 = reinterpret_cast<T3*>(&value);
|
||||
D1 d1;
|
||||
A1 a1(allocator);
|
||||
A1 a1(allocator, &p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
||||
boost::detail::as_init<T2, A, M>(allocator, p2, N, p3);
|
||||
#else
|
||||
boost::detail::ms_init<T2, M>(p2, N, p3);
|
||||
#endif
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -143,13 +143,13 @@ namespace boost {
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
D1 d1;
|
||||
A1 a1(allocator, size);
|
||||
A1 a1(allocator, size, &p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
boost::detail::ms_noinit(p2, n1);
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -167,13 +167,13 @@ namespace boost {
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
D1 d1;
|
||||
A1 a1(allocator);
|
||||
A1 a1(allocator, &p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
boost::detail::ms_noinit(p2, N);
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
}
|
||||
|
@ -45,6 +45,16 @@ namespace boost {
|
||||
struct ms_init_tag { };
|
||||
struct ms_noinit_tag { };
|
||||
|
||||
template<class T>
|
||||
union ms_allocator_state {
|
||||
ms_allocator_state(T** result_)
|
||||
: result(result_) {
|
||||
}
|
||||
|
||||
T** result;
|
||||
T* object;
|
||||
};
|
||||
|
||||
template<class T, class R, class A, class Y = char>
|
||||
class as_allocator
|
||||
: as_size_base<T, A> {
|
||||
@ -93,34 +103,21 @@ namespace boost {
|
||||
typedef as_allocator<T, R, A, U> other;
|
||||
};
|
||||
|
||||
as_allocator(const A& allocator)
|
||||
as_allocator(const A& allocator, type** result)
|
||||
: as_size_base<T, A>(allocator),
|
||||
object(0) {
|
||||
data(result) {
|
||||
}
|
||||
|
||||
as_allocator(const A& allocator, std::size_t size_)
|
||||
as_allocator(const A& allocator, std::size_t size_,
|
||||
type** result)
|
||||
: as_size_base<T, A>(allocator, size_),
|
||||
object(0) {
|
||||
}
|
||||
|
||||
as_allocator(const as_allocator& other, void* memory,
|
||||
std::size_t offset)
|
||||
: as_size_base<T, A>(other) {
|
||||
enum {
|
||||
M = boost::alignment_of<type>::value
|
||||
};
|
||||
std::size_t n1 = offset + M - 1;
|
||||
char* p1 = static_cast<char*>(memory) + n1;
|
||||
while (std::size_t(p1) % M != 0) {
|
||||
p1--;
|
||||
}
|
||||
object = reinterpret_cast<type*>(p1);
|
||||
data(result) {
|
||||
}
|
||||
|
||||
template<class U>
|
||||
as_allocator(const as_allocator<T, R, A, U>& other)
|
||||
: as_size_base<T, A>(other),
|
||||
object(other.object) {
|
||||
data(other.data) {
|
||||
}
|
||||
|
||||
pointer address(reference value) const {
|
||||
@ -150,6 +147,11 @@ namespace boost {
|
||||
#else
|
||||
void* p1 = ca.allocate(n1 + n2, value);
|
||||
#endif
|
||||
char* p2 = static_cast<char*>(p1) + n1;
|
||||
while (std::size_t(p2) % M != 0) {
|
||||
p2--;
|
||||
}
|
||||
*data.result = reinterpret_cast<type*>(p2);
|
||||
return static_cast<value_type*>(p1);
|
||||
}
|
||||
|
||||
@ -207,12 +209,12 @@ namespace boost {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
void swap(type*& other) {
|
||||
std::swap(object, other);
|
||||
void set(type* memory) {
|
||||
data.object = memory;
|
||||
}
|
||||
|
||||
void operator()() {
|
||||
if (object) {
|
||||
if (data.object) {
|
||||
R tag;
|
||||
free(tag);
|
||||
}
|
||||
@ -222,17 +224,17 @@ namespace boost {
|
||||
void free(ms_init_tag) {
|
||||
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
|
||||
const A& a1(*this);
|
||||
as_destroy(a1, object, size);
|
||||
as_destroy(a1, data.object, size);
|
||||
#else
|
||||
ms_destroy(object, size);
|
||||
ms_destroy(data.object, size);
|
||||
#endif
|
||||
}
|
||||
|
||||
void free(ms_noinit_tag) {
|
||||
ms_destroy(object, size);
|
||||
ms_destroy(data.object, size);
|
||||
}
|
||||
|
||||
type* object;
|
||||
ms_allocator_state<type> data;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
@ -264,6 +266,7 @@ namespace boost {
|
||||
|
||||
public:
|
||||
typedef typename array_base<T>::type type;
|
||||
|
||||
typedef Y value_type;
|
||||
typedef Y* pointer;
|
||||
typedef const Y* const_pointer;
|
||||
@ -277,33 +280,19 @@ namespace boost {
|
||||
typedef ms_allocator<T, R, U> other;
|
||||
};
|
||||
|
||||
ms_allocator()
|
||||
: object(0) {
|
||||
ms_allocator(type** result)
|
||||
: data(result) {
|
||||
}
|
||||
|
||||
ms_allocator(std::size_t size_)
|
||||
ms_allocator(std::size_t size_, type** result)
|
||||
: ms_size_base<T>(size_),
|
||||
object(0) {
|
||||
}
|
||||
|
||||
ms_allocator(const ms_allocator& other, void* memory,
|
||||
std::size_t offset)
|
||||
: ms_size_base<T>(other) {
|
||||
enum {
|
||||
M = boost::alignment_of<type>::value
|
||||
};
|
||||
std::size_t n1 = offset + M - 1;
|
||||
char* p1 = static_cast<char*>(memory) + n1;
|
||||
while (std::size_t(p1) % M != 0) {
|
||||
p1--;
|
||||
}
|
||||
object = reinterpret_cast<type*>(p1);
|
||||
data(result) {
|
||||
}
|
||||
|
||||
template<class U>
|
||||
ms_allocator(const ms_allocator<T, R, U>& other)
|
||||
: ms_size_base<T>(other),
|
||||
object(other.object) {
|
||||
data(other.data) {
|
||||
}
|
||||
|
||||
pointer address(reference value) const {
|
||||
@ -328,6 +317,11 @@ namespace boost {
|
||||
std::size_t n1 = count * sizeof(value_type) + M - 1;
|
||||
std::size_t n2 = size * sizeof(type);
|
||||
void* p1 = ::operator new(n1 + n2);
|
||||
char* p2 = static_cast<char*>(p1) + n1;
|
||||
while (std::size_t(p2) % M != 0) {
|
||||
p2--;
|
||||
}
|
||||
*data.result = reinterpret_cast<type*>(p2);
|
||||
return static_cast<value_type*>(p1);
|
||||
}
|
||||
|
||||
@ -367,18 +361,18 @@ namespace boost {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
void swap(type*& other) {
|
||||
std::swap(object, other);
|
||||
void set(type* memory) {
|
||||
data.object = memory;
|
||||
}
|
||||
|
||||
void operator()() {
|
||||
if (object) {
|
||||
ms_destroy(object, size);
|
||||
if (data.object) {
|
||||
ms_destroy(data.object, size);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
type* object;
|
||||
ms_allocator_state<type> data;
|
||||
};
|
||||
|
||||
class ms_in_allocator_tag {
|
||||
|
@ -21,7 +21,7 @@ namespace boost {
|
||||
typedef sp_counted_impl_pda<P, D, A> Y;
|
||||
public:
|
||||
sp_counted_impl_pda(P, const D&, const A& allocator_)
|
||||
: allocator(allocator_, this, sizeof(Y)) {
|
||||
: allocator(allocator_) {
|
||||
}
|
||||
|
||||
virtual void dispose() {
|
||||
|
@ -25,13 +25,13 @@ namespace boost {
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
D1 d1;
|
||||
A1 a1(size);
|
||||
A1 a1(size, &p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
boost::detail::ms_init(p2, n1);
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -49,13 +49,13 @@ namespace boost {
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
D1 d1;
|
||||
A1 a1;
|
||||
A1 a1(&p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
boost::detail::ms_init(p2, N);
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -77,13 +77,13 @@ namespace boost {
|
||||
T2* p2 = 0;
|
||||
T3* p3 = reinterpret_cast<T3*>(&value);
|
||||
D1 d1;
|
||||
A1 a1(size);
|
||||
A1 a1(size, &p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
boost::detail::ms_init<T2, M>(p2, n1, p3);
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -104,13 +104,13 @@ namespace boost {
|
||||
T2* p2 = 0;
|
||||
T3* p3 = reinterpret_cast<T3*>(&value);
|
||||
D1 d1;
|
||||
A1 a1;
|
||||
A1 a1(&p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
boost::detail::ms_init<T2, M>(p2, N, p3);
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -126,13 +126,13 @@ namespace boost {
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
D1 d1;
|
||||
A1 a1(size);
|
||||
A1 a1(size, &p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
boost::detail::ms_noinit(p2, n1);
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
|
||||
@ -150,13 +150,13 @@ namespace boost {
|
||||
T1* p1 = 0;
|
||||
T2* p2 = 0;
|
||||
D1 d1;
|
||||
A1 a1;
|
||||
A1 a1(&p2);
|
||||
shared_ptr<T> s1(p1, d1, a1);
|
||||
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
|
||||
a2->swap(p2);
|
||||
a2->set(0);
|
||||
boost::detail::ms_noinit(p2, N);
|
||||
a2->set(p2);
|
||||
p1 = reinterpret_cast<T1*>(p2);
|
||||
a2->swap(p2);
|
||||
return shared_ptr<T>(s1, p1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user