just kick off another build
[moon.git] / src / asf / asf-debug.h
blobcfaaa7def93d0edce6a8c3432f7c8829a0ddd300
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * asf-debug.h:
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
13 #ifndef _ASF_DEBUG_MOONLIGHT_H
14 #define _ASF_DEBUG_MOONLIGHT_H
16 #if DEBUG
18 #include <glib.h>
19 #include <stdio.h>
21 class ObjectTracker {
22 public:
23 ObjectTracker (const char* tn)
25 this->tn = tn;
26 id = GetNextID (tn);
27 Add ();
28 printf ("ObjectTracer::ObjectTracker (%s): id = %i\n", tn, id);
31 ~ObjectTracker ()
33 printf ("ObjectTracker::~ObjectTracker (%s): id = %i\n", tn, id);
34 Remove ();
38 static bool PrintStatus (const char* tn)
40 GHashTable* list = NULL;
42 if (current_objects != NULL) {
43 list = (GHashTable*)g_hash_table_lookup (current_objects, tn);
46 if (list != NULL) {
47 guint count = g_hash_table_size (list);
48 if (count == 0) {
49 printf ("ObjectTracking::PrintStatus (%s): No unfreed objects.\n", tn);
50 } else {
51 printf ("ObjectTracking::PrintStatus (%s): %u unfreed objects:\n", tn, count);
52 int max_items = 10;
53 g_hash_table_foreach (list, print, &max_items);
55 return count == 0;
56 } else {
57 printf ("ObjectTracking::PrintStatus (%s): No objects tracked.\n", tn);
60 return true;
63 static void print (gpointer key, gpointer value, gpointer user_data)
65 ObjectTracker* obj = (ObjectTracker*) value;
66 (*(int*)user_data)--;
67 int items_left = *(int*)user_data;
68 if (items_left >= 0) {
69 printf (" ObjectTracking::Print (%s): #%i is still alive.\n", obj->tn, obj->id);
73 private:
74 int GetNextID (const char* tn)
76 int result = 0;
78 if (current_ids == NULL)
79 current_ids = g_hash_table_new (g_str_hash, g_str_equal);
81 gpointer idp = NULL;
82 int idn = 0;
84 if (g_hash_table_lookup_extended (current_ids, tn, NULL, &idp)) {
85 idn = GPOINTER_TO_INT (idp);
88 result = ++idn;
90 printf ("ObjectTracer::GetNextID (%s): %i\n", tn, result);
92 g_hash_table_insert (current_ids, (gpointer) tn, GINT_TO_POINTER (idn));
94 return result;
97 void Add ()
99 GHashTable* list = NULL;
101 if (current_objects == NULL)
102 current_objects = g_hash_table_new (g_str_hash, g_str_equal);
104 list = (GHashTable*) g_hash_table_lookup (current_objects, tn);
106 if (list == NULL) {
107 list = g_hash_table_new (g_direct_hash, g_direct_equal);
108 g_hash_table_insert (current_objects, (gpointer) tn, list);
110 g_hash_table_insert (list, GINT_TO_POINTER (id), this);
113 void Remove ()
115 GHashTable* list = NULL;
117 if (current_objects == NULL) {
118 printf ("ObjectTracker::Remove (): the hashtable where I should be is NULL.\n");
119 return;
122 list = (GHashTable*) g_hash_table_lookup (current_objects, tn);
124 if (list == NULL) {
125 printf ("ObjectTracker::Remove (): the list where the I should be is NULL.\n");
126 return;
129 g_hash_table_remove (list, GINT_TO_POINTER (id));
132 // No automagic C++ stuff.
133 ObjectTracker ();
134 ObjectTracker (const ObjectTracker&);
135 ObjectTracker operator= (const ObjectTracker&);
137 int id;
138 const char* tn;
139 static GHashTable* current_ids;
140 static GHashTable* current_objects;
143 #endif
145 #endif