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) 2004, 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 | // Author: Sanjay Ghemawat |
| 33 | // |
| 34 | // Test realloc() functionality |
| 35 | |
| 36 | #include "config_for_unittests.h" |
| 37 | #include <assert.h> // for assert |
| 38 | #include <stdio.h> |
| 39 | #include <stddef.h> // for size_t, NULL |
| 40 | #include <stdlib.h> // for free, malloc, realloc |
| 41 | #include <algorithm> // for min |
| 42 | #include "base/logging.h" |
| 43 | |
| 44 | using std::min; |
| 45 | |
| 46 | |
| 47 | // Fill a buffer of the specified size with a predetermined pattern |
| 48 | static void Fill(unsigned char* buffer, int n) { |
| 49 | for (int i = 0; i < n; i++) { |
| 50 | buffer[i] = (i & 0xff); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Check that the specified buffer has the predetermined pattern |
| 55 | // generated by Fill() |
| 56 | static bool Valid(unsigned char* buffer, int n) { |
| 57 | for (int i = 0; i < n; i++) { |
| 58 | if (buffer[i] != (i & 0xff)) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | // Return the next interesting size/delta to check. Returns -1 if no more. |
| 66 | static int NextSize(int size) { |
| 67 | if (size < 100) { |
| 68 | return size+1; |
| 69 | } else if (size < 100000) { |
| 70 | // Find next power of two |
| 71 | int power = 1; |
| 72 | while (power < size) { |
| 73 | power <<= 1; |
| 74 | } |
| 75 | |
| 76 | // Yield (power-1, power, power+1) |
| 77 | if (size < power-1) { |
| 78 | return power-1; |
| 79 | } else if (size == power-1) { |
| 80 | return power; |
| 81 | } else { |
| 82 | assert(size == power); |
| 83 | return power+1; |
| 84 | } |
| 85 | } else { |
| 86 | return -1; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | int main(int argc, char** argv) { |
| 91 | for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) { |
| 92 | for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) { |
| 93 | unsigned char* src = (unsigned char*) malloc(src_size); |
| 94 | Fill(src, src_size); |
| 95 | unsigned char* dst = (unsigned char*) realloc(src, dst_size); |
| 96 | CHECK(Valid(dst, min(src_size, dst_size))); |
| 97 | Fill(dst, dst_size); |
| 98 | CHECK(Valid(dst, dst_size)); |
| 99 | if (dst != NULL) free(dst); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Now make sure realloc works correctly even when we overflow the |
| 104 | // packed cache, so some entries are evicted from the cache. |
| 105 | // The cache has 2^12 entries, keyed by page number. |
| 106 | const int kNumEntries = 1 << 14; |
| 107 | int** p = (int**)malloc(sizeof(*p) * kNumEntries); |
| 108 | int sum = 0; |
| 109 | for (int i = 0; i < kNumEntries; i++) { |
| 110 | p[i] = (int*)malloc(8192); // no page size is likely to be bigger |
| 111 | p[i][1000] = i; // use memory deep in the heart of p |
| 112 | } |
| 113 | for (int i = 0; i < kNumEntries; i++) { |
| 114 | p[i] = (int*)realloc(p[i], 9000); |
| 115 | } |
| 116 | for (int i = 0; i < kNumEntries; i++) { |
| 117 | sum += p[i][1000]; |
| 118 | free(p[i]); |
| 119 | } |
| 120 | CHECK_EQ(kNumEntries/2 * (kNumEntries - 1), sum); // assume kNE is even |
| 121 | free(p); |
| 122 | |
| 123 | printf("PASS\n"); |
| 124 | return 0; |
| 125 | } |