Merge remote-tracking branch 'joncham/sgen-msvc2'
[mono.git] / mono / metadata / sgen-hash-table.h
1 #ifndef __MONO_SGENHASHTABLE_H__
2 #define __MONO_SGENHASHTABLE_H__
3
4 #include "config.h"
5
6 #ifdef HAVE_SGEN_GC
7
8 #include <glib.h>
9
10 /* hash tables */
11
12 typedef struct _SgenHashTableEntry SgenHashTableEntry;
13 struct _SgenHashTableEntry {
14         SgenHashTableEntry *next;
15         gpointer key;
16         char data [MONO_ZERO_LEN_ARRAY]; /* data is pointer-aligned */
17 };
18
19 typedef struct {
20         int table_mem_type;
21         int entry_mem_type;
22         size_t data_size;
23         GHashFunc hash_func;
24         GEqualFunc equal_func;
25         SgenHashTableEntry **table;
26         guint size;
27         guint num_entries;
28 } SgenHashTable;
29
30 #define SGEN_HASH_TABLE_INIT(table_type,entry_type,data_size,hash_func,equal_func)      { (table_type), (entry_type), (data_size), (hash_func), (equal_func), NULL, 0, 0 }
31 #define SGEN_HASH_TABLE_ENTRY_SIZE(data_size)                   ((data_size) + sizeof (SgenHashTableEntry*) + sizeof (gpointer))
32
33 gpointer sgen_hash_table_lookup (SgenHashTable *table, gpointer key) MONO_INTERNAL;
34 gboolean sgen_hash_table_replace (SgenHashTable *table, gpointer key, gpointer new_value, gpointer old_value) MONO_INTERNAL;
35 gboolean sgen_hash_table_set_value (SgenHashTable *table, gpointer key, gpointer new_value, gpointer old_value) MONO_INTERNAL;
36 gboolean sgen_hash_table_set_key (SgenHashTable *hash_table, gpointer old_key, gpointer new_key) MONO_INTERNAL;
37 gboolean sgen_hash_table_remove (SgenHashTable *table, gpointer key, gpointer data_return) MONO_INTERNAL;
38
39 void sgen_hash_table_clean (SgenHashTable *table) MONO_INTERNAL;
40
41 #define sgen_hash_table_num_entries(h)  ((h)->num_entries)
42
43 #define SGEN_HASH_TABLE_FOREACH(h,k,v) do {                             \
44                 SgenHashTable *__hash_table = (h);                      \
45                 SgenHashTableEntry **__table = __hash_table->table;     \
46                 guint __i;                                              \
47                 for (__i = 0; __i < (h)->size; ++__i) {                 \
48                         SgenHashTableEntry **__iter, **__next;                  \
49                         for (__iter = &__table [__i]; *__iter; __iter = __next) {       \
50                                 SgenHashTableEntry *__entry = *__iter;  \
51                                 __next = &__entry->next;        \
52                                 (k) = __entry->key;                     \
53                                 (v) = (gpointer)__entry->data;
54
55 /* The loop must be continue'd after using this! */
56 #define SGEN_HASH_TABLE_FOREACH_REMOVE(free)    do {                    \
57                 *__iter = *__next;      \
58                 __next = __iter;        \
59                 --__hash_table->num_entries;                            \
60                 if ((free))                                             \
61                         sgen_free_internal (__entry, __hash_table->entry_mem_type); \
62         } while (0)
63
64 #define SGEN_HASH_TABLE_FOREACH_SET_KEY(k)      ((__entry)->key = (k))
65
66 #define SGEN_HASH_TABLE_FOREACH_END                                     \
67                         }                                               \
68                 }                                                       \
69         } while (0)
70
71 #endif
72
73 #endif