Blame view

security/selinux/ss/hashtab.c 2.94 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
  /*
   * Implementation of the hash table type.
   *
   * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
   */
  #include <linux/kernel.h>
  #include <linux/slab.h>
  #include <linux/errno.h>
  #include "hashtab.h"
bb2424974   Chad Sellers   SELinux: ensure k...
10
  struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),
719a2f8e5   Eric Paris   SELinux: hashtab....
11
12
  			       int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),
  			       u32 size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
  {
  	struct hashtab *p;
  	u32 i;
89d155ef6   James Morris   [PATCH] SELinux: ...
16
  	p = kzalloc(sizeof(*p), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
  	if (p == NULL)
  		return p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	p->size = size;
  	p->nel = 0;
  	p->hash_value = hash_value;
  	p->keycmp = keycmp;
  	p->htable = kmalloc(sizeof(*(p->htable)) * size, GFP_KERNEL);
  	if (p->htable == NULL) {
  		kfree(p);
  		return NULL;
  	}
  
  	for (i = 0; i < size; i++)
  		p->htable[i] = NULL;
  
  	return p;
  }
  
  int hashtab_insert(struct hashtab *h, void *key, void *datum)
  {
  	u32 hvalue;
  	struct hashtab_node *prev, *cur, *newnode;
  
  	if (!h || h->nel == HASHTAB_MAX_NODES)
  		return -EINVAL;
  
  	hvalue = h->hash_value(h, key);
  	prev = NULL;
  	cur = h->htable[hvalue];
  	while (cur && h->keycmp(h, key, cur->key) > 0) {
  		prev = cur;
  		cur = cur->next;
  	}
  
  	if (cur && (h->keycmp(h, key, cur->key) == 0))
  		return -EEXIST;
89d155ef6   James Morris   [PATCH] SELinux: ...
53
  	newnode = kzalloc(sizeof(*newnode), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
  	if (newnode == NULL)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
61
62
63
64
65
66
67
68
  	newnode->key = key;
  	newnode->datum = datum;
  	if (prev) {
  		newnode->next = prev->next;
  		prev->next = newnode;
  	} else {
  		newnode->next = h->htable[hvalue];
  		h->htable[hvalue] = newnode;
  	}
  
  	h->nel++;
  	return 0;
  }
bb2424974   Chad Sellers   SELinux: ensure k...
69
  void *hashtab_search(struct hashtab *h, const void *key)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  {
  	u32 hvalue;
  	struct hashtab_node *cur;
  
  	if (!h)
  		return NULL;
  
  	hvalue = h->hash_value(h, key);
  	cur = h->htable[hvalue];
  	while (cur != NULL && h->keycmp(h, key, cur->key) > 0)
  		cur = cur->next;
  
  	if (cur == NULL || (h->keycmp(h, key, cur->key) != 0))
  		return NULL;
  
  	return cur->datum;
  }
  
  void hashtab_destroy(struct hashtab *h)
  {
  	u32 i;
  	struct hashtab_node *cur, *temp;
  
  	if (!h)
  		return;
  
  	for (i = 0; i < h->size; i++) {
  		cur = h->htable[i];
  		while (cur != NULL) {
  			temp = cur;
  			cur = cur->next;
  			kfree(temp);
  		}
  		h->htable[i] = NULL;
  	}
  
  	kfree(h->htable);
  	h->htable = NULL;
  
  	kfree(h);
  }
  
  int hashtab_map(struct hashtab *h,
  		int (*apply)(void *k, void *d, void *args),
  		void *args)
  {
  	u32 i;
  	int ret;
  	struct hashtab_node *cur;
  
  	if (!h)
  		return 0;
  
  	for (i = 0; i < h->size; i++) {
  		cur = h->htable[i];
  		while (cur != NULL) {
  			ret = apply(cur->key, cur->datum, args);
  			if (ret)
  				return ret;
  			cur = cur->next;
  		}
  	}
  	return 0;
  }
  
  
  void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
  {
  	u32 i, chain_len, slots_used, max_chain_len;
  	struct hashtab_node *cur;
  
  	slots_used = 0;
  	max_chain_len = 0;
  	for (slots_used = max_chain_len = i = 0; i < h->size; i++) {
  		cur = h->htable[i];
  		if (cur) {
  			slots_used++;
  			chain_len = 0;
  			while (cur) {
  				chain_len++;
  				cur = cur->next;
  			}
  
  			if (chain_len > max_chain_len)
  				max_chain_len = chain_len;
  		}
  	}
  
  	info->slots_used = slots_used;
  	info->max_chain_len = max_chain_len;
  }