]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/ext/new_allocator/deallocate_local.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / ext / new_allocator / deallocate_local.cc
1 //
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 // Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // 20.4.1.1 allocator members
21
22 #include <string>
23 #include <stdexcept>
24 #include <cstdlib>
25 #include <cstdio>
26 #include <ext/new_allocator.h>
27
28 static size_t count;
29
30 struct count_check
31 {
32 count_check() { }
33 ~count_check()
34 {
35 if (count != 0)
36 throw std::runtime_error("allocation/deallocation count isn't zero");
37 }
38 };
39
40 static count_check check;
41
42 void* operator new(size_t size) throw(std::bad_alloc)
43 {
44 std::printf("operator new is called \n");
45 void* p = std::malloc(size);
46 if (p == NULL)
47 throw std::bad_alloc();
48 count++;
49 return p;
50 }
51
52 void operator delete(void* p) throw()
53 {
54 std::printf("operator delete is called \n");
55 if (p == NULL)
56 return;
57 count--;
58 }
59
60 typedef char char_t;
61 typedef std::char_traits<char_t> traits_t;
62 typedef __gnu_cxx::new_allocator<char_t> allocator_t;
63 typedef std::basic_string<char_t, traits_t, allocator_t> string_t;
64
65 int main()
66 {
67 string_t s;
68 s += "bayou bend";
69 return 0;
70 }