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