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