1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #include <boost/container/detail/dlmalloc.hpp>
12 #include <boost/container/allocator.hpp>
13 #include <boost/container/vector.hpp>
14 #include <boost/container/list.hpp>
15
16 using namespace boost::container;
17
basic_test()18 bool basic_test()
19 {
20 size_t received = 0;
21 if(!dlmalloc_all_deallocated())
22 return false;
23 void *ptr = dlmalloc_alloc(50, 98, &received);
24 if(dlmalloc_size(ptr) != received)
25 return false;
26 if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
27 return false;
28
29 if(dlmalloc_all_deallocated())
30 return false;
31
32 dlmalloc_grow(ptr, received + 20, received + 30, &received);
33
34 if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
35 return false;
36
37 if(dlmalloc_size(ptr) != received)
38 return false;
39
40 if(!dlmalloc_shrink(ptr, 100, 140, &received, 1))
41 return false;
42
43 if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
44 return false;
45
46 if(!dlmalloc_shrink(ptr, 0, 140, &received, 1))
47 return false;
48
49 if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
50 return false;
51
52 if(dlmalloc_shrink(ptr, 0, received/2, &received, 1))
53 return false;
54
55 if(dlmalloc_allocated_memory() != dlmalloc_chunksize(ptr))
56 return false;
57
58 if(dlmalloc_size(ptr) != received)
59 return false;
60
61 dlmalloc_free(ptr);
62
63 dlmalloc_malloc_check();
64 if(!dlmalloc_all_deallocated())
65 return false;
66 return true;
67 }
68
vector_test()69 bool vector_test()
70 {
71 typedef boost::container::vector<int, allocator<int> > Vector;
72 if(!dlmalloc_all_deallocated())
73 return false;
74 {
75 const int NumElem = 1000;
76 Vector v;
77 v.resize(NumElem);
78 int *orig_buf = &v[0];
79 int *new_buf = &v[0];
80 while(orig_buf == new_buf){
81 Vector::size_type cl = v.capacity() - v.size();
82 while(cl--){
83 v.push_back(0);
84 }
85 v.push_back(0);
86 new_buf = &v[0];
87 }
88 }
89 if(!dlmalloc_all_deallocated())
90 return false;
91 return true;
92 }
93
list_test()94 bool list_test()
95 {
96 typedef boost::container::list<int, allocator<int> > List;
97 if(!dlmalloc_all_deallocated())
98 return false;
99 {
100 const int NumElem = 1000;
101 List l;
102 int values[NumElem];
103 l.insert(l.end(), &values[0], &values[NumElem]);
104 }
105 if(!dlmalloc_all_deallocated())
106 return false;
107 return true;
108 }
109
main()110 int main()
111 {
112 if(!basic_test())
113 return 1;
114 if(!vector_test())
115 return 1;
116 if(!list_test())
117 return 1;
118 return 0;
119 }
120