xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/testsuite/ext/allocators.cc (revision 03a78d155d6fff5698289342b62759a75b20d130)
1 // 2001-11-25  Phil Edwards  <pme@gcc.gnu.org>
2 //
3 // Copyright (C) 2001 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 2, 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 COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20 
21 // 20.4.1.1 allocator members
22 
23 #include <memory>
24 #include <cstdlib>
25 #include <testsuite_hooks.h>
26 
27 typedef std::__malloc_alloc_template<3>             weird_alloc;
28 template class std::__malloc_alloc_template<3>;
29 
30 typedef std::__debug_alloc<weird_alloc>             debug_weird_alloc;
31 template class std::__debug_alloc<weird_alloc>;
32 
33 typedef std::__default_alloc_template<true, 3>      unshared_normal_alloc;
34 template class std::__default_alloc_template<true, 3>;
35 
36 typedef std::__default_alloc_template<false, 3>     unshared_singlethreaded;
37 template class std::__default_alloc_template<false, 3>;
38 
39 //std::malloc_alloc test_malloc_alloc;
40 
41 struct big
42 {
43     long f[15];
44 };
45 
46 
47 bool         new_called;
48 bool         delete_called;
49 std::size_t  requested;
50 
51 void*
operator new(std::size_t n)52 operator new(std::size_t n) throw(std::bad_alloc)
53 {
54   new_called = true;
55   requested = n;
56   return std::malloc(n);
57 }
58 
59 void
operator delete(void * v)60 operator delete(void *v) throw()
61 {
62   delete_called = true;
63   return std::free(v);
64 }
65 
66 
67 template <typename arbitrary_SGIstyle_allocator,
68           bool uses_global_new_and_delete>
test()69 void test()
70 {
71   new_called = false;
72   delete_called = false;
73   requested = 0;
74 
75   std::__allocator<big, arbitrary_SGIstyle_allocator>   a;
76   big *p = a.allocate(10);
77   if (uses_global_new_and_delete)  VERIFY (requested >= (10*15*sizeof(long)));
78   // Touch the far end of supposedly-allocated memory to check that we got
79   // all of it.  Why "3"?  Because it's my favorite integer between e and pi.
80   p[9].f[14] = 3;
81   VERIFY (new_called == uses_global_new_and_delete );
82   a.deallocate(p,10);
83   VERIFY (delete_called == uses_global_new_and_delete );
84 }
85 
86 
87 // These just help tracking down error messages.
test01()88 void test01() { test<weird_alloc,false>(); }
test02()89 void test02() { test<debug_weird_alloc,false>(); }
test03()90 void test03() { test<unshared_normal_alloc,true>(); }
test04()91 void test04() { test<unshared_singlethreaded,true>(); }
92 
main()93 int main()
94 {
95   test01();
96   test02();
97   test03();
98   test04();
99 
100   return 0;
101 }
102 
103