thread_dealloc_unittest.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // Check that we do not leak memory when cycling through lots of threads.
  34. #include "config_for_unittests.h"
  35. #include <stdio.h>
  36. #ifdef HAVE_UNISTD_H
  37. #include <unistd.h> // for sleep()
  38. #endif
  39. #include "base/logging.h"
  40. #include <gperftools/malloc_extension.h>
  41. #include "tests/testutil.h" // for RunThread()
  42. // Size/number of objects to allocate per thread (1 MB per thread)
  43. static const int kObjectSize = 1024;
  44. static const int kNumObjects = 1024;
  45. // Number of threads to create and destroy
  46. static const int kNumThreads = 1000;
  47. // Allocate lots of stuff
  48. static void AllocStuff() {
  49. void** objects = new void*[kNumObjects];
  50. for (int i = 0; i < kNumObjects; i++) {
  51. objects[i] = malloc(kObjectSize);
  52. }
  53. for (int i = 0; i < kNumObjects; i++) {
  54. free(objects[i]);
  55. }
  56. delete[] objects;
  57. }
  58. int main(int argc, char** argv) {
  59. static const int kDisplaySize = 1048576;
  60. char* display = new char[kDisplaySize];
  61. for (int i = 0; i < kNumThreads; i++) {
  62. RunThread(&AllocStuff);
  63. if (((i+1) % 200) == 0) {
  64. fprintf(stderr, "Iteration: %d of %d\n", (i+1), kNumThreads);
  65. MallocExtension::instance()->GetStats(display, kDisplaySize);
  66. fprintf(stderr, "%s\n", display);
  67. }
  68. }
  69. delete[] display;
  70. printf("PASS\n");
  71. #ifdef HAVE_UNISTD_H
  72. sleep(1); // Prevent exit race problem with glibc
  73. #endif
  74. return 0;
  75. }