malloc_extension_test.cc 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2008, 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: Craig Silverstein
  32. //
  33. // Simple test of malloc_extension. Includes test of C shims.
  34. #include "config_for_unittests.h"
  35. #include <stdio.h>
  36. #include <sys/types.h>
  37. #include "base/logging.h"
  38. #include <gperftools/malloc_extension.h>
  39. #include <gperftools/malloc_extension_c.h>
  40. int main(int argc, char** argv) {
  41. void* a = malloc(1000);
  42. size_t cxx_bytes_used, c_bytes_used;
  43. ASSERT_TRUE(MallocExtension::instance()->GetNumericProperty(
  44. "generic.current_allocated_bytes", &cxx_bytes_used));
  45. ASSERT_TRUE(MallocExtension_GetNumericProperty(
  46. "generic.current_allocated_bytes", &c_bytes_used));
  47. ASSERT_GT(cxx_bytes_used, 1000);
  48. ASSERT_EQ(cxx_bytes_used, c_bytes_used);
  49. ASSERT_TRUE(MallocExtension::instance()->VerifyAllMemory());
  50. ASSERT_TRUE(MallocExtension_VerifyAllMemory());
  51. ASSERT_EQ(MallocExtension::kOwned,
  52. MallocExtension::instance()->GetOwnership(a));
  53. // TODO(csilvers): this relies on undocumented behavior that
  54. // GetOwnership works on stack-allocated variables. Use a better test.
  55. ASSERT_EQ(MallocExtension::kNotOwned,
  56. MallocExtension::instance()->GetOwnership(&cxx_bytes_used));
  57. ASSERT_EQ(MallocExtension::kNotOwned,
  58. MallocExtension::instance()->GetOwnership(NULL));
  59. ASSERT_GE(MallocExtension::instance()->GetAllocatedSize(a), 1000);
  60. // This is just a sanity check. If we allocated too much, tcmalloc is broken
  61. ASSERT_LE(MallocExtension::instance()->GetAllocatedSize(a), 5000);
  62. ASSERT_GE(MallocExtension::instance()->GetEstimatedAllocatedSize(1000), 1000);
  63. for (int i = 0; i < 10; ++i) {
  64. void *p = malloc(i);
  65. ASSERT_GE(MallocExtension::instance()->GetAllocatedSize(p),
  66. MallocExtension::instance()->GetEstimatedAllocatedSize(i));
  67. free(p);
  68. }
  69. // Check the c-shim version too.
  70. ASSERT_EQ(MallocExtension_kOwned, MallocExtension_GetOwnership(a));
  71. ASSERT_EQ(MallocExtension_kNotOwned,
  72. MallocExtension_GetOwnership(&cxx_bytes_used));
  73. ASSERT_EQ(MallocExtension_kNotOwned, MallocExtension_GetOwnership(NULL));
  74. ASSERT_GE(MallocExtension_GetAllocatedSize(a), 1000);
  75. ASSERT_LE(MallocExtension_GetAllocatedSize(a), 5000);
  76. ASSERT_GE(MallocExtension_GetEstimatedAllocatedSize(1000), 1000);
  77. free(a);
  78. // Verify that the .cc file and .h file have the same enum values.
  79. ASSERT_EQ(static_cast<int>(MallocExtension::kUnknownOwnership),
  80. static_cast<int>(MallocExtension_kUnknownOwnership));
  81. ASSERT_EQ(static_cast<int>(MallocExtension::kOwned),
  82. static_cast<int>(MallocExtension_kOwned));
  83. ASSERT_EQ(static_cast<int>(MallocExtension::kNotOwned),
  84. static_cast<int>(MallocExtension_kNotOwned));
  85. printf("DONE\n");
  86. return 0;
  87. }