OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 // consider that allocation to be a leak, even though it's not (since | 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"). | 45 // the allocated object is reachable from global data and hence "live"). |
46 | 46 |
47 #include <stdlib.h> // for abort() | 47 #include <stdlib.h> // for abort() |
48 #include <google/malloc_extension.h> | 48 #include <google/malloc_extension.h> |
49 | 49 |
50 // A dummy variable to refer from heap-checker.cc. This is to make | 50 // A dummy variable to refer from heap-checker.cc. This is to make |
51 // sure this file is not optimized out by the linker. | 51 // sure this file is not optimized out by the linker. |
52 bool heap_leak_checker_bcad_variable; | 52 bool heap_leak_checker_bcad_variable; |
53 | 53 |
54 extern void HeapLeakChecker_BeforeConstructors(); // in heap-checker.cc | |
55 extern void HeapLeakChecker_AfterDestructors(); // in heap-checker.cc | 54 extern void HeapLeakChecker_AfterDestructors(); // in heap-checker.cc |
56 | 55 |
57 // A helper class to ensure that some components of heap leak checking | 56 // A helper class to ensure that some components of heap leak checking |
58 // can happen before construction and after destruction | 57 // can happen before construction and after destruction |
59 // of all global/static objects. | 58 // of all global/static objects. |
60 class HeapLeakCheckerGlobalPrePost { | 59 class HeapLeakCheckerGlobalPrePost { |
61 public: | 60 public: |
62 HeapLeakCheckerGlobalPrePost() { | 61 HeapLeakCheckerGlobalPrePost() { |
63 if (count_ == 0) { | 62 if (count_ == 0) { |
64 HeapLeakChecker_BeforeConstructors(); | 63 // The 'new int' will ensure that we have run an initial malloc |
| 64 // hook, which will set up the heap checker via |
| 65 // MallocHook_InitAtFirstAllocation_HeapLeakChecker. See malloc_hook.cc. |
| 66 // This is done in this roundabout fashion in order to avoid self-deadlock |
| 67 // if we directly called HeapLeakChecker_BeforeConstructors here. |
| 68 delete new int; |
65 // This needs to be called before the first allocation of an STL | 69 // This needs to be called before the first allocation of an STL |
66 // object, but after libc is done setting up threads (because it | 70 // object, but after libc is done setting up threads (because it |
67 // calls setenv, which requires a thread-aware errno). By | 71 // calls setenv, which requires a thread-aware errno). By |
68 // putting it here, we hope it's the first bit of code executed | 72 // putting it here, we hope it's the first bit of code executed |
69 // after the libc global-constructor code. | 73 // after the libc global-constructor code. |
70 MallocExtension::Initialize(); | 74 MallocExtension::Initialize(); |
71 } | 75 } |
72 ++count_; | 76 ++count_; |
73 } | 77 } |
74 ~HeapLeakCheckerGlobalPrePost() { | 78 ~HeapLeakCheckerGlobalPrePost() { |
75 if (count_ <= 0) abort(); | 79 if (count_ <= 0) abort(); |
76 --count_; | 80 --count_; |
77 if (count_ == 0) HeapLeakChecker_AfterDestructors(); | 81 if (count_ == 0) HeapLeakChecker_AfterDestructors(); |
78 } | 82 } |
79 private: | 83 private: |
80 // Counter of constructions/destructions of objects of this class | 84 // Counter of constructions/destructions of objects of this class |
81 // (just in case there are more than one of them). | 85 // (just in case there are more than one of them). |
82 static int count_; | 86 static int count_; |
83 }; | 87 }; |
84 | 88 |
85 int HeapLeakCheckerGlobalPrePost::count_ = 0; | 89 int HeapLeakCheckerGlobalPrePost::count_ = 0; |
86 | 90 |
87 // The early-construction/late-destruction global object. | 91 // The early-construction/late-destruction global object. |
88 static const HeapLeakCheckerGlobalPrePost heap_leak_checker_global_pre_post; | 92 static const HeapLeakCheckerGlobalPrePost heap_leak_checker_global_pre_post; |
OLD | NEW |