Merged revision(s) 81684-81685 from trunk:

For fixed size arrays upon constructor exception thrown destroy correctly.
........
Minor cosmetic change in detail array_deleter
........


[SVN r81695]
This commit is contained in:
Glen Fernandes
2012-12-03 15:42:15 +00:00
parent ea55019260
commit 88c2baa20b
3 changed files with 135 additions and 53 deletions

View File

@@ -24,53 +24,78 @@ namespace boost {
object(0) { object(0) {
} }
~array_deleter() { ~array_deleter() {
destroy(); destroy(size);
} }
void construct(T* memory) { void construct(T* memory) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < size; i++) { try {
void* p1 = object + i; for (object = memory; i < size; i++) {
void* p1 = memory + i;
::new(p1) T(); ::new(p1) T();
} }
} catch (...) {
destroy(i);
throw;
}
} }
#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS) #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
template<typename... Args> template<typename... Args>
void construct(T* memory, Args&&... args) { void construct(T* memory, Args&&... args) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < size; i++) { try {
void* p1 = object + i; for (object = memory; i < size; i++) {
void* p1 = memory + i;
::new(p1) T(args...); ::new(p1) T(args...);
} }
} catch (...) {
destroy(i);
throw;
}
} }
#endif #endif
void construct_list(T* memory, const T* list) { void construct_list(T* memory, const T* list) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < size; i++) { try {
void* p1 = object + i; for (object = memory; i < size; i++) {
void* p1 = memory + i;
::new(p1) T(list[i]); ::new(p1) T(list[i]);
} }
} catch (...) {
destroy(i);
throw;
}
} }
void construct_list(T* memory, const T* list, std::size_t n) { void construct_list(T* memory, const T* list, std::size_t n) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < size; i++) { try {
void* p1 = object + i; for (object = memory; i < size; i++) {
void* p1 = memory + i;
::new(p1) T(list[i % n]); ::new(p1) T(list[i % n]);
} }
} catch (...) {
destroy(i);
throw;
}
} }
void construct_noinit(T* memory) { void construct_noinit(T* memory) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < size; i++) { try {
void* p1 = object + i; for (object = memory; i < size; i++) {
void* p1 = memory + i;
::new(p1) T; ::new(p1) T;
} }
} catch (...) {
destroy(i);
throw;
}
} }
void operator()(const void*) { void operator()(const void*) {
destroy(); destroy(size);
} }
private: private:
void destroy() { void destroy(std::size_t n) {
if (object) { if (object) {
for (std::size_t i = size; i > 0; ) { for (std::size_t i = n; i > 0; ) {
object[--i].~T(); object[--i].~T();
} }
object = 0; object = 0;
@@ -86,53 +111,78 @@ namespace boost {
: object(0) { : object(0) {
} }
~array_deleter() { ~array_deleter() {
destroy(); destroy(N);
} }
void construct(T* memory) { void construct(T* memory) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < N; i++) { try {
void* p1 = object + i; for (object = memory; i < N; i++) {
void* p1 = memory + i;
::new(p1) T(); ::new(p1) T();
} }
} catch (...) {
destroy(i);
throw;
}
} }
#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS) #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
template<typename... Args> template<typename... Args>
void construct(T* memory, Args&&... args) { void construct(T* memory, Args&&... args) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < N; i++) { try {
void* p1 = object + i; for (object = memory; i < N; i++) {
void* p1 = memory + i;
::new(p1) T(args...); ::new(p1) T(args...);
} }
} catch (...) {
destroy(i);
throw;
}
} }
#endif #endif
void construct_list(T* memory, const T* list) { void construct_list(T* memory, const T* list) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < N; i++) { try {
void* p1 = object + i; for (object = memory; i < N; i++) {
void* p1 = memory + i;
::new(p1) T(list[i]); ::new(p1) T(list[i]);
} }
} catch (...) {
destroy(i);
throw;
}
} }
void construct_list(T* memory, const T* list, std::size_t n) { void construct_list(T* memory, const T* list, std::size_t n) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < N; i++) { try {
void* p1 = object + i; for (object = memory; i < N; i++) {
void* p1 = memory + i;
::new(p1) T(list[i % n]); ::new(p1) T(list[i % n]);
} }
} catch (...) {
destroy(i);
throw;
}
} }
void construct_noinit(T* memory) { void construct_noinit(T* memory) {
object = memory; std::size_t i = 0;
for (std::size_t i = 0; i < N; i++) { try {
void* p1 = object + i; for (object = memory; i < N; i++) {
void* p1 = memory + i;
::new(p1) T; ::new(p1) T;
} }
} catch (...) {
destroy(i);
throw;
}
} }
void operator()(const void*) { void operator()(const void*) {
destroy(); destroy(N);
} }
private: private:
void destroy() { void destroy(std::size_t n) {
if (object) { if (object) {
for (std::size_t i = N; i > 0; ) { for (std::size_t i = n; i > 0; ) {
object[--i].~T(); object[--i].~T();
} }
object = 0; object = 0;

View File

@@ -43,5 +43,21 @@ int main() {
} catch (...) { } catch (...) {
BOOST_TEST(type::instances == 0); BOOST_TEST(type::instances == 0);
} }
#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
BOOST_TEST(type::instances == 0);
try {
boost::allocate_shared<type[6]>(std::allocator<type>());
BOOST_ERROR("allocate_shared did not throw");
} catch (...) {
BOOST_TEST(type::instances == 0);
}
BOOST_TEST(type::instances == 0);
try {
boost::allocate_shared<type[3][2]>(std::allocator<type>());
BOOST_ERROR("allocate_shared did not throw");
} catch (...) {
BOOST_TEST(type::instances == 0);
}
#endif
return boost::report_errors(); return boost::report_errors();
} }

View File

@@ -43,6 +43,22 @@ int main() {
} catch (...) { } catch (...) {
BOOST_TEST(type::instances == 0); BOOST_TEST(type::instances == 0);
} }
#if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
BOOST_TEST(type::instances == 0);
try {
boost::make_shared<type[6]>();
BOOST_ERROR("make_shared did not throw");
} catch (...) {
BOOST_TEST(type::instances == 0);
}
BOOST_TEST(type::instances == 0);
try {
boost::make_shared<type[3][2]>();
BOOST_ERROR("make_shared did not throw");
} catch (...) {
BOOST_TEST(type::instances == 0);
}
#endif
BOOST_TEST(type::instances == 0); BOOST_TEST(type::instances == 0);
try { try {
boost::make_shared_noinit<type[]>(6); boost::make_shared_noinit<type[]>(6);