123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #ifndef BASE_DEBUG_LEAK_TRACKER_H_
- #define BASE_DEBUG_LEAK_TRACKER_H_
- #include <stddef.h>
- #include "build/build_config.h"
- #if !defined(NDEBUG) && !defined(__UCLIBC__)
- #define ENABLE_LEAK_TRACKER
- #endif
- #ifdef ENABLE_LEAK_TRACKER
- #include "base/check_op.h"
- #include "base/containers/linked_list.h"
- #include "base/debug/stack_trace.h"
- #include "base/logging.h"
- #endif
- namespace base {
- namespace debug {
- #ifndef ENABLE_LEAK_TRACKER
- template<typename T>
- class LeakTracker {
- public:
-
-
- ~LeakTracker() {}
- static void CheckForLeaks() {}
- static int NumLiveInstances() { return -1; }
- };
- #else
- template<typename T>
- class LeakTracker : public LinkNode<LeakTracker<T> > {
- public:
- LeakTracker() {
- instances()->Append(this);
- }
- ~LeakTracker() {
- this->RemoveFromList();
- }
- static void CheckForLeaks() {
-
- size_t count = 0;
-
-
-
- const size_t kMaxStackTracesToCopyOntoStack = 3;
- StackTrace stacktraces[kMaxStackTracesToCopyOntoStack];
- for (LinkNode<LeakTracker<T> >* node = instances()->head();
- node != instances()->end();
- node = node->next()) {
- StackTrace& allocation_stack = node->value()->allocation_stack_;
- if (count < kMaxStackTracesToCopyOntoStack)
- stacktraces[count] = allocation_stack;
- ++count;
- if (LOG_IS_ON(ERROR)) {
- LOG_STREAM(ERROR) << "Leaked " << node << " which was allocated by:";
- allocation_stack.OutputToStream(&LOG_STREAM(ERROR));
- }
- }
- CHECK_EQ(0u, count);
-
-
- if (count == 0x1234) {
- for (size_t i = 0; i < kMaxStackTracesToCopyOntoStack; ++i)
- stacktraces[i].Print();
- }
- }
- static int NumLiveInstances() {
-
- int count = 0;
- for (LinkNode<LeakTracker<T> >* node = instances()->head();
- node != instances()->end();
- node = node->next()) {
- ++count;
- }
- return count;
- }
- private:
-
- static LinkedList<LeakTracker<T> >* instances() {
- static LinkedList<LeakTracker<T> > list;
- return &list;
- }
- StackTrace allocation_stack_;
- };
- #endif
- }
- }
- #endif
|