1 // ObjectTracker.h 2 3 #include <new> 4 #include <typeinfo> 5 6 #include <AutoLocker.h> 7 8 #include "Debug.h" 9 #include "ObjectTracker.h" 10 11 static char sTrackerBuffer[sizeof(ObjectTracker)]; 12 13 // constructor ObjectTrackable()14ObjectTrackable::ObjectTrackable() 15 { 16 ObjectTracker* tracker = ObjectTracker::GetDefault(); 17 if (tracker != NULL) 18 tracker->AddTrackable(this); 19 } 20 21 // destructor ~ObjectTrackable()22ObjectTrackable::~ObjectTrackable() 23 { 24 ObjectTracker* tracker = ObjectTracker::GetDefault(); 25 if (tracker != NULL) 26 tracker->RemoveTrackable(this); 27 } 28 29 30 // #pragma mark - 31 32 // constructor ObjectTracker()33ObjectTracker::ObjectTracker() 34 : fLock("object tracker"), 35 fTrackables() 36 { 37 } 38 39 // destructor ~ObjectTracker()40ObjectTracker::~ObjectTracker() 41 { 42 ObjectTrackable* trackable = fTrackables.First(); 43 if (trackable) { 44 WARN(("ObjectTracker: WARNING: There are still undeleted objects:\n")); 45 #ifndef _KERNEL_MODE 46 for (; trackable; trackable = fTrackables.GetNext(trackable)) { 47 WARN((" trackable: %p: type: `%s'\n", trackable, 48 typeid(*trackable).name())); 49 } 50 #endif 51 } 52 } 53 54 // InitDefault 55 ObjectTracker* InitDefault()56ObjectTracker::InitDefault() 57 { 58 if (!sTracker) 59 sTracker = new(sTrackerBuffer) ObjectTracker; 60 return sTracker; 61 } 62 63 // ExitDefault 64 void ExitDefault()65ObjectTracker::ExitDefault() 66 { 67 if (sTracker) { 68 sTracker->~ObjectTracker(); 69 sTracker = NULL; 70 } 71 } 72 73 // GetDefault 74 ObjectTracker* GetDefault()75ObjectTracker::GetDefault() 76 { 77 return sTracker; 78 } 79 80 // AddTrackable 81 void AddTrackable(ObjectTrackable * trackable)82ObjectTracker::AddTrackable(ObjectTrackable* trackable) 83 { 84 if (trackable) { 85 AutoLocker<Locker> _(fLock); 86 fTrackables.Insert(trackable); 87 } 88 } 89 90 // RemoveTrackable 91 void RemoveTrackable(ObjectTrackable * trackable)92ObjectTracker::RemoveTrackable(ObjectTrackable* trackable) 93 { 94 if (trackable) { 95 AutoLocker<Locker> _(fLock); 96 fTrackables.Remove(trackable); 97 } 98 } 99 100 // sTracker 101 ObjectTracker* ObjectTracker::sTracker = NULL; 102 103