~case/dbns

ref: 85d3c74215baa48cb2fd97d3c85ff6060e7c42b1 dbns/src/hashstr.c -rw-r--r-- 6.1 KiB
85d3c742Zerhyn SBC oopsie 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/****************************************************************************
 * Advanced string hashing functions (c)1996 D.S.D. Software, written by    *
 * Derek Snider for use in SMAUG.					    *
 *									    *
 * These functions keep track of how many "links" are pointing to the	    *
 * memory allocated, and will free the memory if all the links are removed. *
 * Make absolutely sure you do not mix use of strdup and free with these    *
 * functions, or nasty stuff will happen!				    *
 * Most occurances of strdup/str_dup should be replaced with str_alloc, and *
 * any free/DISPOSE used on the same pointer should be replaced with	    *
 * str_free.  If a function uses strdup for temporary use... it is best if  *
 * it is left as is.  Just don't get usage mixed up between conventions.    *
 * The hashstr_data size is 8 bytes of overhead.  Don't be concerned about  *
 * this as you still save lots of space on duplicate strings.	-Thoric	    *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>


#define STR_HASH_SIZE	1024

struct hashstr_data {
	struct hashstr_data *next;	/* next hash element */
	unsigned short int links;	/* number of links to this string */
	unsigned short int length;	/* length of string */
};

char   *str_alloc(char *str);
char   *quick_link(char *str);
int 	str_free(char *str);
void 	show_hash(int count);
char   *hash_stats(void);

struct hashstr_data *string_hash[STR_HASH_SIZE];

/*
 * Check hash table for existing occurance of string.
 * If found, increase link count, and return pointer,
 * otherwise add new string to hash table, and return pointer.
 */
char   *
str_alloc(char *str)
{
	register int len, hash, psize;
	register struct hashstr_data *ptr;

	len = strlen(str);
	psize = sizeof(struct hashstr_data);
	hash = len % STR_HASH_SIZE;
	for (ptr = string_hash[hash]; ptr; ptr = ptr->next)
		if (len == ptr->length && !strcmp(str, (char *) ptr + psize)) {
			if (ptr->links < 65535)
				++ptr->links;
			return (char *) ptr + psize;
		}
	ptr = (struct hashstr_data *) malloc(len + psize + 1);
	ptr->links = 1;
	ptr->length = len;
	if (len)
		strcpy((char *) ptr + psize, str);
	else
		strcpy((char *) ptr + psize, "");
	ptr->next = string_hash[hash];
	string_hash[hash] = ptr;
	return (char *) ptr + psize;
}

/*
 * Used to make a quick copy of a string pointer that is known to be already
 * in the hash table.  Function increments the link count and returns the
 * same pointer passed.
 */
char   *
quick_link(char *str)
{
	register struct hashstr_data *ptr;

	ptr = (struct hashstr_data *) (str - sizeof(struct hashstr_data));
	if (ptr->links == 0) {
		fprintf(stderr, "quick_link: bad pointer\n");
		return NULL;
	}
	if (ptr->links < 65535)
		++ptr->links;
	return str;
}

/*
 * Used to remove a link to a string in the hash table.
 * If all existing links are removed, the string is removed from the
 * hash table and disposed of.
 * returns how many links are left, or -1 if an error occurred.
 */
int 
str_free(char *str)
{
	register int len, hash;
	register struct hashstr_data *ptr, *ptr2, *ptr2_next;

	len = strlen(str);
	hash = len % STR_HASH_SIZE;
	ptr = (struct hashstr_data *) (str - sizeof(struct hashstr_data));
	if (ptr->links == 65535)	/* permanent */
		return ptr->links;
	if (ptr->links == 0) {
		fprintf(stderr, "str_free: bad pointer\n");
		return -1;
	}
	if (--ptr->links == 0) {
		if (string_hash[hash] == ptr) {
			string_hash[hash] = ptr->next;
			free(ptr);
			return 0;
		}
		for (ptr2 = string_hash[hash]; ptr2; ptr2 = ptr2_next) {
			ptr2_next = ptr2->next;
			if (ptr2_next == ptr) {
				ptr2->next = ptr->next;
				free(ptr);
				return 0;
			}
		}
		fprintf(stderr, "str_free: pointer not found for string: %s\n", str);
		return -1;
	}
	return ptr->links;
}

void 
show_hash(int count)
{
	struct hashstr_data *ptr;
	int 	x , c;

	for (x = 0; x < count; x++) {
		for (c = 0, ptr = string_hash[x]; ptr; ptr = ptr->next, c++);
		fprintf(stderr, " %d", c);
	}
	fprintf(stderr, "\n");
}

void 
hash_dump(int hash)
{
	struct hashstr_data *ptr;
	int 	c , psize;

	if (hash > STR_HASH_SIZE || hash < 0) {
		fprintf(stderr, "hash_dump: invalid hash size\n\r");
		return;
	}
	psize = sizeof(struct hashstr_data);
	for (c = 0, ptr = string_hash[hash]; ptr; ptr = ptr->next, c++) {
		fprintf(stderr, "Len:%4d Lnks:%5d",
		    ptr->length, ptr->links);
	}
	fprintf(stderr, "Total strings in hash %d: %d\n\r", hash, c);
}

char   *
check_hash(char *str)
{
	static char buf[1024];
	int 	len, hash, psize, p = 0, c;
	struct hashstr_data *ptr, *fnd;

	buf[0] = '\0';
	len = strlen(str);
	psize = sizeof(struct hashstr_data);
	hash = len % STR_HASH_SIZE;
	for (fnd = NULL, ptr = string_hash[hash], c = 0; ptr; ptr = ptr->next, c++)
		if (len == ptr->length && !strcmp(str, (char *) ptr + psize)) {
			fnd = ptr;
			p = c + 1;
		}
	if (fnd)
		sprintf(buf, "Hash info on string: %s\n\rLinks: %d  Position: %d/%d  Hash: %d  Length: %d\n\r",
		    str, fnd->links, p, c, hash, fnd->length);
	else
		sprintf(buf, "%s not found.\n\r", str);
	return buf;
}

char   *
hash_stats(void)
{
	static char buf[1024];
	struct hashstr_data *ptr;
	int 	x , c, total, totlinks, unique, bytesused, wouldhave, hilink;

	totlinks = unique = total = bytesused = wouldhave = hilink = 0;
	for (x = 0; x < STR_HASH_SIZE; x++) {
		for (c = 0, ptr = string_hash[x]; ptr; ptr = ptr->next, c++) {
			total++;
			if (ptr->links == 1)
				unique++;
			if (ptr->links > hilink)
				hilink = ptr->links;
			totlinks += ptr->links;
			bytesused += (ptr->length + 1 + sizeof(struct hashstr_data));
			wouldhave += (ptr->links * (ptr->length + 1));
		}
	}
	sprintf(buf, "Hash strings allocated:%8d  Total links  : %d\n\rString bytes allocated:%8d  Bytes saved  : %d\n\rUnique (wasted) links :%8d  Hi-Link count: %d\n\r",
	    total, totlinks, bytesused, wouldhave - bytesused, unique, hilink);
	return buf;
}

void 
show_high_hash(int top)
{
	struct hashstr_data *ptr;
	int 	x , psize;

	psize = sizeof(struct hashstr_data);
	for (x = 0; x < STR_HASH_SIZE; x++)
		for (ptr = string_hash[x]; ptr; ptr = ptr->next)
			if (ptr->links >= top) {
			  fprintf(stderr, "Links: %5d  Size: >%d<\n\r", ptr->links, ptr->length);
			}
}