2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
22 #include "cwchash/hashtable.h"
24 static inline unsigned int djb2_hash(void *ky
)
26 char *str
= (char *)ky
;
27 unsigned long hash
= 5381;
31 hash
= ((hash
<< 5) + hash
) + c
; /* hash * 33 + c */
36 static inline int equalkeys(void *k1
, void *k2
)
38 return !strcmp((char *)k1
, (char *)k2
);
41 #define DEFINE_FUNCTION_ADD_HOOK(_name, _item_type, _list_type) \
42 void add_##_name(struct hashtable *table, const char *look_for, _item_type *value) \
47 key = alloc_string(look_for); \
48 list = search_##_name(table, key); \
50 add_ptr_list(&list, value); \
52 remove_##_name(table, key); \
53 add_ptr_list(&list, value); \
55 insert_##_name(table, key, list); \
58 static inline struct hashtable
*create_function_hashtable(int size
)
60 return create_hashtable(size
, djb2_hash
, equalkeys
);
63 static inline void destroy_function_hashtable(struct hashtable
*table
)
65 hashtable_destroy(table
, 0);
68 #define DEFINE_FUNCTION_HASHTABLE(_name, _item_type, _list_type) \
69 DEFINE_HASHTABLE_INSERT(insert_##_name, char, _list_type); \
70 DEFINE_HASHTABLE_SEARCH(search_##_name, char, _list_type); \
71 DEFINE_HASHTABLE_REMOVE(remove_##_name, char, _list_type); \
72 DEFINE_FUNCTION_ADD_HOOK(_name, _item_type, _list_type);
74 #define DEFINE_FUNCTION_HASHTABLE_STATIC(_name, _item_type, _list_type) \
75 static DEFINE_HASHTABLE_INSERT(insert_##_name, char, _list_type); \
76 static DEFINE_HASHTABLE_SEARCH(search_##_name, char, _list_type); \
77 static DEFINE_HASHTABLE_REMOVE(remove_##_name, char, _list_type); \
78 static DEFINE_FUNCTION_ADD_HOOK(_name, _item_type, _list_type);
80 #define DEFINE_STRING_HASHTABLE_STATIC(_name) \
81 static DEFINE_HASHTABLE_INSERT(insert_##_name, char, int); \
82 static DEFINE_HASHTABLE_SEARCH(search_##_name, char, int); \
83 static struct hashtable *_name
85 static inline void load_hashtable_helper(const char *file
, int (*insert_func
)(struct hashtable
*, char *, int *), struct hashtable
*table
)
91 snprintf(filename
, sizeof(filename
), "%s.%s", option_project_str
, file
);
92 token
= get_tokens_file(filename
);
95 if (token_type(token
) != TOKEN_STREAMBEGIN
)
98 while (token_type(token
) != TOKEN_STREAMEND
) {
99 if (token_type(token
) != TOKEN_IDENT
)
101 name
= alloc_string(show_ident(token
->ident
));
102 insert_func(table
, name
, (void *)1);
108 #define load_strings(file, _table) load_hashtable_helper(file, insert_##_table, _table)