xref: /illumos-gate/usr/src/tools/smatch/src/smatch_function_hashtable.h (revision 1f5207b7604fb44407eb4342aff613f7c4508508)
1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2010 Dan Carpenter.
3*1f5207b7SJohn Levon  *
4*1f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon  * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*1f5207b7SJohn Levon  * GNU General Public License for more details.
13*1f5207b7SJohn Levon  *
14*1f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon  */
17*1f5207b7SJohn Levon 
18*1f5207b7SJohn Levon #include <stdlib.h>
19*1f5207b7SJohn Levon #include <stdio.h>
20*1f5207b7SJohn Levon #include <string.h>
21*1f5207b7SJohn Levon #include "smatch.h"
22*1f5207b7SJohn Levon #include "cwchash/hashtable.h"
23*1f5207b7SJohn Levon 
djb2_hash(void * ky)24*1f5207b7SJohn Levon static inline unsigned int djb2_hash(void *ky)
25*1f5207b7SJohn Levon {
26*1f5207b7SJohn Levon 	char *str = (char *)ky;
27*1f5207b7SJohn Levon 	unsigned long hash = 5381;
28*1f5207b7SJohn Levon 	int c;
29*1f5207b7SJohn Levon 
30*1f5207b7SJohn Levon 	while ((c = *str++))
31*1f5207b7SJohn Levon 		hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
32*1f5207b7SJohn Levon 
33*1f5207b7SJohn Levon         return hash;
34*1f5207b7SJohn Levon }
35*1f5207b7SJohn Levon 
equalkeys(void * k1,void * k2)36*1f5207b7SJohn Levon static inline int equalkeys(void *k1, void *k2)
37*1f5207b7SJohn Levon {
38*1f5207b7SJohn Levon 	return !strcmp((char *)k1, (char *)k2);
39*1f5207b7SJohn Levon }
40*1f5207b7SJohn Levon 
41*1f5207b7SJohn Levon #define DEFINE_FUNCTION_ADD_HOOK(_name, _item_type, _list_type) \
42*1f5207b7SJohn Levon void add_##_name(struct hashtable *table, const char *look_for, _item_type *value) \
43*1f5207b7SJohn Levon {                                                               \
44*1f5207b7SJohn Levon 	_list_type *list;                                       \
45*1f5207b7SJohn Levon 	char *key;                                              \
46*1f5207b7SJohn Levon                                                                 \
47*1f5207b7SJohn Levon 	key = alloc_string(look_for);                           \
48*1f5207b7SJohn Levon 	list = search_##_name(table, key);                      \
49*1f5207b7SJohn Levon 	if (!list) {                                            \
50*1f5207b7SJohn Levon 		add_ptr_list(&list, value);                     \
51*1f5207b7SJohn Levon 	} else {                                                \
52*1f5207b7SJohn Levon 		remove_##_name(table, key);                     \
53*1f5207b7SJohn Levon 		add_ptr_list(&list, value);                     \
54*1f5207b7SJohn Levon 	}                                                       \
55*1f5207b7SJohn Levon 	insert_##_name(table, key, list);                       \
56*1f5207b7SJohn Levon }
57*1f5207b7SJohn Levon 
create_function_hashtable(int size)58*1f5207b7SJohn Levon static inline struct hashtable *create_function_hashtable(int size)
59*1f5207b7SJohn Levon {
60*1f5207b7SJohn Levon 	return create_hashtable(size, djb2_hash, equalkeys);
61*1f5207b7SJohn Levon }
62*1f5207b7SJohn Levon 
destroy_function_hashtable(struct hashtable * table)63*1f5207b7SJohn Levon static inline void destroy_function_hashtable(struct hashtable *table)
64*1f5207b7SJohn Levon {
65*1f5207b7SJohn Levon 	hashtable_destroy(table, 0);
66*1f5207b7SJohn Levon }
67*1f5207b7SJohn Levon 
68*1f5207b7SJohn Levon #define DEFINE_FUNCTION_HASHTABLE(_name, _item_type, _list_type)   \
69*1f5207b7SJohn Levon 	DEFINE_HASHTABLE_INSERT(insert_##_name, char, _list_type); \
70*1f5207b7SJohn Levon 	DEFINE_HASHTABLE_SEARCH(search_##_name, char, _list_type); \
71*1f5207b7SJohn Levon 	DEFINE_HASHTABLE_REMOVE(remove_##_name, char, _list_type); \
72*1f5207b7SJohn Levon 	DEFINE_FUNCTION_ADD_HOOK(_name, _item_type, _list_type);
73*1f5207b7SJohn Levon 
74*1f5207b7SJohn Levon #define DEFINE_FUNCTION_HASHTABLE_STATIC(_name, _item_type, _list_type)   \
75*1f5207b7SJohn Levon 	static DEFINE_HASHTABLE_INSERT(insert_##_name, char, _list_type); \
76*1f5207b7SJohn Levon 	static DEFINE_HASHTABLE_SEARCH(search_##_name, char, _list_type); \
77*1f5207b7SJohn Levon 	static DEFINE_HASHTABLE_REMOVE(remove_##_name, char, _list_type); \
78*1f5207b7SJohn Levon 	static DEFINE_FUNCTION_ADD_HOOK(_name, _item_type, _list_type);
79*1f5207b7SJohn Levon 
80*1f5207b7SJohn Levon #define DEFINE_STRING_HASHTABLE_STATIC(_name)   \
81*1f5207b7SJohn Levon 	static DEFINE_HASHTABLE_INSERT(insert_##_name, char, int); \
82*1f5207b7SJohn Levon 	static DEFINE_HASHTABLE_SEARCH(search_##_name, char, int); \
83*1f5207b7SJohn Levon 	static struct hashtable *_name
84*1f5207b7SJohn Levon 
load_hashtable_helper(const char * file,int (* insert_func)(struct hashtable *,char *,int *),struct hashtable * table)85*1f5207b7SJohn Levon static inline void load_hashtable_helper(const char *file, int (*insert_func)(struct hashtable *, char *, int *), struct hashtable *table)
86*1f5207b7SJohn Levon {
87*1f5207b7SJohn Levon 	char filename[256];
88*1f5207b7SJohn Levon 	struct token *token;
89*1f5207b7SJohn Levon 	char *name;
90*1f5207b7SJohn Levon 
91*1f5207b7SJohn Levon 	snprintf(filename, sizeof(filename), "%s.%s", option_project_str, file);
92*1f5207b7SJohn Levon 	token = get_tokens_file(filename);
93*1f5207b7SJohn Levon 	if (!token)
94*1f5207b7SJohn Levon 		return;
95*1f5207b7SJohn Levon 	if (token_type(token) != TOKEN_STREAMBEGIN)
96*1f5207b7SJohn Levon 		return;
97*1f5207b7SJohn Levon 	token = token->next;
98*1f5207b7SJohn Levon 	while (token_type(token) != TOKEN_STREAMEND) {
99*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_IDENT)
100*1f5207b7SJohn Levon 			return;
101*1f5207b7SJohn Levon 		name = alloc_string(show_ident(token->ident));
102*1f5207b7SJohn Levon 		insert_func(table, name, (void *)1);
103*1f5207b7SJohn Levon 		token = token->next;
104*1f5207b7SJohn Levon 	}
105*1f5207b7SJohn Levon 	clear_token_alloc();
106*1f5207b7SJohn Levon }
107*1f5207b7SJohn Levon 
108*1f5207b7SJohn Levon #define load_strings(file, _table) load_hashtable_helper(file, insert_##_table, _table)
109