blob: d72ca8df5b62029a929eba34b23e4927ec12b255 [file] [log] [blame]
Howard Hinnant987afbe2011-12-06 18:01:47 +00001//===--------------------- test_fallback_malloc.cpp -----------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Howard Hinnant987afbe2011-12-06 18:01:47 +00006//
7//===----------------------------------------------------------------------===//
8
Louis Dionnecc69d212020-10-13 15:47:31 -04009#include <cstdio>
Marshall Clowe2dcb752011-07-20 15:04:39 +000010#include <deque>
11
Asiri Rathnayake97ba9fa2017-01-03 12:58:34 +000012#include <__threading_support>
Marshall Clowe2dcb752011-07-20 15:04:39 +000013
14typedef std::deque<void *> container;
15
16// #define DEBUG_FALLBACK_MALLOC
17#define INSTRUMENT_FALLBACK_MALLOC
Igor Kudrind9edde42016-10-07 08:48:28 +000018#include "../src/fallback_malloc.cpp"
Marshall Clowe2dcb752011-07-20 15:04:39 +000019
20container alloc_series ( size_t sz ) {
21 container ptrs;
22 void *p;
Louis Dionnecc69d212020-10-13 15:47:31 -040023
Marshall Clowe2dcb752011-07-20 15:04:39 +000024 while ( NULL != ( p = fallback_malloc ( sz )))
25 ptrs.push_back ( p );
26 return ptrs;
Louis Dionnecc69d212020-10-13 15:47:31 -040027}
Marshall Clowe2dcb752011-07-20 15:04:39 +000028
29container alloc_series ( size_t sz, float growth ) {
30 container ptrs;
31 void *p;
Louis Dionnecc69d212020-10-13 15:47:31 -040032
Marshall Clowe2dcb752011-07-20 15:04:39 +000033 while ( NULL != ( p = fallback_malloc ( sz ))) {
34 ptrs.push_back ( p );
35 sz *= growth;
Louis Dionnecc69d212020-10-13 15:47:31 -040036 }
Marshall Clowe2dcb752011-07-20 15:04:39 +000037
38 return ptrs;
Louis Dionnecc69d212020-10-13 15:47:31 -040039}
Marshall Clowe2dcb752011-07-20 15:04:39 +000040
41container alloc_series ( const size_t *first, size_t len ) {
42 container ptrs;
43 const size_t *last = first + len;
44 void * p;
Louis Dionnecc69d212020-10-13 15:47:31 -040045
Marshall Clowe2dcb752011-07-20 15:04:39 +000046 for ( const size_t *iter = first; iter != last; ++iter ) {
47 if ( NULL == (p = fallback_malloc ( *iter )))
48 break;
49 ptrs.push_back ( p );
Louis Dionnecc69d212020-10-13 15:47:31 -040050 }
Marshall Clowe2dcb752011-07-20 15:04:39 +000051
52 return ptrs;
Louis Dionnecc69d212020-10-13 15:47:31 -040053}
Marshall Clowe2dcb752011-07-20 15:04:39 +000054
55void *pop ( container &c, bool from_end ) {
56 void *ptr;
57 if ( from_end ) {
58 ptr = c.back ();
59 c.pop_back ();
Louis Dionnecc69d212020-10-13 15:47:31 -040060 }
Marshall Clowe2dcb752011-07-20 15:04:39 +000061 else {
62 ptr = c.front ();
63 c.pop_front ();
Marshall Clowe2dcb752011-07-20 15:04:39 +000064 }
Louis Dionnecc69d212020-10-13 15:47:31 -040065 return ptr;
66}
Marshall Clowe2dcb752011-07-20 15:04:39 +000067
68void exhaustion_test1 () {
69 container ptrs;
Louis Dionnecc69d212020-10-13 15:47:31 -040070
Marshall Clowe2dcb752011-07-20 15:04:39 +000071 init_heap ();
Louis Dionnecc69d212020-10-13 15:47:31 -040072 std::printf("Constant exhaustion tests\n");
73
Marshall Clowe2dcb752011-07-20 15:04:39 +000074// Delete in allocation order
75 ptrs = alloc_series ( 32 );
Simon Tathambcb7b872020-10-16 13:59:10 +010076 std::printf("Allocated %zu 32 byte chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +000077 print_free_list ();
78 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
79 fallback_free ( *iter );
80 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -040081 std::printf("----\n");
Marshall Clowe2dcb752011-07-20 15:04:39 +000082
83// Delete in reverse order
84 ptrs = alloc_series ( 32 );
Simon Tathambcb7b872020-10-16 13:59:10 +010085 std::printf("Allocated %zu 32 byte chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +000086 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
87 fallback_free ( *iter );
88 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -040089 std::printf("----\n");
Marshall Clowe2dcb752011-07-20 15:04:39 +000090
91// Alternate deletions
92 ptrs = alloc_series ( 32 );
Simon Tathambcb7b872020-10-16 13:59:10 +010093 std::printf("Allocated %zu 32 byte chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +000094 while ( ptrs.size () > 0 )
95 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
96 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -040097}
98
Marshall Clowe2dcb752011-07-20 15:04:39 +000099void exhaustion_test2 () {
100 container ptrs;
101 init_heap ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400102
103 std::printf("Growing exhaustion tests\n");
Marshall Clowe2dcb752011-07-20 15:04:39 +0000104
105// Delete in allocation order
106 ptrs = alloc_series ( 32, 1.5 );
Louis Dionnecc69d212020-10-13 15:47:31 -0400107
Simon Tathambcb7b872020-10-16 13:59:10 +0100108 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
109 ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000110 print_free_list ();
111 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
112 fallback_free ( *iter );
113 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400114 std::printf("----\n");
115
Marshall Clowe2dcb752011-07-20 15:04:39 +0000116// Delete in reverse order
117 print_free_list ();
118 ptrs = alloc_series ( 32, 1.5 );
Simon Tathambcb7b872020-10-16 13:59:10 +0100119 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
120 ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000121 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
122 fallback_free ( *iter );
123 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400124 std::printf("----\n");
Marshall Clowe2dcb752011-07-20 15:04:39 +0000125
126// Alternate deletions
127 ptrs = alloc_series ( 32, 1.5 );
Simon Tathambcb7b872020-10-16 13:59:10 +0100128 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
129 ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000130 while ( ptrs.size () > 0 )
131 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
Louis Dionnecc69d212020-10-13 15:47:31 -0400132 print_free_list ();
133
134}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000135
136void exhaustion_test3 () {
137 const size_t allocs [] = { 124, 60, 252, 60, 4 };
138 container ptrs;
139 init_heap ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400140
141 std::printf("Complete exhaustion tests\n");
Marshall Clowe2dcb752011-07-20 15:04:39 +0000142
143// Delete in allocation order
144 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
Simon Tathambcb7b872020-10-16 13:59:10 +0100145 std::printf("Allocated %zu chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000146 print_free_list ();
147 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
148 fallback_free ( *iter );
149 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400150 std::printf("----\n");
151
Marshall Clowe2dcb752011-07-20 15:04:39 +0000152// Delete in reverse order
153 print_free_list ();
154 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
Simon Tathambcb7b872020-10-16 13:59:10 +0100155 std::printf("Allocated %zu chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000156 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
157 fallback_free ( *iter );
158 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400159 std::printf("----\n");
Marshall Clowe2dcb752011-07-20 15:04:39 +0000160
161// Alternate deletions
162 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
Simon Tathambcb7b872020-10-16 13:59:10 +0100163 std::printf("Allocated %zu chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000164 while ( ptrs.size () > 0 )
165 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
Louis Dionnecc69d212020-10-13 15:47:31 -0400166 print_free_list ();
Marshall Clowe2dcb752011-07-20 15:04:39 +0000167
Louis Dionnecc69d212020-10-13 15:47:31 -0400168}
169
170
Eric Fiseliera140cba2016-12-24 00:37:13 +0000171int main () {
Marshall Clowe2dcb752011-07-20 15:04:39 +0000172 print_free_list ();
173
174 char *p = (char *) fallback_malloc ( 1024 ); // too big!
Louis Dionnecc69d212020-10-13 15:47:31 -0400175 std::printf("fallback_malloc ( 1024 ) --> %lu\n", (unsigned long ) p);
Marshall Clowe2dcb752011-07-20 15:04:39 +0000176 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400177
Marshall Clowe2dcb752011-07-20 15:04:39 +0000178 p = (char *) fallback_malloc ( 32 );
Louis Dionnecc69d212020-10-13 15:47:31 -0400179 std::printf("fallback_malloc ( 32 ) --> %lu\n", (unsigned long) (p - heap));
Marshall Clowe2dcb752011-07-20 15:04:39 +0000180 if ( !is_fallback_ptr ( p ))
Louis Dionnecc69d212020-10-13 15:47:31 -0400181 std::printf("### p is not a fallback pointer!!\n");
182
Marshall Clowe2dcb752011-07-20 15:04:39 +0000183 print_free_list ();
184 fallback_free ( p );
185 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400186
187 exhaustion_test1();
188 exhaustion_test2();
189 exhaustion_test3();
Marshall Clowe2dcb752011-07-20 15:04:39 +0000190 return 0;
Louis Dionnecc69d212020-10-13 15:47:31 -0400191}