system-alloc_unittest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2007, 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: Arun Sharma
  32. #include "config_for_unittests.h"
  33. #include "system-alloc.h"
  34. #include <stdio.h>
  35. #if defined HAVE_STDINT_H
  36. #include <stdint.h> // to get uintptr_t
  37. #elif defined HAVE_INTTYPES_H
  38. #include <inttypes.h> // another place uintptr_t might be defined
  39. #endif
  40. #include <sys/types.h>
  41. #include <algorithm>
  42. #include <limits>
  43. #include "base/logging.h" // for Check_GEImpl, Check_LTImpl, etc
  44. #include <gperftools/malloc_extension.h> // for MallocExtension::instance
  45. #include "common.h" // for kAddressBits
  46. class ArraySysAllocator : public SysAllocator {
  47. public:
  48. // Was this allocator invoked at least once?
  49. bool invoked_;
  50. ArraySysAllocator() : SysAllocator() {
  51. ptr_ = 0;
  52. invoked_ = false;
  53. }
  54. void* Alloc(size_t size, size_t *actual_size, size_t alignment) {
  55. invoked_ = true;
  56. if (size > kArraySize) {
  57. return NULL;
  58. }
  59. void *result = &array_[ptr_];
  60. uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
  61. if (actual_size) {
  62. *actual_size = size;
  63. }
  64. // Try to get more memory for alignment
  65. size_t extra = alignment - (ptr & (alignment-1));
  66. size += extra;
  67. CHECK_LT(ptr_ + size, kArraySize);
  68. if ((ptr & (alignment-1)) != 0) {
  69. ptr += alignment - (ptr & (alignment-1));
  70. }
  71. ptr_ += size;
  72. return reinterpret_cast<void *>(ptr);
  73. }
  74. void DumpStats() {
  75. }
  76. private:
  77. static const int kArraySize = 8 * 1024 * 1024;
  78. char array_[kArraySize];
  79. // We allocate the next chunk from here
  80. int ptr_;
  81. };
  82. const int ArraySysAllocator::kArraySize;
  83. ArraySysAllocator a;
  84. static void TestBasicInvoked() {
  85. MallocExtension::instance()->SetSystemAllocator(&a);
  86. // An allocation size that is likely to trigger the system allocator.
  87. // XXX: this is implementation specific.
  88. char *p = new char[1024 * 1024];
  89. delete [] p;
  90. // Make sure that our allocator was invoked.
  91. CHECK(a.invoked_);
  92. }
  93. #if 0 // could port this to various OSs, but won't bother for now
  94. TEST(AddressBits, CpuVirtualBits) {
  95. // Check that kAddressBits is as least as large as either the number of bits
  96. // in a pointer or as the number of virtual bits handled by the processor.
  97. // To be effective this test must be run on each processor model.
  98. const int kPointerBits = 8 * sizeof(void*);
  99. const int kImplementedVirtualBits = NumImplementedVirtualBits();
  100. CHECK_GE(kAddressBits, std::min(kImplementedVirtualBits, kPointerBits));
  101. }
  102. #endif
  103. static void TestBasicRetryFailTest() {
  104. // Check with the allocator still works after a failed allocation.
  105. //
  106. // There is no way to call malloc and guarantee it will fail. malloc takes a
  107. // size_t parameter and the C++ standard does not constrain the size of
  108. // size_t. For example, consider an implementation where size_t is 32 bits
  109. // and pointers are 64 bits.
  110. //
  111. // It is likely, though, that sizeof(size_t) == sizeof(void*). In that case,
  112. // the first allocation here might succeed but the second allocation must
  113. // fail.
  114. //
  115. // If the second allocation succeeds, you will have to rewrite or
  116. // disable this test.
  117. // The weird parens are to avoid macro-expansion of 'max' on windows.
  118. const size_t kHugeSize = (std::numeric_limits<size_t>::max)() / 2;
  119. void* p1 = malloc(kHugeSize);
  120. void* p2 = malloc(kHugeSize);
  121. CHECK(p2 == NULL);
  122. if (p1 != NULL) free(p1);
  123. char* q = new char[1024];
  124. CHECK(q != NULL);
  125. delete [] q;
  126. }
  127. int main(int argc, char** argv) {
  128. TestBasicInvoked();
  129. TestBasicRetryFailTest();
  130. printf("PASS\n");
  131. return 0;
  132. }