2 * sparse/smatch_function_hashtable.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 #include "cwchash/hashtable.h"
16 static inline unsigned int djb2_hash(void *ky
)
18 char *str
= (char *)ky
;
19 unsigned long hash
= 5381;
23 hash
= ((hash
<< 5) + hash
) + c
; /* hash * 33 + c */
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) \
39 key = alloc_string(look_for); \
40 list = search_##_name(table, key); \
42 add_ptr_list(&list, value); \
44 remove_##_name(table, key); \
45 add_ptr_list(&list, value); \
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);