Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxxabi / test / test_fallback_malloc.pass.cpp
blobff6ef60710c568ffe9df82e0471a0e67000d5b3e
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include <cstdio>
10 #include <deque>
11 #include <cassert>
12 #include <inttypes.h>
14 #include <__threading_support>
16 // UNSUPPORTED: c++03
17 // UNSUPPORTED: modules-build && no-threads
19 // Necessary because we include a private source file of libc++abi, which
20 // only understands _LIBCXXABI_HAS_NO_THREADS.
21 #include "test_macros.h"
22 #ifdef TEST_HAS_NO_THREADS
23 # define _LIBCXXABI_HAS_NO_THREADS
24 #endif
26 typedef std::deque<void *> container;
28 TEST_DIAGNOSTIC_PUSH
29 TEST_CLANG_DIAGNOSTIC_IGNORED("-Wprivate-header")
30 // #define DEBUG_FALLBACK_MALLOC
31 #define INSTRUMENT_FALLBACK_MALLOC
32 #include "../src/fallback_malloc.cpp"
33 TEST_DIAGNOSTIC_POP
35 void assertAlignment(void* ptr) { assert(reinterpret_cast<size_t>(ptr) % alignof(FallbackMaxAlignType) == 0); }
37 container alloc_series ( size_t sz ) {
38 container ptrs;
39 void *p;
41 while (NULL != (p = fallback_malloc(sz))) {
42 assertAlignment(p);
43 ptrs.push_back(p);
45 return ptrs;
48 container alloc_series ( size_t sz, float growth ) {
49 container ptrs;
50 void *p;
52 while ( NULL != ( p = fallback_malloc ( sz ))) {
53 assertAlignment(p);
54 ptrs.push_back(p);
55 sz *= growth;
58 return ptrs;
61 container alloc_series ( const size_t *first, size_t len ) {
62 container ptrs;
63 const size_t *last = first + len;
64 void * p;
66 for ( const size_t *iter = first; iter != last; ++iter ) {
67 if ( NULL == (p = fallback_malloc ( *iter )))
68 break;
69 assertAlignment(p);
70 ptrs.push_back ( p );
73 return ptrs;
76 void *pop ( container &c, bool from_end ) {
77 void *ptr;
78 if ( from_end ) {
79 ptr = c.back ();
80 c.pop_back ();
82 else {
83 ptr = c.front ();
84 c.pop_front ();
86 return ptr;
89 void exhaustion_test1 () {
90 container ptrs;
92 init_heap ();
93 std::printf("Constant exhaustion tests\n");
95 // Delete in allocation order
96 ptrs = alloc_series ( 32 );
97 std::printf("Allocated %zu 32 byte chunks\n", ptrs.size());
98 print_free_list ();
99 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
100 fallback_free ( *iter );
101 print_free_list ();
102 std::printf("----\n");
104 // Delete in reverse order
105 ptrs = alloc_series ( 32 );
106 std::printf("Allocated %zu 32 byte chunks\n", ptrs.size());
107 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
108 fallback_free ( *iter );
109 print_free_list ();
110 std::printf("----\n");
112 // Alternate deletions
113 ptrs = alloc_series ( 32 );
114 std::printf("Allocated %zu 32 byte chunks\n", ptrs.size());
115 while ( ptrs.size () > 0 )
116 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
117 print_free_list ();
120 void exhaustion_test2 () {
121 container ptrs;
122 init_heap ();
124 std::printf("Growing exhaustion tests\n");
126 // Delete in allocation order
127 ptrs = alloc_series ( 32, 1.5 );
129 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
130 ptrs.size());
131 print_free_list ();
132 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
133 fallback_free ( *iter );
134 print_free_list ();
135 std::printf("----\n");
137 // Delete in reverse order
138 print_free_list ();
139 ptrs = alloc_series ( 32, 1.5 );
140 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
141 ptrs.size());
142 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
143 fallback_free ( *iter );
144 print_free_list ();
145 std::printf("----\n");
147 // Alternate deletions
148 ptrs = alloc_series ( 32, 1.5 );
149 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
150 ptrs.size());
151 while ( ptrs.size () > 0 )
152 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
153 print_free_list ();
157 void exhaustion_test3 () {
158 const size_t allocs [] = { 124, 60, 252, 60, 4 };
159 container ptrs;
160 init_heap ();
162 std::printf("Complete exhaustion tests\n");
164 // Delete in allocation order
165 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
166 std::printf("Allocated %zu chunks\n", ptrs.size());
167 print_free_list ();
168 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
169 fallback_free ( *iter );
170 print_free_list ();
171 std::printf("----\n");
173 // Delete in reverse order
174 print_free_list ();
175 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
176 std::printf("Allocated %zu chunks\n", ptrs.size());
177 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
178 fallback_free ( *iter );
179 print_free_list ();
180 std::printf("----\n");
182 // Alternate deletions
183 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
184 std::printf("Allocated %zu chunks\n", ptrs.size());
185 while ( ptrs.size () > 0 )
186 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
187 print_free_list ();
192 int main () {
193 print_free_list ();
195 char *p = (char *) fallback_malloc ( 1024 ); // too big!
196 std::printf("fallback_malloc ( 1024 ) --> %" PRIuPTR"\n", (uintptr_t) p);
197 print_free_list ();
199 p = (char *) fallback_malloc ( 32 );
200 std::printf("fallback_malloc ( 32 ) --> %" PRIuPTR"\n", (uintptr_t) (p - heap));
201 if ( !is_fallback_ptr ( p ))
202 std::printf("### p is not a fallback pointer!!\n");
204 print_free_list ();
205 fallback_free ( p );
206 print_free_list ();
208 exhaustion_test1();
209 exhaustion_test2();
210 exhaustion_test3();
211 return 0;