diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 48d6f879f75..463cbf6304c 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2007-06-01 Benjamin Kosnik + + * include/ext/throw_allocator.h (__throw_allocator::allocate): + Throw bad_alloc for out of memory conditions. + * testsuite/ext/throw_allocator/deallocate_global.cc: New. + * testsuite/ext/throw_allocator/check_delete.cc: Same. + * testsuite/ext/throw_allocator/check_allocate_max_size.cc: Same. + * testsuite/ext/throw_allocator/check_deallocate_null.cc: Same. + * testsuite/ext/throw_allocator/explicit_instantiation.cc: Same. + * testsuite/ext/throw_allocator/check_new.cc: Same. + * testsuite/ext/throw_allocator/deallocate_local.cc: Same. + 2007-05-31 Paolo Carlini PR libstdc++/31426 diff --git a/libstdc++-v3/include/ext/throw_allocator.h b/libstdc++-v3/include/ext/throw_allocator.h index bb2707d376f..240a3ccbe4f 100644 --- a/libstdc++-v3/include/ext/throw_allocator.h +++ b/libstdc++-v3/include/ext/throw_allocator.h @@ -210,32 +210,35 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) { return std::allocator().max_size(); } pointer - allocate(size_type num, std::allocator::const_pointer hint = 0) + allocate(size_type __n, std::allocator::const_pointer hint = 0) { + if (__builtin_expect(__n > this->max_size(), false)) + std::__throw_bad_alloc(); + throw_conditionally(); - value_type* const a = std::allocator().allocate(num, hint); - insert(a, sizeof(value_type) * num); + value_type* const a = std::allocator().allocate(__n, hint); + insert(a, sizeof(value_type) * __n); return a; } void - construct(pointer p, const T& val) - { return std::allocator().construct(p, val); } + construct(pointer __p, const T& val) + { return std::allocator().construct(__p, val); } void - destroy(pointer p) - { std::allocator().destroy(p); } + destroy(pointer __p) + { std::allocator().destroy(__p); } void - deallocate(pointer p, size_type num) + deallocate(pointer __p, size_type __n) { - erase(p, sizeof(value_type) * num); - std::allocator().deallocate(p, num); + erase(__p, sizeof(value_type) * __n); + std::allocator().deallocate(__p, __n); } void - check_allocated(pointer p, size_type num) - { throw_allocator_base::check_allocated(p, sizeof(value_type) * num); } + check_allocated(pointer __p, size_type __n) + { throw_allocator_base::check_allocated(__p, sizeof(value_type) * __n); } void check_allocated(size_type label) diff --git a/libstdc++-v3/testsuite/ext/throw_allocator/check_allocate_max_size.cc b/libstdc++-v3/testsuite/ext/throw_allocator/check_allocate_max_size.cc new file mode 100644 index 00000000000..902361eff0d --- /dev/null +++ b/libstdc++-v3/testsuite/ext/throw_allocator/check_allocate_max_size.cc @@ -0,0 +1,29 @@ +// +// Copyright (C) 2007 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include + +int main() +{ + typedef int value_type; + typedef __gnu_cxx::throw_allocator allocator_type; + __gnu_test::check_allocate_max_size(); + return 0; +} diff --git a/libstdc++-v3/testsuite/ext/throw_allocator/check_deallocate_null.cc b/libstdc++-v3/testsuite/ext/throw_allocator/check_deallocate_null.cc new file mode 100644 index 00000000000..8e6e1a025aa --- /dev/null +++ b/libstdc++-v3/testsuite/ext/throw_allocator/check_deallocate_null.cc @@ -0,0 +1,36 @@ +// +// Copyright (C) 2007 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include + +int main() +{ + typedef int value_type; + typedef __gnu_cxx::throw_allocator allocator_type; + + try { __gnu_test::check_deallocate_null(); } + catch (std::logic_error&) + { + // Should throw logic_error to catch null erase. + } + + return 0; +} + diff --git a/libstdc++-v3/testsuite/ext/throw_allocator/check_delete.cc b/libstdc++-v3/testsuite/ext/throw_allocator/check_delete.cc new file mode 100644 index 00000000000..a8e716798fe --- /dev/null +++ b/libstdc++-v3/testsuite/ext/throw_allocator/check_delete.cc @@ -0,0 +1,54 @@ +// +// Copyright (C) 2007 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include +#include + +using __gnu_cxx::throw_allocator; + +void* +operator new(std::size_t n) throw(std::bad_alloc) +{ + new_called = true; + return std::malloc(n); +} + +void +operator delete(void *v) throw() +{ + delete_called = true; + return std::free(v); +} + +// These just help tracking down error messages. +void test01() +{ + bool test __attribute__((unused)) = true; + typedef throw_allocator allocator_type; + VERIFY( bool(__gnu_test::check_delete()) ); +} + +int main() +{ + test01(); + return 0; +} + diff --git a/libstdc++-v3/testsuite/ext/throw_allocator/check_new.cc b/libstdc++-v3/testsuite/ext/throw_allocator/check_new.cc new file mode 100644 index 00000000000..1b00412918f --- /dev/null +++ b/libstdc++-v3/testsuite/ext/throw_allocator/check_new.cc @@ -0,0 +1,54 @@ +// +// Copyright (C) 2007 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include +#include + +using __gnu_cxx::throw_allocator; + +void* +operator new(std::size_t n) throw(std::bad_alloc) +{ + new_called = true; + return std::malloc(n); +} + +void +operator delete(void *v) throw() +{ + delete_called = true; + return std::free(v); +} + +// These just help tracking down error messages. +void test01() +{ + bool test __attribute__((unused)) = true; + typedef throw_allocator allocator_type; + VERIFY( bool(__gnu_test::check_new()) ); +} + +int main() +{ + test01(); + return 0; +} + diff --git a/libstdc++-v3/testsuite/ext/throw_allocator/deallocate_global.cc b/libstdc++-v3/testsuite/ext/throw_allocator/deallocate_global.cc new file mode 100644 index 00000000000..c8bbe510c9b --- /dev/null +++ b/libstdc++-v3/testsuite/ext/throw_allocator/deallocate_global.cc @@ -0,0 +1,73 @@ +// +// Copyright (C) 2007 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include +#include + +static size_t count; + +struct count_check +{ + count_check() {} + ~count_check() + { + if (count != 0) + throw std::runtime_error("count isn't zero"); + } +}; + +static count_check check; + +void* operator new(size_t size) throw(std::bad_alloc) +{ + printf("operator new is called \n"); + void* p = malloc(size); + if (p == NULL) + throw std::bad_alloc(); + count++; + return p; +} + +void operator delete(void* p) throw() +{ + printf("operator delete is called \n"); + if (p == NULL) + return; + count--; + if (count == 0) + printf("All memory released \n"); + else + printf("%lu allocations to be released \n", + static_cast(count)); + free(p); +} + +typedef char char_t; +typedef std::char_traits traits_t; +typedef __gnu_cxx::throw_allocator allocator_t; +typedef std::basic_string string_t; + +string_t s("bayou bend"); + +int main() +{ + return 0; +} diff --git a/libstdc++-v3/testsuite/ext/throw_allocator/deallocate_local.cc b/libstdc++-v3/testsuite/ext/throw_allocator/deallocate_local.cc new file mode 100644 index 00000000000..794232e914e --- /dev/null +++ b/libstdc++-v3/testsuite/ext/throw_allocator/deallocate_local.cc @@ -0,0 +1,64 @@ +// +// Copyright (C) 2007 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include +#include + +static size_t alloc_cnt; + +void* operator new(size_t size) throw(std::bad_alloc) +{ + printf("operator new is called \n"); + void* p = malloc(size); + if (p == NULL) + throw std::bad_alloc(); + alloc_cnt++; + return p; +} + +void operator delete(void* p) throw() +{ + printf("operator delete is called \n"); + if (p == NULL) + return; + alloc_cnt--; + if (alloc_cnt == 0) + printf("All memory released \n"); + else + printf("%lu allocations to be released \n", + static_cast(alloc_cnt)); + free(p); +} + +typedef char char_t; +typedef std::char_traits traits_t; +typedef __gnu_cxx::throw_allocator allocator_t; +typedef std::basic_string string_t; + +int main() +{ + bool test __attribute__((unused)) = true; + { + string_t s; + s += "bayou bend"; + } + VERIFY( alloc_cnt == 0 ); + return 0; +} diff --git a/libstdc++-v3/testsuite/ext/throw_allocator/explicit_instantiation.cc b/libstdc++-v3/testsuite/ext/throw_allocator/explicit_instantiation.cc new file mode 100644 index 00000000000..2ef7d3a9303 --- /dev/null +++ b/libstdc++-v3/testsuite/ext/throw_allocator/explicit_instantiation.cc @@ -0,0 +1,25 @@ +// { dg-do compile } + +// +// Copyright (C) 2007 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include +#include + +template class __gnu_cxx::throw_allocator;