heap-checker-bcad.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2005, 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. // All Rights Reserved.
  32. //
  33. // Author: Maxim Lifantsev
  34. //
  35. // A file to ensure that components of heap leak checker run before
  36. // all global object constructors and after all global object
  37. // destructors.
  38. //
  39. // This file must be the last library any binary links against.
  40. // Otherwise, the heap checker may not be able to run early enough to
  41. // catalog all the global objects in your program. If this happens,
  42. // and later in the program you allocate memory and have one of these
  43. // "uncataloged" global objects point to it, the heap checker will
  44. // consider that allocation to be a leak, even though it's not (since
  45. // the allocated object is reachable from global data and hence "live").
  46. #include <stdlib.h> // for abort()
  47. #include <gperftools/malloc_extension.h>
  48. // A dummy variable to refer from heap-checker.cc. This is to make
  49. // sure this file is not optimized out by the linker.
  50. bool heap_leak_checker_bcad_variable;
  51. extern void HeapLeakChecker_AfterDestructors(); // in heap-checker.cc
  52. // A helper class to ensure that some components of heap leak checking
  53. // can happen before construction and after destruction
  54. // of all global/static objects.
  55. class HeapLeakCheckerGlobalPrePost {
  56. public:
  57. HeapLeakCheckerGlobalPrePost() {
  58. if (count_ == 0) {
  59. // The 'new int' will ensure that we have run an initial malloc
  60. // hook, which will set up the heap checker via
  61. // MallocHook_InitAtFirstAllocation_HeapLeakChecker. See malloc_hook.cc.
  62. // This is done in this roundabout fashion in order to avoid self-deadlock
  63. // if we directly called HeapLeakChecker_BeforeConstructors here.
  64. delete new int;
  65. // This needs to be called before the first allocation of an STL
  66. // object, but after libc is done setting up threads (because it
  67. // calls setenv, which requires a thread-aware errno). By
  68. // putting it here, we hope it's the first bit of code executed
  69. // after the libc global-constructor code.
  70. MallocExtension::Initialize();
  71. }
  72. ++count_;
  73. }
  74. ~HeapLeakCheckerGlobalPrePost() {
  75. if (count_ <= 0) abort();
  76. --count_;
  77. if (count_ == 0) HeapLeakChecker_AfterDestructors();
  78. }
  79. private:
  80. // Counter of constructions/destructions of objects of this class
  81. // (just in case there are more than one of them).
  82. static int count_;
  83. };
  84. int HeapLeakCheckerGlobalPrePost::count_ = 0;
  85. // The early-construction/late-destruction global object.
  86. static const HeapLeakCheckerGlobalPrePost heap_leak_checker_global_pre_post;