File: shstr.c

package info (click to toggle)
crossfire 1.11.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 24,456 kB
  • ctags: 7,800
  • sloc: ansic: 80,483; sh: 11,825; perl: 2,327; lex: 1,946; makefile: 1,149
file content (414 lines) | stat: -rw-r--r-- 9,346 bytes parent folder | download
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * static char *rcsid_shstr_c =
 *   "$Id: shstr.c 4961 2006-09-21 05:14:18Z mwedel $";
 *
 * shstr.c
 *
 * This is a simple shared strings package with a simple interface.
 *
 *     char *find_string(const char *str)
 *     char *add_string(const char *str)
 *	char *add_refcount(char *str)
 *	void free_string(char *str)
 *	char *ss_dump_table(int what)
 *	void ss_dump_statistics()
 *
 * Author: Kjetil T. Homme, Oslo 1992.
 */

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

#if defined (__sun__) && defined (StupidSunHeaders)
#include <sys/time.h>
#include "sunos.h"
#endif

#include "shstr.h"

#ifndef WIN32
#include <autoconf.h>
#endif
#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
#endif

static shared_string *hash_table[TABLESIZE];

/*
 * Initialises the hash-table used by the shared string library.
 */

void
init_hash_table(void) {
    /* A static object should be zeroed out always */
#if !defined(__STDC__)
    (void) memset((void *)hash_table, 0, TABLESIZE * sizeof(shared_string *));
#endif
}

/*
 * Hashing-function used by the shared string library.
 */

static int
hashstr(const char *str) {
    unsigned long hash = 0;
    int i = 0;
    unsigned rot = 0;
    const char *p;

    GATHER(hash_stats.calls);

    for (p = str; i < MAXSTRING && *p; p++, i++) {
	hash ^= (unsigned long) *p << rot;
	rot += 2;
	if (rot >= (sizeof(long) - sizeof(char)) * CHAR_BIT)
	    rot = 0;
    }
    return (hash % TABLESIZE);
}

/*
 * Allocates and initialises a new shared_string structure, containing
 * the string str.
 */

static shared_string *
new_shared_string(const char *str) {
    shared_string *ss;

    /* Allocate room for a struct which can hold str. Note
     * that some bytes for the string are already allocated in the 
     * shared_string struct.
     */
    ss = (shared_string *) malloc(sizeof(shared_string) - PADDING +
				  strlen(str) + 1);
    ss->u.previous = NULL;
    ss->next = NULL;
    ss->refcount = 1;
    strcpy(ss->string, str);

    return ss;
}

/*
 * Description:
 *      This will add 'str' to the hash table. If there's no entry for this
 *      string, a copy will be allocated, and a pointer to that is returned.
 * Return values:
 *      - pointer to string identical to str
 */

const char *
add_string(const char *str) {
    shared_string *ss;
    int ind;

    GATHER(add_stats.calls);

    /* Should really core dump here, since functions should not be calling
     * add_string with a null parameter.  But this will prevent a few
     * core dumps.
     */
    if (str==NULL) {
#ifdef MANY_CORES
	abort();
#else
	return NULL;
#endif
    }

    ind = hashstr(str);
    ss = hash_table[ind];

    /* Is there an entry for that hash?
     */
    if (ss) {
	/* Simple case first: See if the first pointer matches.
	 */
	if (str != ss->string) {
	    GATHER(add_stats.strcmps);
	    if (strcmp(ss->string, str)) {
		/* Apparantly, a string with the same hash value has this 
		 * slot. We must see in the list if "str" has been 
		 * registered earlier.
		 */
		while (ss->next) {
		    GATHER(add_stats.search);
		    ss = ss->next;
		    if (ss->string != str) {
			GATHER(add_stats.strcmps);
			if (strcmp(ss->string, str)) {
			    /* This wasn't the right string...
			     */
			    continue;
			}
		    }
		    /* We found an entry for this string. Fix the
		     * refcount and exit.
		     */
		    GATHER(add_stats.linked);
		    ++(ss->refcount);

		    return ss->string;
		}
		/* There are no occurences of this string in the hash table.
		 */
		{
		    shared_string *new_ss;

		    GATHER(add_stats.linked);
		    new_ss = new_shared_string(str);
		    ss->next = new_ss;
		    new_ss->u.previous = ss;
		    return new_ss->string;
		}
	    }
	    /* Fall through.
	     */
	}
	GATHER(add_stats.hashed);
	++(ss->refcount);
	return ss->string;
    } else {
	/* The string isn't registered, and the slot is empty.
	 */
	GATHER(add_stats.hashed);
	hash_table[ind] = new_shared_string(str);

	/* One bit in refcount is used to keep track of the union.
	 */
	hash_table[ind]->refcount |= TOPBIT;
	hash_table[ind]->u.array = &(hash_table[ind]);

	return hash_table[ind]->string;
    }
}

/*
 * Description:
 *      This will increase the refcount of the string str, which *must*
 *      have been returned from a previous add_string().
 * Return values:
 *      - str
 */

const char *
add_refcount(const char *str) {
    GATHER(add_ref_stats.calls);
    ++(SS(str)->refcount);
    return str;
}

/*
 * Description:
 *      This will return the refcount of the string str, which *must*
 *      have been returned from a previous add_string().
 * Return values:
 *      - length
 */

int
query_refcount(const char *str) {
    return (SS(str)->refcount) & ~TOPBIT;
}

/*
 * Description:
 *      This will see if str is in the hash table, and return the address
 *      of that string if it exists.
 * Return values:
 *      - pointer to identical string or NULL
 */

const char *
find_string(const char *str) {
    shared_string *ss;
    int ind;

    GATHER(find_stats.calls);

    ind = hashstr(str);
    ss = hash_table[ind];

    /* Is there an entry for that hash?
     */
    if (ss) {
	/* Simple case first: Is the first string the right one?
	 */
	GATHER(find_stats.strcmps);
	if (!strcmp(ss->string, str)) {
	    GATHER(find_stats.hashed);
	    return ss->string;
	} else {
	    /* Recurse through the linked list, if there's one.
	     */
	    while (ss->next) {
		GATHER(find_stats.search);
		GATHER(find_stats.strcmps);
		ss = ss->next;
		if (!strcmp(ss->string, str)) {
		    GATHER(find_stats.linked);
		    return ss->string;
		}
	    }
	    /* No match. Fall through.
	     */
	}
    }
    return NULL;
}

/*
 * Description:
 *     This will reduce the refcount, and if it has reached 0, str will
 *     be freed.
 * Return values:
 *     None
 */

void
free_string(const char *str) {
    shared_string *ss;

    GATHER(free_stats.calls);

    ss = SS(str);

    if ((--ss->refcount & ~TOPBIT) == 0) {
	/* Remove this entry.
	 */
	if (ss->refcount & TOPBIT) {
	    /* We must put a new value into the hash_table[].
	     */
	    if (ss->next) {
		*(ss->u.array) = ss->next;
		ss->next->u.array = ss->u.array;
		ss->next->refcount |= TOPBIT;
	    } else {
		*(ss->u.array) = NULL;
	    }
	    free(ss);
	} else {
	    /* Relink and free this struct.
	     */
	    if (ss->next)
		ss->next->u.previous = ss->u.previous;
	    ss->u.previous->next = ss->next;
	    free(ss);
	}
    }
}

#ifdef SS_STATISTICS

extern char errmsg[];

/*
 * Description:
 *      The routines will gather statistics if SS_STATISTICS is defined.
 *      A call to this function will cause the statistics to be dumped
 *      into an external string errmsg, which must be large.
 * Return values:
 *      None
 */

void
ss_dump_statistics(void) {
    static char line[80];

    sprintf(errmsg, "%-13s %6s %6s %6s %6s %6s\n", 
	    "", "calls", "hashed", "strcmp", "search", "linked");
    sprintf(line, "%-13s %6d %6d %6d %6d %6d\n", 
	    "add_string:", add_stats.calls, add_stats.hashed, 
	    add_stats.strcmps, add_stats.search, add_stats.linked);
    strcat(errmsg, line);
    sprintf(line, "%-13s %6d\n",
	    "add_refcount:", add_ref_stats.calls);
    strcat(errmsg, line);
    sprintf(line, "%-13s %6d\n",
	    "free_string:", free_stats.calls);
    strcat(errmsg, line);
    sprintf(line, "%-13s %6d %6d %6d %6d %6d\n", 
	    "find_string:", find_stats.calls, find_stats.hashed, 
	    find_stats.strcmps, find_stats.search, find_stats.linked);
    strcat(errmsg, line);
    sprintf(line, "%-13s %6d\n",
	    "hashstr:", hash_stats.calls);
    strcat(errmsg, line);
}
#endif /* SS_STATISTICS */

/*
 * Description:
 *      If (what & SS_DUMP_TABLE) dump the contents of the hash table to
 *      stderr. If (what & SS_DUMP_TOTALS) return a string which
 *      says how many entries etc. there are in the table.
 * Return values:
 *      - a string or NULL
 */

const char *
ss_dump_table(int what) {
    static char totals[80];
    int entries = 0, refs = 0, links = 0;
    int i;

    for (i = 0; i < TABLESIZE; i++) {
	shared_string *ss;
	
	if ((ss = hash_table[i])!=NULL) {
	    ++entries;
	    refs += (ss->refcount & ~TOPBIT);
#if 1 /* Can't use stderr any longer, need to include global.h and
	    if (what & SS_DUMP_TABLE)
       * use logfile. */
		fprintf(stderr, "%4d -- %4d refs '%s' %c\n",
			i, (ss->refcount & ~TOPBIT), ss->string,
			(ss->refcount & TOPBIT ? ' ' : '#'));
#endif
	    while (ss->next) {
		ss = ss->next;
		++links;
		refs += (ss->refcount & ~TOPBIT);
#if 1
		if (what & SS_DUMP_TABLE)
		    fprintf(stderr, "     -- %4d refs '%s' %c\n",
			    (ss->refcount & ~TOPBIT), ss->string,
			    (ss->refcount & TOPBIT ? '*' : ' '));
#endif
	    }
	}
    }

    if (what & SS_DUMP_TOTALS) {
	sprintf(totals, "\n%d entries, %d refs, %d links.",
		entries, refs, links);
        return totals;
    }
    return NULL;
}

/* buf_overflow() - we don't want to exceed the buffer size of 
 * buf1 by adding on buf2! Returns true if overflow will occur.
 */

int 
buf_overflow (const char *buf1, const char *buf2, int bufsize)
{
    int     len1 = 0, len2 = 0;

    if (buf1)
	len1 = strlen (buf1);
    if (buf2)
	len2 = strlen (buf2);
    if ((len1 + len2) >= bufsize)
	return 1;
    return 0;
}