bit_shift: add test_bit() function
[smatch.git] / smatch_function_hashtable.h
blob80410cbf7fc4fe46be75909b5904dd78484588fd
1 /*
2 * sparse/smatch_function_hashtable.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include "smatch.h"
14 #include "cwchash/hashtable.h"
16 static inline unsigned int djb2_hash(void *ky)
18 char *str = (char *)ky;
19 unsigned long hash = 5381;
20 int c;
22 while ((c = *str++))
23 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
25 return hash;
28 static inline int equalkeys(void *k1, void *k2)
30 return !strcmp((char *)k1, (char *)k2);
33 #define DEFINE_FUNCTION_ADD_HOOK(_name, _item_type, _list_type) \
34 void add_##_name(struct hashtable *table, const char *look_for, _item_type *value) \
35 { \
36 _list_type *list; \
37 char *key; \
39 key = alloc_string(look_for); \
40 list = search_##_name(table, key); \
41 if (!list) { \
42 add_ptr_list(&list, value); \
43 } else { \
44 remove_##_name(table, key); \
45 add_ptr_list(&list, value); \
46 } \
47 insert_##_name(table, key, list); \
50 static inline struct hashtable *create_function_hashtable(int size)
52 return create_hashtable(size, djb2_hash, equalkeys);
55 static inline void destroy_function_hashtable(struct hashtable *table)
57 hashtable_destroy(table, 0);
60 #define DEFINE_FUNCTION_HASHTABLE(_name, _item_type, _list_type) \
61 DEFINE_HASHTABLE_INSERT(insert_##_name, char, _list_type); \
62 DEFINE_HASHTABLE_SEARCH(search_##_name, char, _list_type); \
63 DEFINE_HASHTABLE_REMOVE(remove_##_name, char, _list_type); \
64 DEFINE_FUNCTION_ADD_HOOK(_name, _item_type, _list_type);
66 #define DEFINE_FUNCTION_HASHTABLE_STATIC(_name, _item_type, _list_type) \
67 static DEFINE_HASHTABLE_INSERT(insert_##_name, char, _list_type); \
68 static DEFINE_HASHTABLE_SEARCH(search_##_name, char, _list_type); \
69 static DEFINE_HASHTABLE_REMOVE(remove_##_name, char, _list_type); \
70 static DEFINE_FUNCTION_ADD_HOOK(_name, _item_type, _list_type);