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 _LIBCXXABI_ASSERT(expr, msg) assert((expr) && (msg))
32 // #define DEBUG_FALLBACK_MALLOC
33 #define INSTRUMENT_FALLBACK_MALLOC
34 #include "../src/fallback_malloc.cpp"
37 void assertAlignment(void* ptr
) { assert(reinterpret_cast<size_t>(ptr
) % alignof(FallbackMaxAlignType
) == 0); }
39 container
alloc_series ( size_t sz
) {
43 while (NULL
!= (p
= fallback_malloc(sz
))) {
50 container
alloc_series ( size_t sz
, float growth
) {
54 while ( NULL
!= ( p
= fallback_malloc ( sz
))) {
63 container
alloc_series ( const size_t *first
, size_t len
) {
65 const size_t *last
= first
+ len
;
68 for ( const size_t *iter
= first
; iter
!= last
; ++iter
) {
69 if ( NULL
== (p
= fallback_malloc ( *iter
)))
78 void *pop ( container
&c
, bool from_end
) {
91 void exhaustion_test1 () {
95 std::printf("Constant exhaustion tests\n");
97 // Delete in allocation order
98 ptrs
= alloc_series ( 32 );
99 std::printf("Allocated %zu 32 byte chunks\n", ptrs
.size());
101 for ( container::iterator iter
= ptrs
.begin (); iter
!= ptrs
.end (); ++iter
)
102 fallback_free ( *iter
);
104 std::printf("----\n");
106 // Delete in reverse order
107 ptrs
= alloc_series ( 32 );
108 std::printf("Allocated %zu 32 byte chunks\n", ptrs
.size());
109 for ( container::reverse_iterator iter
= ptrs
.rbegin (); iter
!= ptrs
.rend (); ++iter
)
110 fallback_free ( *iter
);
112 std::printf("----\n");
114 // Alternate deletions
115 ptrs
= alloc_series ( 32 );
116 std::printf("Allocated %zu 32 byte chunks\n", ptrs
.size());
117 while ( ptrs
.size () > 0 )
118 fallback_free ( pop ( ptrs
, ptrs
.size () % 1 == 1 ));
122 void exhaustion_test2 () {
126 std::printf("Growing exhaustion tests\n");
128 // Delete in allocation order
129 ptrs
= alloc_series ( 32, 1.5 );
131 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
134 for ( container::iterator iter
= ptrs
.begin (); iter
!= ptrs
.end (); ++iter
)
135 fallback_free ( *iter
);
137 std::printf("----\n");
139 // Delete in reverse order
141 ptrs
= alloc_series ( 32, 1.5 );
142 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
144 for ( container::reverse_iterator iter
= ptrs
.rbegin (); iter
!= ptrs
.rend (); ++iter
)
145 fallback_free ( *iter
);
147 std::printf("----\n");
149 // Alternate deletions
150 ptrs
= alloc_series ( 32, 1.5 );
151 std::printf("Allocated %zu { 32, 48, 72, 108, 162 ... } byte chunks\n",
153 while ( ptrs
.size () > 0 )
154 fallback_free ( pop ( ptrs
, ptrs
.size () % 1 == 1 ));
159 void exhaustion_test3 () {
160 const size_t allocs
[] = { 124, 60, 252, 60, 4 };
164 std::printf("Complete exhaustion tests\n");
166 // Delete in allocation order
167 ptrs
= alloc_series ( allocs
, sizeof ( allocs
) / sizeof ( allocs
[0] ));
168 std::printf("Allocated %zu chunks\n", ptrs
.size());
170 for ( container::iterator iter
= ptrs
.begin (); iter
!= ptrs
.end (); ++iter
)
171 fallback_free ( *iter
);
173 std::printf("----\n");
175 // Delete in reverse order
177 ptrs
= alloc_series ( allocs
, sizeof ( allocs
) / sizeof ( allocs
[0] ));
178 std::printf("Allocated %zu chunks\n", ptrs
.size());
179 for ( container::reverse_iterator iter
= ptrs
.rbegin (); iter
!= ptrs
.rend (); ++iter
)
180 fallback_free ( *iter
);
182 std::printf("----\n");
184 // Alternate deletions
185 ptrs
= alloc_series ( allocs
, sizeof ( allocs
) / sizeof ( allocs
[0] ));
186 std::printf("Allocated %zu chunks\n", ptrs
.size());
187 while ( ptrs
.size () > 0 )
188 fallback_free ( pop ( ptrs
, ptrs
.size () % 1 == 1 ));
197 char *p
= (char *) fallback_malloc ( 1024 ); // too big!
198 std::printf("fallback_malloc ( 1024 ) --> %" PRIuPTR
"\n", (uintptr_t) p
);
201 p
= (char *) fallback_malloc ( 32 );
202 std::printf("fallback_malloc ( 32 ) --> %" PRIuPTR
"\n", (uintptr_t) (p
- heap
));
203 if ( !is_fallback_ptr ( p
))
204 std::printf("### p is not a fallback pointer!!\n");