realloc_unittest.cc 4.2 KB

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