ipc/chromium/src/base/tracked.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 //------------------------------------------------------------------------------
michael@0 6 // Tracked is the base class for all tracked objects. During construction, it
michael@0 7 // registers the fact that an instance was created, and at destruction time, it
michael@0 8 // records that event. The instance may be tagged with a name, which is refered
michael@0 9 // to as its Location. The Location is a file and line number, most
michael@0 10 // typically indicated where the object was constructed. In some cases, as the
michael@0 11 // object's significance is refined (for example, a Task object is augmented to
michael@0 12 // do additonal things), its Location may be redefined to that later location.
michael@0 13
michael@0 14 // Tracking includes (for each instance) recording the birth thread, death
michael@0 15 // thread, and duration of life (from construction to destruction). All this
michael@0 16 // data is accumulated and filtered for review at about:objects.
michael@0 17
michael@0 18 #ifndef BASE_TRACKED_H_
michael@0 19 #define BASE_TRACKED_H_
michael@0 20
michael@0 21 #include <string>
michael@0 22
michael@0 23 #include "base/time.h"
michael@0 24
michael@0 25 #ifndef NDEBUG
michael@0 26 #ifndef TRACK_ALL_TASK_OBJECTS
michael@0 27 #define TRACK_ALL_TASK_OBJECTS
michael@0 28 #endif // TRACK_ALL_TASK_OBJECTS
michael@0 29 #endif // NDEBUG
michael@0 30
michael@0 31 namespace tracked_objects {
michael@0 32
michael@0 33 //------------------------------------------------------------------------------
michael@0 34 // Location provides basic info where of an object was constructed, or was
michael@0 35 // significantly brought to life.
michael@0 36
michael@0 37 class Location {
michael@0 38 public:
michael@0 39 // Constructor should be called with a long-lived char*, such as __FILE__.
michael@0 40 // It assumes the provided value will persist as a global constant, and it
michael@0 41 // will not make a copy of it.
michael@0 42 Location(const char* function_name, const char* file_name, int line_number)
michael@0 43 : function_name_(function_name),
michael@0 44 file_name_(file_name),
michael@0 45 line_number_(line_number) { }
michael@0 46
michael@0 47 // Provide a default constructor for easy of debugging.
michael@0 48 Location()
michael@0 49 : function_name_("Unknown"),
michael@0 50 file_name_("Unknown"),
michael@0 51 line_number_(-1) { }
michael@0 52
michael@0 53 // Comparison operator for insertion into a std::map<> hash tables.
michael@0 54 // All we need is *some* (any) hashing distinction. Strings should already
michael@0 55 // be unique, so we don't bother with strcmp or such.
michael@0 56 // Use line number as the primary key (because it is fast, and usually gets us
michael@0 57 // a difference), and then pointers as secondary keys (just to get some
michael@0 58 // distinctions).
michael@0 59 bool operator < (const Location& other) const {
michael@0 60 if (line_number_ != other.line_number_)
michael@0 61 return line_number_ < other.line_number_;
michael@0 62 if (file_name_ != other.file_name_)
michael@0 63 return file_name_ < other.file_name_;
michael@0 64 return function_name_ < other.function_name_;
michael@0 65 }
michael@0 66
michael@0 67 const char* function_name() const { return function_name_; }
michael@0 68 const char* file_name() const { return file_name_; }
michael@0 69 int line_number() const { return line_number_; }
michael@0 70
michael@0 71 void Write(bool display_filename, bool display_function_name,
michael@0 72 std::string* output) const;
michael@0 73
michael@0 74 // Write function_name_ in HTML with '<' and '>' properly encoded.
michael@0 75 void WriteFunctionName(std::string* output) const;
michael@0 76
michael@0 77 private:
michael@0 78 const char* const function_name_;
michael@0 79 const char* const file_name_;
michael@0 80 const int line_number_;
michael@0 81 };
michael@0 82
michael@0 83
michael@0 84 //------------------------------------------------------------------------------
michael@0 85 // Define a macro to record the current source location.
michael@0 86
michael@0 87 #define FROM_HERE tracked_objects::Location(__FUNCTION__, __FILE__, __LINE__)
michael@0 88
michael@0 89
michael@0 90 //------------------------------------------------------------------------------
michael@0 91
michael@0 92
michael@0 93 class Births;
michael@0 94
michael@0 95 class Tracked {
michael@0 96 public:
michael@0 97 Tracked();
michael@0 98 virtual ~Tracked();
michael@0 99
michael@0 100 // Used to record the FROM_HERE location of a caller.
michael@0 101 void SetBirthPlace(const Location& from_here);
michael@0 102
michael@0 103 // When a task sits around a long time, such as in a timer, or object watcher,
michael@0 104 // this method should be called when the task becomes active, and its
michael@0 105 // significant lifetime begins (and its waiting to be woken up has passed).
michael@0 106 void ResetBirthTime();
michael@0 107
michael@0 108 bool MissingBirthplace() const;
michael@0 109
michael@0 110 private:
michael@0 111 #ifdef TRACK_ALL_TASK_OBJECTS
michael@0 112
michael@0 113 // Pointer to instance were counts of objects with the same birth location
michael@0 114 // (on the same thread) are stored.
michael@0 115 Births* tracked_births_;
michael@0 116 // The time this object was constructed. If its life consisted of a long
michael@0 117 // waiting period, and then it became active, then this value is generally
michael@0 118 // reset before the object begins it active life.
michael@0 119 base::Time tracked_birth_time_;
michael@0 120
michael@0 121 #endif // TRACK_ALL_TASK_OBJECTS
michael@0 122
michael@0 123 DISALLOW_COPY_AND_ASSIGN(Tracked);
michael@0 124 };
michael@0 125
michael@0 126 } // namespace tracked_objects
michael@0 127
michael@0 128 #endif // BASE_TRACKED_H_

mercurial