Marshall Clow | e2dcb75 | 2011-07-20 15:04:39 +0000 | [diff] [blame^] | 1 | #include <iostream> |
| 2 | #include <deque> |
| 3 | |
| 4 | #include <pthread.h> |
| 5 | |
| 6 | typedef std::deque<void *> container; |
| 7 | |
| 8 | // #define DEBUG_FALLBACK_MALLOC |
| 9 | #define INSTRUMENT_FALLBACK_MALLOC |
| 10 | #include "../src/fallback_malloc.cpp" |
| 11 | |
| 12 | container alloc_series ( size_t sz ) { |
| 13 | container ptrs; |
| 14 | void *p; |
| 15 | |
| 16 | while ( NULL != ( p = fallback_malloc ( sz ))) |
| 17 | ptrs.push_back ( p ); |
| 18 | return ptrs; |
| 19 | } |
| 20 | |
| 21 | container alloc_series ( size_t sz, float growth ) { |
| 22 | container ptrs; |
| 23 | void *p; |
| 24 | |
| 25 | while ( NULL != ( p = fallback_malloc ( sz ))) { |
| 26 | ptrs.push_back ( p ); |
| 27 | sz *= growth; |
| 28 | } |
| 29 | |
| 30 | return ptrs; |
| 31 | } |
| 32 | |
| 33 | container alloc_series ( const size_t *first, size_t len ) { |
| 34 | container ptrs; |
| 35 | const size_t *last = first + len; |
| 36 | void * p; |
| 37 | |
| 38 | for ( const size_t *iter = first; iter != last; ++iter ) { |
| 39 | if ( NULL == (p = fallback_malloc ( *iter ))) |
| 40 | break; |
| 41 | ptrs.push_back ( p ); |
| 42 | } |
| 43 | |
| 44 | return ptrs; |
| 45 | } |
| 46 | |
| 47 | void *pop ( container &c, bool from_end ) { |
| 48 | void *ptr; |
| 49 | if ( from_end ) { |
| 50 | ptr = c.back (); |
| 51 | c.pop_back (); |
| 52 | } |
| 53 | else { |
| 54 | ptr = c.front (); |
| 55 | c.pop_front (); |
| 56 | } |
| 57 | return ptr; |
| 58 | } |
| 59 | |
| 60 | void exhaustion_test1 () { |
| 61 | container ptrs; |
| 62 | |
| 63 | init_heap (); |
| 64 | std::cout << "Constant exhaustion tests" << std::endl; |
| 65 | |
| 66 | // Delete in allocation order |
| 67 | ptrs = alloc_series ( 32 ); |
| 68 | std::cout << "Allocated " << ptrs.size () << " 32 byte chunks" << std::endl; |
| 69 | print_free_list (); |
| 70 | for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter ) |
| 71 | fallback_free ( *iter ); |
| 72 | print_free_list (); |
| 73 | std::cout << "----" << std::endl; |
| 74 | |
| 75 | // Delete in reverse order |
| 76 | ptrs = alloc_series ( 32 ); |
| 77 | std::cout << "Allocated " << ptrs.size () << " 32 byte chunks" << std::endl; |
| 78 | for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter ) |
| 79 | fallback_free ( *iter ); |
| 80 | print_free_list (); |
| 81 | std::cout << "----" << std::endl; |
| 82 | |
| 83 | // Alternate deletions |
| 84 | ptrs = alloc_series ( 32 ); |
| 85 | std::cout << "Allocated " << ptrs.size () << " 32 byte chunks" << std::endl; |
| 86 | while ( ptrs.size () > 0 ) |
| 87 | fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 )); |
| 88 | print_free_list (); |
| 89 | } |
| 90 | |
| 91 | void exhaustion_test2 () { |
| 92 | container ptrs; |
| 93 | init_heap (); |
| 94 | |
| 95 | std::cout << "Growing exhaustion tests" << std::endl; |
| 96 | |
| 97 | // Delete in allocation order |
| 98 | ptrs = alloc_series ( 32, 1.5 ); |
| 99 | std::cout << "Allocated " << ptrs.size () << " { 32, 48, 72, 108, 162 ... } byte chunks" << std::endl; |
| 100 | print_free_list (); |
| 101 | for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter ) |
| 102 | fallback_free ( *iter ); |
| 103 | print_free_list (); |
| 104 | std::cout << "----" << std::endl; |
| 105 | |
| 106 | // Delete in reverse order |
| 107 | print_free_list (); |
| 108 | ptrs = alloc_series ( 32, 1.5 ); |
| 109 | std::cout << "Allocated " << ptrs.size () << " { 32, 48, 72, 108, 162 ... } byte chunks" << std::endl; |
| 110 | for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter ) |
| 111 | fallback_free ( *iter ); |
| 112 | print_free_list (); |
| 113 | std::cout << "----" << std::endl; |
| 114 | |
| 115 | // Alternate deletions |
| 116 | ptrs = alloc_series ( 32, 1.5 ); |
| 117 | std::cout << "Allocated " << ptrs.size () << " { 32, 48, 72, 108, 162 ... } byte chunks" << std::endl; |
| 118 | while ( ptrs.size () > 0 ) |
| 119 | fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 )); |
| 120 | print_free_list (); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | void exhaustion_test3 () { |
| 125 | const size_t allocs [] = { 124, 60, 252, 60, 4 }; |
| 126 | container ptrs; |
| 127 | init_heap (); |
| 128 | |
| 129 | std::cout << "Complete exhaustion tests" << std::endl; |
| 130 | |
| 131 | // Delete in allocation order |
| 132 | ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] )); |
| 133 | std::cout << "Allocated " << ptrs.size () << " chunks" << std::endl; |
| 134 | print_free_list (); |
| 135 | for ( container::iterator iter = ptrs.begin (); iter != ptrs.end (); ++iter ) |
| 136 | fallback_free ( *iter ); |
| 137 | print_free_list (); |
| 138 | std::cout << "----" << std::endl; |
| 139 | |
| 140 | // Delete in reverse order |
| 141 | print_free_list (); |
| 142 | ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] )); |
| 143 | std::cout << "Allocated " << ptrs.size () << " chunks" << std::endl; |
| 144 | for ( container::reverse_iterator iter = ptrs.rbegin (); iter != ptrs.rend (); ++iter ) |
| 145 | fallback_free ( *iter ); |
| 146 | print_free_list (); |
| 147 | std::cout << "----" << std::endl; |
| 148 | |
| 149 | // Alternate deletions |
| 150 | ptrs = alloc_series ( allocs, sizeof ( allocs ) / sizeof ( allocs[0] )); |
| 151 | std::cout << "Allocated " << ptrs.size () << " chunks" << std::endl; |
| 152 | while ( ptrs.size () > 0 ) |
| 153 | fallback_free ( pop ( ptrs, ptrs.size () % 1 == 1 )); |
| 154 | print_free_list (); |
| 155 | |
| 156 | } |
| 157 | |
| 158 | |
| 159 | int main ( int argc, char *argv [] ) { |
| 160 | print_free_list (); |
| 161 | |
| 162 | char *p = (char *) fallback_malloc ( 1024 ); // too big! |
| 163 | std::cout << "fallback_malloc ( 1024 ) --> " << (unsigned long ) p << std::endl; |
| 164 | print_free_list (); |
| 165 | |
| 166 | p = (char *) fallback_malloc ( 32 ); |
| 167 | std::cout << "fallback_malloc ( 32 ) --> " << (unsigned long) (p - heap) << std::endl; |
| 168 | if ( !is_fallback_ptr ( p )) |
| 169 | std::cout << "### p is not a fallback pointer!!" << std::endl; |
| 170 | |
| 171 | print_free_list (); |
| 172 | fallback_free ( p ); |
| 173 | print_free_list (); |
| 174 | |
| 175 | std::cout << std::endl; |
| 176 | exhaustion_test1 (); std::cout << std::endl; |
| 177 | exhaustion_test2 (); std::cout << std::endl; |
| 178 | exhaustion_test3 (); std::cout << std::endl; |
| 179 | return 0; |
| 180 | } |