OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_LOCATION_H_ | 5 #ifndef BASE_LOCATION_H_ |
6 #define BASE_LOCATION_H_ | 6 #define BASE_LOCATION_H_ |
7 | 7 |
| 8 #include <cassert> |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/base_export.h" | 11 #include "base/base_export.h" |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/containers/hash_tables.h" |
12 | 14 |
13 namespace tracked_objects { | 15 namespace tracked_objects { |
14 | 16 |
15 // Location provides basic info where of an object was constructed, or was | 17 // Location provides basic info where of an object was constructed, or was |
16 // significantly brought to life. | 18 // significantly brought to life. |
17 class BASE_EXPORT Location { | 19 class BASE_EXPORT Location { |
18 public: | 20 public: |
19 // Constructor should be called with a long-lived char*, such as __FILE__. | 21 // Constructor should be called with a long-lived char*, such as __FILE__. |
20 // It assumes the provided value will persist as a global constant, and it | 22 // It assumes the provided value will persist as a global constant, and it |
21 // will not make a copy of it. | 23 // will not make a copy of it. |
22 Location(const char* function_name, | 24 Location(const char* function_name, |
23 const char* file_name, | 25 const char* file_name, |
24 int line_number, | 26 int line_number, |
25 const void* program_counter); | 27 const void* program_counter); |
26 | 28 |
27 // Provide a default constructor for easy of debugging. | 29 // Provide a default constructor for easy of debugging. |
28 Location(); | 30 Location(); |
29 | 31 |
30 // Comparison operator for insertion into a std::map<> hash tables. | 32 // Copy constructor. |
31 // All we need is *some* (any) hashing distinction. Strings should already | 33 Location(const Location& other); |
32 // be unique, so we don't bother with strcmp or such. | 34 |
33 // Use line number as the primary key (because it is fast, and usually gets us | 35 // Comparator for hash map insertion. |
34 // a difference), and then pointers as secondary keys (just to get some | 36 // No need to use |function_name_| since the other two fields uniquely |
35 // distinctions). | 37 // identify this location. |
36 bool operator < (const Location& other) const { | 38 bool operator==(const Location& other) const { |
37 if (line_number_ != other.line_number_) | 39 return line_number_ == other.line_number_ && |
38 return line_number_ < other.line_number_; | 40 file_name_ == other.file_name_; |
39 if (file_name_ != other.file_name_) | |
40 return file_name_ < other.file_name_; | |
41 return function_name_ < other.function_name_; | |
42 } | 41 } |
43 | 42 |
44 const char* function_name() const { return function_name_; } | 43 const char* function_name() const { return function_name_; } |
45 const char* file_name() const { return file_name_; } | 44 const char* file_name() const { return file_name_; } |
46 int line_number() const { return line_number_; } | 45 int line_number() const { return line_number_; } |
47 const void* program_counter() const { return program_counter_; } | 46 const void* program_counter() const { return program_counter_; } |
48 | 47 |
49 std::string ToString() const; | 48 std::string ToString() const; |
50 | 49 |
| 50 // Hash operator for hash maps. |
| 51 struct Hash { |
| 52 size_t operator()(const Location& location) const { |
| 53 // Compute the hash value using file name pointer and line number. |
| 54 // No need to use |function_name_| since the other two fields uniquely |
| 55 // identify this location. |
| 56 |
| 57 // The file name will always be uniquely identified by its pointer since |
| 58 // it comes from __FILE__, so no need to check the contents of the string. |
| 59 // See the definition of FROM_HERE in location.h, and how it is used |
| 60 // elsewhere. |
| 61 |
| 62 // Due to inconsistent definitions of uint64_t and uintptr_t, casting the |
| 63 // file name pointer to a uintptr_t causes a compiler error for some |
| 64 // platforms. The solution is to explicitly cast it to a uint64_t. |
| 65 return base::HashPair(reinterpret_cast<uint64_t>(location.file_name()), |
| 66 location.line_number()); |
| 67 } |
| 68 }; |
| 69 |
51 // Translate the some of the state in this instance into a human readable | 70 // Translate the some of the state in this instance into a human readable |
52 // string with HTML characters in the function names escaped, and append that | 71 // string with HTML characters in the function names escaped, and append that |
53 // string to |output|. Inclusion of the file_name_ and function_name_ are | 72 // string to |output|. Inclusion of the file_name_ and function_name_ are |
54 // optional, and controlled by the boolean arguments. | 73 // optional, and controlled by the boolean arguments. |
55 void Write(bool display_filename, bool display_function_name, | 74 void Write(bool display_filename, bool display_function_name, |
56 std::string* output) const; | 75 std::string* output) const; |
57 | 76 |
58 // Write function_name_ in HTML with '<' and '>' properly encoded. | 77 // Write function_name_ in HTML with '<' and '>' properly encoded. |
59 void WriteFunctionName(std::string* output) const; | 78 void WriteFunctionName(std::string* output) const; |
60 | 79 |
(...skipping 24 matching lines...) Expand all Loading... |
85 | 104 |
86 #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ | 105 #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ |
87 ::tracked_objects::Location(function_name, \ | 106 ::tracked_objects::Location(function_name, \ |
88 __FILE__, \ | 107 __FILE__, \ |
89 __LINE__, \ | 108 __LINE__, \ |
90 ::tracked_objects::GetProgramCounter()) | 109 ::tracked_objects::GetProgramCounter()) |
91 | 110 |
92 } // namespace tracked_objects | 111 } // namespace tracked_objects |
93 | 112 |
94 #endif // BASE_LOCATION_H_ | 113 #endif // BASE_LOCATION_H_ |
OLD | NEW |