1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
14 #include <__threading_support>
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
26 typedef std::deque
<void *> container
;
29 TEST_CLANG_DIAGNOSTIC_IGNORED("-Wprivate-header")
30 // #define DEBUG_FALLBACK_MALLOC
31 #define INSTRUMENT_FALLBACK_MALLOC
32 #include "../src/fallback_malloc.cpp"
35 void assertAlignment(void* ptr
) { assert(reinterpret_cast<size_t>(ptr
) % alignof(FallbackMaxAlignType
) == 0); }
37 container
alloc_series ( size_t sz
) {
41 while (NULL
!= (p
= fallback_malloc(sz
))) {
48 container
alloc_series ( size_t sz
, float growth
) {
52 while ( NULL
!= ( p
= fallback_malloc ( sz
))) {
61 container
alloc_series ( const size_t *first
, size_t len
) {
63 const size_t *last
= first
+ len
;
66 for ( const size_t *iter
= first
; iter
!= last
; ++iter
) {
67 if ( NULL
== (p
= fallback_malloc ( *iter
)))
76 void *pop ( container
&c
, bool from_end
) {
89 void exhaustion_test1 () {
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());
99 for ( container::iterator iter
= ptrs
.begin (); iter
!= ptrs
.end (); ++iter
)
100 fallback_free ( *iter
);
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
);
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 ));
120 void exhaustion_test2 () {
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",
132 for ( container::iterator iter
= ptrs
.begin (); iter
!= ptrs
.end (); ++iter
)
133 fallback_free ( *iter
);
135 std::printf("----\n");
137 // Delete in reverse order
139 ptrs
= alloc_series ( 32, 1.5 );
140 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
142 for ( container::reverse_iterator iter
= ptrs
.rbegin (); iter
!= ptrs
.rend (); ++iter
)
143 fallback_free ( *iter
);
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",
151 while ( ptrs
.size () > 0 )
152 fallback_free ( pop ( ptrs
, ptrs
.size () % 1 == 1 ));
157 void exhaustion_test3 () {
158 const size_t allocs
[] = { 124, 60, 252, 60, 4 };
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());
168 for ( container::iterator iter
= ptrs
.begin (); iter
!= ptrs
.end (); ++iter
)
169 fallback_free ( *iter
);
171 std::printf("----\n");
173 // Delete in reverse order
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
);
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 ));
195 char *p
= (char *) fallback_malloc ( 1024 ); // too big!
196 std::printf("fallback_malloc ( 1024 ) --> %" PRIuPTR
"\n", (uintptr_t) p
);
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");