annotate libcxxabi/test/test_fallback_malloc.pass.cpp @ 240:ca573705d418

merge
author matac
date Fri, 28 Jul 2023 20:50:09 +0900
parents c4bab56944e8
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1 //===----------------------------------------------------------------------===//
150
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
9 #include <cstdio>
150
anatofuz
parents:
diff changeset
10 #include <deque>
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
11 #include <cassert>
150
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include <__threading_support>
anatofuz
parents:
diff changeset
14
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
15 // UNSUPPORTED: c++03
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
16 // UNSUPPORTED: modules-build && no-threads
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
17
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
18 // Necessary because we include a private source file of libc++abi, which
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
19 // only understands _LIBCXXABI_HAS_NO_THREADS.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
20 #include "test_macros.h"
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
21 #ifdef TEST_HAS_NO_THREADS
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
22 # define _LIBCXXABI_HAS_NO_THREADS
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
23 #endif
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
24
150
anatofuz
parents:
diff changeset
25 typedef std::deque<void *> container;
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 // #define DEBUG_FALLBACK_MALLOC
anatofuz
parents:
diff changeset
28 #define INSTRUMENT_FALLBACK_MALLOC
anatofuz
parents:
diff changeset
29 #include "../src/fallback_malloc.cpp"
anatofuz
parents:
diff changeset
30
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
31 void assertAlignment(void* ptr) { assert(reinterpret_cast<size_t>(ptr) % alignof(FallbackMaxAlignType) == 0); }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
32
150
anatofuz
parents:
diff changeset
33 container alloc_series ( size_t sz ) {
anatofuz
parents:
diff changeset
34 container ptrs;
anatofuz
parents:
diff changeset
35 void *p;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
36
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
37 while (NULL != (p = fallback_malloc(sz))) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
38 assertAlignment(p);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
39 ptrs.push_back(p);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
40 }
150
anatofuz
parents:
diff changeset
41 return ptrs;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
42 }
150
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 container alloc_series ( size_t sz, float growth ) {
anatofuz
parents:
diff changeset
45 container ptrs;
anatofuz
parents:
diff changeset
46 void *p;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
47
150
anatofuz
parents:
diff changeset
48 while ( NULL != ( p = fallback_malloc ( sz ))) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
49 assertAlignment(p);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
50 ptrs.push_back(p);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
51 sz *= growth;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
52 }
150
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 return ptrs;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
55 }
150
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 container alloc_series ( const size_t *first, size_t len ) {
anatofuz
parents:
diff changeset
58 container ptrs;
anatofuz
parents:
diff changeset
59 const size_t *last = first + len;
anatofuz
parents:
diff changeset
60 void * p;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
61
150
anatofuz
parents:
diff changeset
62 for ( const size_t *iter = first; iter != last; ++iter ) {
anatofuz
parents:
diff changeset
63 if ( NULL == (p = fallback_malloc ( *iter )))
anatofuz
parents:
diff changeset
64 break;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
65 assertAlignment(p);
150
anatofuz
parents:
diff changeset
66 ptrs.push_back ( p );
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
67 }
150
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 return ptrs;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
70 }
150
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 void *pop ( container &c, bool from_end ) {
anatofuz
parents:
diff changeset
73 void *ptr;
anatofuz
parents:
diff changeset
74 if ( from_end ) {
anatofuz
parents:
diff changeset
75 ptr = c.back ();
anatofuz
parents:
diff changeset
76 c.pop_back ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
77 }
150
anatofuz
parents:
diff changeset
78 else {
anatofuz
parents:
diff changeset
79 ptr = c.front ();
anatofuz
parents:
diff changeset
80 c.pop_front ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
81 }
150
anatofuz
parents:
diff changeset
82 return ptr;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
83 }
150
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 void exhaustion_test1 () {
anatofuz
parents:
diff changeset
86 container ptrs;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
87
150
anatofuz
parents:
diff changeset
88 init_heap ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
89 std::printf("Constant exhaustion tests\n");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
90
150
anatofuz
parents:
diff changeset
91 // Delete in allocation order
anatofuz
parents:
diff changeset
92 ptrs = alloc_series ( 32 );
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
93 std::printf("Allocated %zu 32 byte chunks\n", ptrs.size());
150
anatofuz
parents:
diff changeset
94 print_free_list ();
anatofuz
parents:
diff changeset
95 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
anatofuz
parents:
diff changeset
96 fallback_free ( *iter );
anatofuz
parents:
diff changeset
97 print_free_list ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
98 std::printf("----\n");
150
anatofuz
parents:
diff changeset
99
anatofuz
parents:
diff changeset
100 // Delete in reverse order
anatofuz
parents:
diff changeset
101 ptrs = alloc_series ( 32 );
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
102 std::printf("Allocated %zu 32 byte chunks\n", ptrs.size());
150
anatofuz
parents:
diff changeset
103 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
anatofuz
parents:
diff changeset
104 fallback_free ( *iter );
anatofuz
parents:
diff changeset
105 print_free_list ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
106 std::printf("----\n");
150
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 // Alternate deletions
anatofuz
parents:
diff changeset
109 ptrs = alloc_series ( 32 );
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
110 std::printf("Allocated %zu 32 byte chunks\n", ptrs.size());
150
anatofuz
parents:
diff changeset
111 while ( ptrs.size () > 0 )
anatofuz
parents:
diff changeset
112 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
anatofuz
parents:
diff changeset
113 print_free_list ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
114 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
115
150
anatofuz
parents:
diff changeset
116 void exhaustion_test2 () {
anatofuz
parents:
diff changeset
117 container ptrs;
anatofuz
parents:
diff changeset
118 init_heap ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
119
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
120 std::printf("Growing exhaustion tests\n");
150
anatofuz
parents:
diff changeset
121
anatofuz
parents:
diff changeset
122 // Delete in allocation order
anatofuz
parents:
diff changeset
123 ptrs = alloc_series ( 32, 1.5 );
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
124
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
125 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
126 ptrs.size());
150
anatofuz
parents:
diff changeset
127 print_free_list ();
anatofuz
parents:
diff changeset
128 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
anatofuz
parents:
diff changeset
129 fallback_free ( *iter );
anatofuz
parents:
diff changeset
130 print_free_list ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
131 std::printf("----\n");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
132
150
anatofuz
parents:
diff changeset
133 // Delete in reverse order
anatofuz
parents:
diff changeset
134 print_free_list ();
anatofuz
parents:
diff changeset
135 ptrs = alloc_series ( 32, 1.5 );
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
136 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
137 ptrs.size());
150
anatofuz
parents:
diff changeset
138 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
anatofuz
parents:
diff changeset
139 fallback_free ( *iter );
anatofuz
parents:
diff changeset
140 print_free_list ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
141 std::printf("----\n");
150
anatofuz
parents:
diff changeset
142
anatofuz
parents:
diff changeset
143 // Alternate deletions
anatofuz
parents:
diff changeset
144 ptrs = alloc_series ( 32, 1.5 );
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
145 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
146 ptrs.size());
150
anatofuz
parents:
diff changeset
147 while ( ptrs.size () > 0 )
anatofuz
parents:
diff changeset
148 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
149 print_free_list ();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
150
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
151 }
150
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 void exhaustion_test3 () {
anatofuz
parents:
diff changeset
154 const size_t allocs [] = { 124, 60, 252, 60, 4 };
anatofuz
parents:
diff changeset
155 container ptrs;
anatofuz
parents:
diff changeset
156 init_heap ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
157
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
158 std::printf("Complete exhaustion tests\n");
150
anatofuz
parents:
diff changeset
159
anatofuz
parents:
diff changeset
160 // Delete in allocation order
anatofuz
parents:
diff changeset
161 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
162 std::printf("Allocated %zu chunks\n", ptrs.size());
150
anatofuz
parents:
diff changeset
163 print_free_list ();
anatofuz
parents:
diff changeset
164 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
anatofuz
parents:
diff changeset
165 fallback_free ( *iter );
anatofuz
parents:
diff changeset
166 print_free_list ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
167 std::printf("----\n");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
168
150
anatofuz
parents:
diff changeset
169 // Delete in reverse order
anatofuz
parents:
diff changeset
170 print_free_list ();
anatofuz
parents:
diff changeset
171 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
172 std::printf("Allocated %zu chunks\n", ptrs.size());
150
anatofuz
parents:
diff changeset
173 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
anatofuz
parents:
diff changeset
174 fallback_free ( *iter );
anatofuz
parents:
diff changeset
175 print_free_list ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
176 std::printf("----\n");
150
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 // Alternate deletions
anatofuz
parents:
diff changeset
179 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
180 std::printf("Allocated %zu chunks\n", ptrs.size());
150
anatofuz
parents:
diff changeset
181 while ( ptrs.size () > 0 )
anatofuz
parents:
diff changeset
182 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
183 print_free_list ();
150
anatofuz
parents:
diff changeset
184
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
185 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
186
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
187
150
anatofuz
parents:
diff changeset
188 int main () {
anatofuz
parents:
diff changeset
189 print_free_list ();
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 char *p = (char *) fallback_malloc ( 1024 ); // too big!
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
192 std::printf("fallback_malloc ( 1024 ) --> %lu\n", (unsigned long ) p);
150
anatofuz
parents:
diff changeset
193 print_free_list ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
194
150
anatofuz
parents:
diff changeset
195 p = (char *) fallback_malloc ( 32 );
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
196 std::printf("fallback_malloc ( 32 ) --> %lu\n", (unsigned long) (p - heap));
150
anatofuz
parents:
diff changeset
197 if ( !is_fallback_ptr ( p ))
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
198 std::printf("### p is not a fallback pointer!!\n");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
199
150
anatofuz
parents:
diff changeset
200 print_free_list ();
anatofuz
parents:
diff changeset
201 fallback_free ( p );
anatofuz
parents:
diff changeset
202 print_free_list ();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
203
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
204 exhaustion_test1();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
205 exhaustion_test2();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
206 exhaustion_test3();
150
anatofuz
parents:
diff changeset
207 return 0;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
208 }