Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame^] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | /* Copyright (c) 2006, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | // A test for low_level_alloc.cc |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <map> |
| 36 | #include "base/low_level_alloc.h" |
| 37 | #include "base/logging.h" |
| 38 | #include <gperftools/malloc_hook.h> |
| 39 | |
| 40 | using std::map; |
| 41 | |
| 42 | // a block of memory obtained from the allocator |
| 43 | struct BlockDesc { |
| 44 | char *ptr; // pointer to memory |
| 45 | int len; // number of bytes |
| 46 | int fill; // filled with data starting with this |
| 47 | }; |
| 48 | |
| 49 | // Check that the pattern placed in the block d |
| 50 | // by RandomizeBlockDesc is still there. |
| 51 | static void CheckBlockDesc(const BlockDesc &d) { |
| 52 | for (int i = 0; i != d.len; i++) { |
| 53 | CHECK((d.ptr[i] & 0xff) == ((d.fill + i) & 0xff)); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Fill the block "*d" with a pattern |
| 58 | // starting with a random byte. |
| 59 | static void RandomizeBlockDesc(BlockDesc *d) { |
| 60 | d->fill = rand() & 0xff; |
| 61 | for (int i = 0; i != d->len; i++) { |
| 62 | d->ptr[i] = (d->fill + i) & 0xff; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Use to indicate to the malloc hooks that |
| 67 | // this calls is from LowLevelAlloc. |
| 68 | static bool using_low_level_alloc = false; |
| 69 | |
| 70 | // n times, toss a coin, and based on the outcome |
| 71 | // either allocate a new block or deallocate an old block. |
| 72 | // New blocks are placed in a map with a random key |
| 73 | // and initialized with RandomizeBlockDesc(). |
| 74 | // If keys conflict, the older block is freed. |
| 75 | // Old blocks are always checked with CheckBlockDesc() |
| 76 | // before being freed. At the end of the run, |
| 77 | // all remaining allocated blocks are freed. |
| 78 | // If use_new_arena is true, use a fresh arena, and then delete it. |
| 79 | // If call_malloc_hook is true and user_arena is true, |
| 80 | // allocations and deallocations are reported via the MallocHook |
| 81 | // interface. |
| 82 | static void Test(bool use_new_arena, bool call_malloc_hook, int n) { |
| 83 | typedef map<int, BlockDesc> AllocMap; |
| 84 | AllocMap allocated; |
| 85 | AllocMap::iterator it; |
| 86 | BlockDesc block_desc; |
| 87 | int rnd; |
| 88 | LowLevelAlloc::Arena *arena = 0; |
| 89 | if (use_new_arena) { |
| 90 | int32 flags = call_malloc_hook? LowLevelAlloc::kCallMallocHook : 0; |
| 91 | arena = LowLevelAlloc::NewArena(flags, LowLevelAlloc::DefaultArena()); |
| 92 | } |
| 93 | for (int i = 0; i != n; i++) { |
| 94 | if (i != 0 && i % 10000 == 0) { |
| 95 | printf("."); |
| 96 | fflush(stdout); |
| 97 | } |
| 98 | |
| 99 | switch(rand() & 1) { // toss a coin |
| 100 | case 0: // coin came up heads: add a block |
| 101 | using_low_level_alloc = true; |
| 102 | block_desc.len = rand() & 0x3fff; |
| 103 | block_desc.ptr = |
| 104 | reinterpret_cast<char *>( |
| 105 | arena == 0 |
| 106 | ? LowLevelAlloc::Alloc(block_desc.len) |
| 107 | : LowLevelAlloc::AllocWithArena(block_desc.len, arena)); |
| 108 | using_low_level_alloc = false; |
| 109 | RandomizeBlockDesc(&block_desc); |
| 110 | rnd = rand(); |
| 111 | it = allocated.find(rnd); |
| 112 | if (it != allocated.end()) { |
| 113 | CheckBlockDesc(it->second); |
| 114 | using_low_level_alloc = true; |
| 115 | LowLevelAlloc::Free(it->second.ptr); |
| 116 | using_low_level_alloc = false; |
| 117 | it->second = block_desc; |
| 118 | } else { |
| 119 | allocated[rnd] = block_desc; |
| 120 | } |
| 121 | break; |
| 122 | case 1: // coin came up tails: remove a block |
| 123 | it = allocated.begin(); |
| 124 | if (it != allocated.end()) { |
| 125 | CheckBlockDesc(it->second); |
| 126 | using_low_level_alloc = true; |
| 127 | LowLevelAlloc::Free(it->second.ptr); |
| 128 | using_low_level_alloc = false; |
| 129 | allocated.erase(it); |
| 130 | } |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | // remove all remaniing blocks |
| 135 | while ((it = allocated.begin()) != allocated.end()) { |
| 136 | CheckBlockDesc(it->second); |
| 137 | using_low_level_alloc = true; |
| 138 | LowLevelAlloc::Free(it->second.ptr); |
| 139 | using_low_level_alloc = false; |
| 140 | allocated.erase(it); |
| 141 | } |
| 142 | if (use_new_arena) { |
| 143 | CHECK(LowLevelAlloc::DeleteArena(arena)); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // used for counting allocates and frees |
| 148 | static int32 allocates; |
| 149 | static int32 frees; |
| 150 | |
| 151 | // called on each alloc if kCallMallocHook specified |
| 152 | static void AllocHook(const void *p, size_t size) { |
| 153 | if (using_low_level_alloc) { |
| 154 | allocates++; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // called on each free if kCallMallocHook specified |
| 159 | static void FreeHook(const void *p) { |
| 160 | if (using_low_level_alloc) { |
| 161 | frees++; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | int main(int argc, char *argv[]) { |
| 166 | // This is needed by maybe_threads_unittest.sh, which parses argv[0] |
| 167 | // to figure out what directory low_level_alloc_unittest is in. |
| 168 | if (argc != 1) { |
| 169 | fprintf(stderr, "USAGE: %s\n", argv[0]); |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | CHECK(MallocHook::AddNewHook(&AllocHook)); |
| 174 | CHECK(MallocHook::AddDeleteHook(&FreeHook)); |
| 175 | CHECK_EQ(allocates, 0); |
| 176 | CHECK_EQ(frees, 0); |
| 177 | Test(false, false, 50000); |
| 178 | CHECK_NE(allocates, 0); // default arena calls hooks |
| 179 | CHECK_NE(frees, 0); |
| 180 | for (int i = 0; i != 16; i++) { |
| 181 | bool call_hooks = ((i & 1) == 1); |
| 182 | allocates = 0; |
| 183 | frees = 0; |
| 184 | Test(true, call_hooks, 15000); |
| 185 | if (call_hooks) { |
| 186 | CHECK_GT(allocates, 5000); // arena calls hooks |
| 187 | CHECK_GT(frees, 5000); |
| 188 | } else { |
| 189 | CHECK_EQ(allocates, 0); // arena doesn't call hooks |
| 190 | CHECK_EQ(frees, 0); |
| 191 | } |
| 192 | } |
| 193 | printf("\nPASS\n"); |
| 194 | CHECK(MallocHook::RemoveNewHook(&AllocHook)); |
| 195 | CHECK(MallocHook::RemoveDeleteHook(&FreeHook)); |
| 196 | return 0; |
| 197 | } |