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