Merge pull request #1691 from esdrubal/exitevent
[mono.git] / mono / utils / mono-property-hash.c
1 /*
2  * mono-property-hash.c: Hash table for (object, property) pairs
3  *
4  * Author:
5  *      Zoltan Varga (vargaz@gmail.com)
6  *
7  * (C) 2008 Novell, Inc
8  */
9
10 #include "mono-property-hash.h"
11
12 struct _MonoPropertyHash {
13         /* We use one hash table per property */
14         GHashTable *hashes;
15 };
16
17 MonoPropertyHash*
18 mono_property_hash_new (void)
19 {
20         MonoPropertyHash *hash = g_new0 (MonoPropertyHash, 1);
21
22         hash->hashes = g_hash_table_new (NULL, NULL);
23
24         return hash;
25 }
26
27 static void
28 free_hash (gpointer key, gpointer value, gpointer user_data)
29 {
30         GHashTable *hash = (GHashTable*)value;
31
32         g_hash_table_destroy (hash);
33 }
34
35 void
36 mono_property_hash_destroy (MonoPropertyHash *hash)
37 {
38         g_hash_table_foreach (hash->hashes, free_hash, NULL);
39         g_hash_table_destroy (hash->hashes);
40
41         g_free (hash);
42 }
43
44 void
45 mono_property_hash_insert (MonoPropertyHash *hash, gpointer object, guint32 property,
46                                                    gpointer value)
47 {
48         GHashTable *prop_hash;
49
50         prop_hash = (GHashTable *) g_hash_table_lookup (hash->hashes, GUINT_TO_POINTER (property));
51         if (!prop_hash) {
52                 // FIXME: Maybe use aligned_hash
53                 prop_hash = g_hash_table_new (NULL, NULL);
54                 g_hash_table_insert (hash->hashes, GUINT_TO_POINTER (property), prop_hash);
55         }
56
57         g_hash_table_insert (prop_hash, object, value);
58 }       
59
60 static void
61 remove_object (gpointer key, gpointer value, gpointer user_data)
62 {
63         GHashTable *prop_hash = (GHashTable*)value;
64
65         g_hash_table_remove (prop_hash, user_data);
66 }
67
68 void
69 mono_property_hash_remove_object (MonoPropertyHash *hash, gpointer object)
70 {
71         g_hash_table_foreach (hash->hashes, remove_object, object);
72 }
73
74 gpointer
75 mono_property_hash_lookup (MonoPropertyHash *hash, gpointer object, guint32 property)
76 {
77         GHashTable *prop_hash;
78
79         prop_hash = (GHashTable *) g_hash_table_lookup (hash->hashes, GUINT_TO_POINTER (property));
80         if (!prop_hash)
81                 return NULL;
82         return g_hash_table_lookup (prop_hash, object);
83 }
84