blob: 8b986599a68008936f89fbae2e612022cd6df9ce [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 );
Louis Dionnecc69d212020-10-13 15:47:31 -040076 std::printf("Allocated %lu 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 );
Louis Dionnecc69d212020-10-13 15:47:31 -040085 std::printf("Allocated %lu 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 );
Louis Dionnecc69d212020-10-13 15:47:31 -040093 std::printf("Allocated %lu 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
108 std::printf("Allocated %lu { 32, 48, 72, 108, 162 ... } byte chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000109 print_free_list ();
110 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
111 fallback_free ( *iter );
112 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400113 std::printf("----\n");
114
Marshall Clowe2dcb752011-07-20 15:04:39 +0000115// Delete in reverse order
116 print_free_list ();
117 ptrs = alloc_series ( 32, 1.5 );
Louis Dionnecc69d212020-10-13 15:47:31 -0400118 std::printf("Allocated %lu { 32, 48, 72, 108, 162 ... } byte chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000119 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
120 fallback_free ( *iter );
121 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400122 std::printf("----\n");
Marshall Clowe2dcb752011-07-20 15:04:39 +0000123
124// Alternate deletions
125 ptrs = alloc_series ( 32, 1.5 );
Louis Dionnecc69d212020-10-13 15:47:31 -0400126 std::printf("Allocated %lu { 32, 48, 72, 108, 162 ... } byte chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000127 while ( ptrs.size () > 0 )
128 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
Louis Dionnecc69d212020-10-13 15:47:31 -0400129 print_free_list ();
130
131}
Marshall Clowe2dcb752011-07-20 15:04:39 +0000132
133void exhaustion_test3 () {
134 const size_t allocs [] = { 124, 60, 252, 60, 4 };
135 container ptrs;
136 init_heap ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400137
138 std::printf("Complete exhaustion tests\n");
Marshall Clowe2dcb752011-07-20 15:04:39 +0000139
140// Delete in allocation order
141 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
Louis Dionnecc69d212020-10-13 15:47:31 -0400142 std::printf("Allocated %lu chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000143 print_free_list ();
144 for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter )
145 fallback_free ( *iter );
146 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400147 std::printf("----\n");
148
Marshall Clowe2dcb752011-07-20 15:04:39 +0000149// Delete in reverse order
150 print_free_list ();
151 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
Louis Dionnecc69d212020-10-13 15:47:31 -0400152 std::printf("Allocated %lu chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000153 for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter )
154 fallback_free ( *iter );
155 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400156 std::printf("----\n");
Marshall Clowe2dcb752011-07-20 15:04:39 +0000157
158// Alternate deletions
159 ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] ));
Louis Dionnecc69d212020-10-13 15:47:31 -0400160 std::printf("Allocated %lu chunks\n", ptrs.size());
Marshall Clowe2dcb752011-07-20 15:04:39 +0000161 while ( ptrs.size () > 0 )
162 fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 ));
Louis Dionnecc69d212020-10-13 15:47:31 -0400163 print_free_list ();
Marshall Clowe2dcb752011-07-20 15:04:39 +0000164
Louis Dionnecc69d212020-10-13 15:47:31 -0400165}
166
167
Eric Fiseliera140cba2016-12-24 00:37:13 +0000168int main () {
Marshall Clowe2dcb752011-07-20 15:04:39 +0000169 print_free_list ();
170
171 char *p = (char *) fallback_malloc ( 1024 ); // too big!
Louis Dionnecc69d212020-10-13 15:47:31 -0400172 std::printf("fallback_malloc ( 1024 ) --> %lu\n", (unsigned long ) p);
Marshall Clowe2dcb752011-07-20 15:04:39 +0000173 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400174
Marshall Clowe2dcb752011-07-20 15:04:39 +0000175 p = (char *) fallback_malloc ( 32 );
Louis Dionnecc69d212020-10-13 15:47:31 -0400176 std::printf("fallback_malloc ( 32 ) --> %lu\n", (unsigned long) (p - heap));
Marshall Clowe2dcb752011-07-20 15:04:39 +0000177 if ( !is_fallback_ptr ( p ))
Louis Dionnecc69d212020-10-13 15:47:31 -0400178 std::printf("### p is not a fallback pointer!!\n");
179
Marshall Clowe2dcb752011-07-20 15:04:39 +0000180 print_free_list ();
181 fallback_free ( p );
182 print_free_list ();
Louis Dionnecc69d212020-10-13 15:47:31 -0400183
184 exhaustion_test1();
185 exhaustion_test2();
186 exhaustion_test3();
Marshall Clowe2dcb752011-07-20 15:04:39 +0000187 return 0;
Louis Dionnecc69d212020-10-13 15:47:31 -0400188}