1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <memory>
11 
12 // allocator:
13 // pointer allocate(size_type n, allocator<void>::const_pointer hint=0);
14 
15 #include <memory>
16 #include <new>
17 #include <cstdlib>
18 #include <cassert>
19 
20 int new_called = 0;
21 
22 void* operator new(std::size_t s) throw(std::bad_alloc)
23 {
24     ++new_called;
25     assert(s == 3 * sizeof(int));
26     return std::malloc(s);
27 }
28 
29 void  operator delete(void* p) throw()
30 {
31     --new_called;
32     std::free(p);
33 }
34 
35 int A_constructed = 0;
36 
37 struct A
38 {
39     int data;
40     A() {++A_constructed;}
41     A(const A&) {++A_constructed;}
42     ~A() {--A_constructed;}
43 };
44 
45 int main()
46 {
47     std::allocator<A> a;
48     assert(new_called == 0);
49     assert(A_constructed == 0);
50     A* ap = a.allocate(3);
51     assert(new_called == 1);
52     assert(A_constructed == 0);
53     a.deallocate(ap, 3);
54     assert(new_called == 0);
55     assert(A_constructed == 0);
56 
57     A* ap2 = a.allocate(3, (const void*)5);
58     assert(new_called == 1);
59     assert(A_constructed == 0);
60     a.deallocate(ap2, 3);
61     assert(new_called == 0);
62     assert(A_constructed == 0);
63 }
64