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