1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/* hash table and linked lists, for libreswan
*
* Copyright (C) 2015, 2017, 2019 Andrew Cagney
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
#include "list_entry.h"
#include "shunk.h" /* has constant ptr */
#include "where.h"
/*
* Generic hash table.
*/
typedef struct { unsigned hash; } hash_t;
extern const hash_t zero_hash;
struct hash_table {
const struct list_info *const info;
hash_t (*hasher)(const void *data);
struct list_entry *(*entry)(void *data);
long nr_entries; /* approx? */
unsigned long nr_slots;
struct list_head *slots;
};
#define HASH_TABLE(STRUCT, NAME, FIELD, NR_BUCKETS) \
\
LIST_INFO(STRUCT, STRUCT##_db_entries.NAME, \
STRUCT##_##NAME##_hash_info, jam_##STRUCT); \
\
static hash_t hash_table_hash_##STRUCT##_##NAME(const void *data) \
{ \
const struct STRUCT *s = data; \
return hash_##STRUCT##_##NAME(&(*s)FIELD); \
} \
\
static struct list_head STRUCT##_##NAME##_buckets[NR_BUCKETS]; \
\
static struct list_entry *hash_table_entry_##STRUCT##_##NAME(void *data) \
{ \
struct STRUCT *s = data; \
return &s->STRUCT##_db_entries.NAME; \
} \
\
struct hash_table STRUCT##_##NAME##_hash_table = { \
.hasher = hash_table_hash_##STRUCT##_##NAME, \
.entry = hash_table_entry_##STRUCT##_##NAME, \
.nr_slots = NR_BUCKETS, \
.slots = STRUCT##_##NAME##_buckets, \
.info = &STRUCT##_##NAME##_hash_info, \
}
void init_hash_table(struct hash_table *table, struct logger *logger);
void check_hash_table(struct hash_table *table, struct logger *logger);
hash_t hash_bytes(const void *ptr, size_t len, hash_t hash);
#define hash_hunk(HUNK, HASH) \
({ \
typeof(HUNK) h_ = HUNK; /* evaluate once */ \
hash_bytes(h_.ptr, h_.len, HASH); \
})
#define hash_thing(THING, HASH) \
({ \
shunk_t h_ = THING_AS_SHUNK(THING); /* evaluate once */ \
hash_bytes(h_.ptr, h_.len, HASH); \
})
/*
* Maintain the table.
*
* Use the terms "add" and "del" as this table has no true ordering
* beyond new being the most recent insertion - rehash does "del" then
* "add" which re-orders things.
*/
void init_hash_table_entry(struct hash_table *table, void *data);
void add_hash_table_entry(struct hash_table *table, void *data);
void del_hash_table_entry(struct hash_table *table, void *data);
#define HASH_DB(STRUCT, TABLE, ...) \
\
/* backup list for when there is no accelerator */ \
\
LIST_INFO(STRUCT, STRUCT##_db_entries.list, \
STRUCT##_db_list_info, jam_##STRUCT); \
\
static struct list_head STRUCT##_db_list_head = \
INIT_LIST_HEAD(&STRUCT##_db_list_head, \
&STRUCT##_db_list_info); \
\
struct hash_table *const STRUCT##_db_hash_tables[] = { \
TABLE, ##__VA_ARGS__, \
}; \
\
void STRUCT##_db_init(struct logger *logger) \
{ \
FOR_EACH_ELEMENT(h, STRUCT##_db_hash_tables) { \
init_hash_table(*h, logger); \
} \
} \
\
void STRUCT##_db_check(struct logger *logger) \
{ \
FOR_EACH_ELEMENT(h, STRUCT##_db_hash_tables) { \
check_hash_table(*h, logger); \
} \
} \
\
void STRUCT##_db_init_##STRUCT(struct STRUCT *s) \
{ \
init_list_entry(&STRUCT##_db_list_info, s, \
&s->STRUCT##_db_entries.list); \
FOR_EACH_ELEMENT(H, STRUCT##_db_hash_tables) { \
init_hash_table_entry(*H, s); \
} \
} \
\
void STRUCT##_db_add(struct STRUCT *s) \
{ \
insert_list_entry(&STRUCT##_db_list_head, \
&s->STRUCT##_db_entries.list); \
FOR_EACH_ELEMENT(H, STRUCT##_db_hash_tables) { \
add_hash_table_entry(*H, s); \
} \
} \
\
void STRUCT##_db_del(struct STRUCT *s) \
{ \
remove_list_entry(&s->STRUCT##_db_entries.list); \
FOR_EACH_ELEMENT(h, STRUCT##_db_hash_tables) { \
del_hash_table_entry(*h, s); \
} \
}
/* not all code requires rehashing */
#define REHASH_DB(STRUCT) \
void STRUCT##_db_rehash(struct STRUCT *s) \
{ \
FOR_EACH_ELEMENT(h, STRUCT##_db_hash_tables) { \
del_hash_table_entry(*h, s); \
add_hash_table_entry(*h, s); \
} \
}
#define REHASH_DB_ENTRY(STRUCT, NAME, FIELD) \
void STRUCT##_db_rehash_##NAME(struct STRUCT *s) \
{ \
del_hash_table_entry(&STRUCT##_##NAME##_hash_table, s); \
add_hash_table_entry(&STRUCT##_##NAME##_hash_table, s); \
}
/*
* Return the head of the list entries that match HASH.
*
* Use this, in conjunction with FOR_EACH_LIST_ENTRY, when searching.
*
* Don't forget to also check that the object itself matches - more
* than one hash can map to the same list of entries.
*/
struct list_head *hash_table_bucket(struct hash_table *table, hash_t hash);
#endif
|