diff options
author | bkoz <bkoz@138bc75d-0d04-0410-961f-82ee72b054a4> | 2007-06-01 10:09:40 +0000 |
---|---|---|
committer | bkoz <bkoz@138bc75d-0d04-0410-961f-82ee72b054a4> | 2007-06-01 10:09:40 +0000 |
commit | f9ca8b77ffddc3323e44e521a3ac31d3dc515c52 (patch) | |
tree | ee33ea2741f0a470133a6d3aaf97e5dd0337bd6f /libstdc++-v3 | |
parent | ca2e246e7b95cbd28b244310d4b84201054d7491 (diff) | |
download | gcc-f9ca8b77ffddc3323e44e521a3ac31d3dc515c52.tar.gz |
2007-06-01 Benjamin Kosnik <bkoz@redhat.com>
* 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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@125261 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libstdc++-v3')
9 files changed, 362 insertions, 12 deletions
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 <bkoz@redhat.com> + + * 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 <pcarlini@suse.de> 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<value_type>().max_size(); } pointer - allocate(size_type num, std::allocator<void>::const_pointer hint = 0) + allocate(size_type __n, std::allocator<void>::const_pointer hint = 0) { + if (__builtin_expect(__n > this->max_size(), false)) + std::__throw_bad_alloc(); + throw_conditionally(); - value_type* const a = std::allocator<value_type>().allocate(num, hint); - insert(a, sizeof(value_type) * num); + value_type* const a = std::allocator<value_type>().allocate(__n, hint); + insert(a, sizeof(value_type) * __n); return a; } void - construct(pointer p, const T& val) - { return std::allocator<value_type>().construct(p, val); } + construct(pointer __p, const T& val) + { return std::allocator<value_type>().construct(__p, val); } void - destroy(pointer p) - { std::allocator<value_type>().destroy(p); } + destroy(pointer __p) + { std::allocator<value_type>().destroy(__p); } void - deallocate(pointer p, size_type num) + deallocate(pointer __p, size_type __n) { - erase(p, sizeof(value_type) * num); - std::allocator<value_type>().deallocate(p, num); + erase(__p, sizeof(value_type) * __n); + std::allocator<value_type>().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 <ext/throw_allocator.h> +#include <testsuite_allocator.h> + +int main() +{ + typedef int value_type; + typedef __gnu_cxx::throw_allocator<value_type> allocator_type; + __gnu_test::check_allocate_max_size<allocator_type>(); + 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 <ext/throw_allocator.h> +#include <testsuite_allocator.h> + +int main() +{ + typedef int value_type; + typedef __gnu_cxx::throw_allocator<value_type> allocator_type; + + try { __gnu_test::check_deallocate_null<allocator_type>(); } + 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 <cstdlib> +#include <ext/throw_allocator.h> +#include <testsuite_hooks.h> +#include <testsuite_allocator.h> + +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<unsigned int> allocator_type; + VERIFY( bool(__gnu_test::check_delete<allocator_type, true>()) ); +} + +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 <cstdlib> +#include <ext/throw_allocator.h> +#include <testsuite_hooks.h> +#include <testsuite_allocator.h> + +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<unsigned int> allocator_type; + VERIFY( bool(__gnu_test::check_new<allocator_type, true>()) ); +} + +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 <string> +#include <stdexcept> +#include <ext/throw_allocator.h> +#include <testsuite_hooks.h> + +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<unsigned long>(count)); + free(p); +} + +typedef char char_t; +typedef std::char_traits<char_t> traits_t; +typedef __gnu_cxx::throw_allocator<char_t> allocator_t; +typedef std::basic_string<char_t, traits_t, allocator_t> 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 <string> +#include <ext/throw_allocator.h> +#include <testsuite_hooks.h> + +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<unsigned long>(alloc_cnt)); + free(p); +} + +typedef char char_t; +typedef std::char_traits<char_t> traits_t; +typedef __gnu_cxx::throw_allocator<char_t> allocator_t; +typedef std::basic_string<char_t, traits_t, allocator_t> 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 <cstdlib> +#include <ext/throw_allocator.h> + +template class __gnu_cxx::throw_allocator<int>; |