Further simplification of ms_allocator

This commit is contained in:
Glen Fernandes
2014-02-12 22:12:04 -08:00
parent d9333e5375
commit 8c9e8b5556
4 changed files with 81 additions and 87 deletions

View File

@@ -25,17 +25,17 @@ namespace boost {
T1* p1 = 0; T1* p1 = 0;
T2* p2 = 0; T2* p2 = 0;
D1 d1; D1 d1;
A1 a1(allocator, size); A1 a1(allocator, size, &p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
#if !defined(BOOST_NO_CXX11_ALLOCATOR) #if !defined(BOOST_NO_CXX11_ALLOCATOR)
boost::detail::as_init(allocator, p2, n1); boost::detail::as_init(allocator, p2, n1);
#else #else
boost::detail::ms_init(p2, n1); boost::detail::ms_init(p2, n1);
#endif #endif
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -53,17 +53,17 @@ namespace boost {
T1* p1 = 0; T1* p1 = 0;
T2* p2 = 0; T2* p2 = 0;
D1 d1; D1 d1;
A1 a1(allocator); A1 a1(allocator, &p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
#if !defined(BOOST_NO_CXX11_ALLOCATOR) #if !defined(BOOST_NO_CXX11_ALLOCATOR)
boost::detail::as_init(allocator, p2, N); boost::detail::as_init(allocator, p2, N);
#else #else
boost::detail::ms_init(p2, N); boost::detail::ms_init(p2, N);
#endif #endif
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -85,17 +85,17 @@ namespace boost {
T2* p2 = 0; T2* p2 = 0;
T3* p3 = reinterpret_cast<T3*>(&value); T3* p3 = reinterpret_cast<T3*>(&value);
D1 d1; D1 d1;
A1 a1(allocator, size); A1 a1(allocator, size, &p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
#if !defined(BOOST_NO_CXX11_ALLOCATOR) #if !defined(BOOST_NO_CXX11_ALLOCATOR)
boost::detail::as_init<T2, A, M>(allocator, p2, n1, p3); boost::detail::as_init<T2, A, M>(allocator, p2, n1, p3);
#else #else
boost::detail::ms_init<T2, M>(p2, n1, p3); boost::detail::ms_init<T2, M>(p2, n1, p3);
#endif #endif
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -117,17 +117,17 @@ namespace boost {
T2* p2 = 0; T2* p2 = 0;
T3* p3 = reinterpret_cast<T3*>(&value); T3* p3 = reinterpret_cast<T3*>(&value);
D1 d1; D1 d1;
A1 a1(allocator); A1 a1(allocator, &p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
#if !defined(BOOST_NO_CXX11_ALLOCATOR) #if !defined(BOOST_NO_CXX11_ALLOCATOR)
boost::detail::as_init<T2, A, M>(allocator, p2, N, p3); boost::detail::as_init<T2, A, M>(allocator, p2, N, p3);
#else #else
boost::detail::ms_init<T2, M>(p2, N, p3); boost::detail::ms_init<T2, M>(p2, N, p3);
#endif #endif
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -143,13 +143,13 @@ namespace boost {
T1* p1 = 0; T1* p1 = 0;
T2* p2 = 0; T2* p2 = 0;
D1 d1; D1 d1;
A1 a1(allocator, size); A1 a1(allocator, size, &p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
boost::detail::ms_noinit(p2, n1); boost::detail::ms_noinit(p2, n1);
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -167,13 +167,13 @@ namespace boost {
T1* p1 = 0; T1* p1 = 0;
T2* p2 = 0; T2* p2 = 0;
D1 d1; D1 d1;
A1 a1(allocator); A1 a1(allocator, &p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
boost::detail::ms_noinit(p2, N); boost::detail::ms_noinit(p2, N);
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
} }

View File

@@ -45,6 +45,16 @@ namespace boost {
struct ms_init_tag { }; struct ms_init_tag { };
struct ms_noinit_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> template<class T, class R, class A, class Y = char>
class as_allocator class as_allocator
: as_size_base<T, A> { : as_size_base<T, A> {
@@ -93,34 +103,21 @@ namespace boost {
typedef as_allocator<T, R, A, U> other; 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), : 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_), : as_size_base<T, A>(allocator, size_),
object(0) { data(result) {
}
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);
} }
template<class U> template<class U>
as_allocator(const as_allocator<T, R, A, U>& other) as_allocator(const as_allocator<T, R, A, U>& other)
: as_size_base<T, A>(other), : as_size_base<T, A>(other),
object(other.object) { data(other.data) {
} }
pointer address(reference value) const { pointer address(reference value) const {
@@ -150,6 +147,11 @@ namespace boost {
#else #else
void* p1 = ca.allocate(n1 + n2, value); void* p1 = ca.allocate(n1 + n2, value);
#endif #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); return static_cast<value_type*>(p1);
} }
@@ -207,12 +209,12 @@ namespace boost {
return !(*this == other); return !(*this == other);
} }
void swap(type*& other) { void set(type* memory) {
std::swap(object, other); data.object = memory;
} }
void operator()() { void operator()() {
if (object) { if (data.object) {
R tag; R tag;
free(tag); free(tag);
} }
@@ -222,17 +224,17 @@ namespace boost {
void free(ms_init_tag) { void free(ms_init_tag) {
#if !defined(BOOST_NO_CXX11_ALLOCATOR) #if !defined(BOOST_NO_CXX11_ALLOCATOR)
const A& a1(*this); const A& a1(*this);
as_destroy(a1, object, size); as_destroy(a1, data.object, size);
#else #else
ms_destroy(object, size); ms_destroy(data.object, size);
#endif #endif
} }
void free(ms_noinit_tag) { void free(ms_noinit_tag) {
ms_destroy(object, size); ms_destroy(data.object, size);
} }
type* object; ms_allocator_state<type> data;
}; };
template<class T> template<class T>
@@ -264,6 +266,7 @@ namespace boost {
public: public:
typedef typename array_base<T>::type type; typedef typename array_base<T>::type type;
typedef Y value_type; typedef Y value_type;
typedef Y* pointer; typedef Y* pointer;
typedef const Y* const_pointer; typedef const Y* const_pointer;
@@ -277,33 +280,19 @@ namespace boost {
typedef ms_allocator<T, R, U> other; typedef ms_allocator<T, R, U> other;
}; };
ms_allocator() ms_allocator(type** result)
: object(0) { : data(result) {
} }
ms_allocator(std::size_t size_) ms_allocator(std::size_t size_, type** result)
: ms_size_base<T>(size_), : ms_size_base<T>(size_),
object(0) { data(result) {
}
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);
} }
template<class U> template<class U>
ms_allocator(const ms_allocator<T, R, U>& other) ms_allocator(const ms_allocator<T, R, U>& other)
: ms_size_base<T>(other), : ms_size_base<T>(other),
object(other.object) { data(other.data) {
} }
pointer address(reference value) const { pointer address(reference value) const {
@@ -328,6 +317,11 @@ namespace boost {
std::size_t n1 = count * sizeof(value_type) + M - 1; std::size_t n1 = count * sizeof(value_type) + M - 1;
std::size_t n2 = size * sizeof(type); std::size_t n2 = size * sizeof(type);
void* p1 = ::operator new(n1 + n2); 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); return static_cast<value_type*>(p1);
} }
@@ -367,18 +361,18 @@ namespace boost {
return !(*this == other); return !(*this == other);
} }
void swap(type*& other) { void set(type* memory) {
std::swap(object, other); data.object = memory;
} }
void operator()() { void operator()() {
if (object) { if (data.object) {
ms_destroy(object, size); ms_destroy(data.object, size);
} }
} }
private: private:
type* object; ms_allocator_state<type> data;
}; };
class ms_in_allocator_tag { class ms_in_allocator_tag {

View File

@@ -21,7 +21,7 @@ namespace boost {
typedef sp_counted_impl_pda<P, D, A> Y; typedef sp_counted_impl_pda<P, D, A> Y;
public: public:
sp_counted_impl_pda(P, const D&, const A& allocator_) sp_counted_impl_pda(P, const D&, const A& allocator_)
: allocator(allocator_, this, sizeof(Y)) { : allocator(allocator_) {
} }
virtual void dispose() { virtual void dispose() {

View File

@@ -25,13 +25,13 @@ namespace boost {
T1* p1 = 0; T1* p1 = 0;
T2* p2 = 0; T2* p2 = 0;
D1 d1; D1 d1;
A1 a1(size); A1 a1(size, &p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
boost::detail::ms_init(p2, n1); boost::detail::ms_init(p2, n1);
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -49,13 +49,13 @@ namespace boost {
T1* p1 = 0; T1* p1 = 0;
T2* p2 = 0; T2* p2 = 0;
D1 d1; D1 d1;
A1 a1; A1 a1(&p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
boost::detail::ms_init(p2, N); boost::detail::ms_init(p2, N);
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -77,13 +77,13 @@ namespace boost {
T2* p2 = 0; T2* p2 = 0;
T3* p3 = reinterpret_cast<T3*>(&value); T3* p3 = reinterpret_cast<T3*>(&value);
D1 d1; D1 d1;
A1 a1(size); A1 a1(size, &p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); 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); boost::detail::ms_init<T2, M>(p2, n1, p3);
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -104,13 +104,13 @@ namespace boost {
T2* p2 = 0; T2* p2 = 0;
T3* p3 = reinterpret_cast<T3*>(&value); T3* p3 = reinterpret_cast<T3*>(&value);
D1 d1; D1 d1;
A1 a1; A1 a1(&p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); 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); boost::detail::ms_init<T2, M>(p2, N, p3);
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -126,13 +126,13 @@ namespace boost {
T1* p1 = 0; T1* p1 = 0;
T2* p2 = 0; T2* p2 = 0;
D1 d1; D1 d1;
A1 a1(size); A1 a1(size, &p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
boost::detail::ms_noinit(p2, n1); boost::detail::ms_noinit(p2, n1);
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
@@ -150,13 +150,13 @@ namespace boost {
T1* p1 = 0; T1* p1 = 0;
T2* p2 = 0; T2* p2 = 0;
D1 d1; D1 d1;
A1 a1; A1 a1(&p2);
shared_ptr<T> s1(p1, d1, a1); shared_ptr<T> s1(p1, d1, a1);
A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter()); A1* a2 = static_cast<A1*>(s1._internal_get_untyped_deleter());
a2->swap(p2); a2->set(0);
boost::detail::ms_noinit(p2, N); boost::detail::ms_noinit(p2, N);
a2->set(p2);
p1 = reinterpret_cast<T1*>(p2); p1 = reinterpret_cast<T1*>(p2);
a2->swap(p2);
return shared_ptr<T>(s1, p1); return shared_ptr<T>(s1, p1);
} }
} }