Commit b8637750 by Benjamin Kosnik Committed by Benjamin Kosnik

throw_allocator.h (__throw_allocator::allocate): Throw bad_alloc for out of memory conditions.

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.

From-SVN: r125261
parent 5fda945d
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
......
......@@ -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)
......
//
// 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;
}
//
// 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;
}
//
// 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;
}
//
// 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;
}
//
// 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;
}
//
// 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;
}
// { 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>;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment