vsta

Check-in [0d6d3e328e]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add symbol tabler
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:0d6d3e328e43c3cfeaa5958f380bad91fd43eac6f2144dc30d297f93f0aa8ba3
User & Date: vandys 2002-01-21 08:49:45
Context
2002-01-25
10:53
Add a teensy stty(1) emulation. check-in: b5b8acd181 user: vandys tags: master, trunk
2002-01-21
08:49
Add symbol tabler check-in: 0d6d3e328e user: vandys tags: master, trunk
08:38
More stuff :-> check-in: fe98dc4f28 user: vandys tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added vsta/src/include/symbol.h.

























>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * symbol.h
 *	Public API for symbol table service
 */
#ifndef _SYMBOL_H
#define _SYMBOL_H

extern struct symbol *sym_alloc(void);
extern const char *sym_lookup(struct symbol *, const char *);
extern void sym_dealloc(struct symbol *);

#endif /* _SYMBOL_H */

Changes to vsta/src/lib/makefile.

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Special version for boot servers
startsrv.o: start.c
	$(CC) $(CFLAGS) -DSRV -o startsrv.o -c start.c

USROBJS= llist.o hash.o permsup.o permpr.o statsup.o \
	files.o rmap.o passwd.o ids.o assert.o mem.o \
	sema.o lock.o abc.o startsrv.o selfs.o complete.o \
	mcount.o

libusr.a: $(USROBJS)
	rm -f libusr.a
	ar rs libusr.a $(USROBJS)

TERMOBJS=termcap.o tgoto.o tputs.o
libtermcap.a: mkshlib libtermcap.tmp term/libtermcap.db







|







80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Special version for boot servers
startsrv.o: start.c
	$(CC) $(CFLAGS) -DSRV -o startsrv.o -c start.c

USROBJS= llist.o hash.o permsup.o permpr.o statsup.o \
	files.o rmap.o passwd.o ids.o assert.o mem.o \
	sema.o lock.o abc.o startsrv.o selfs.o complete.o \
	mcount.o symbol.o

libusr.a: $(USROBJS)
	rm -f libusr.a
	ar rs libusr.a $(USROBJS)

TERMOBJS=termcap.o tgoto.o tputs.o
libtermcap.a: mkshlib libtermcap.tmp term/libtermcap.db

Added vsta/src/lib/symbol.c.































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * symbol.c
 *	Maintain table of symbols
 *
 * A C string pointer is not guaranteed to be unique; a pointer to
 * the characters "foo" in one place may not be the same pointer as
 * one pointing to "foo" somewhere else.  This module provides
 * registry of strings, guaranteeing that any unique string will have
 * a globally unique and consistent value.
 */
#include <sys/param.h>
#include <std.h>
#include <alloc.h>
#include <string.h>
#include <symbol.h>

/*
 * Our private symbol table format
 */
struct symbol {
	int sym_size;
	const char **sym_strings;
};

/*
 * Initial size.  From here we double our size each time until we
 * reach our max increment size.
 */
#define INIT_SIZE (32)
#define MAX_INCREMENT (512)

/*
 * sym_alloc()
 *	Allocate a new symbol table
 */
struct symbol *
sym_alloc(void)
{
	struct symbol *sp;

	sp = calloc(1, sizeof(struct symbol));
	if (sp == NULL) {
		return(NULL);
	}
	sp->sym_size = INIT_SIZE;
	sp->sym_strings = calloc(INIT_SIZE, sizeof(char *));
	if (sp->sym_strings == NULL) {
		free(sp);
		return(NULL);
	}
	return(sp);
}

/*
 * hashval()
 *	Generate a hashval for a string
 */
static unsigned int
hashval(unsigned const char *ptr)
{
	unsigned int h = 0, idx = 0;
	unsigned char c;

	while ((c = *ptr++)) {
		h ^= (c << (idx++ * NBBY));
		if (idx >= sizeof(h)) {
			idx = 0;
		}
	}
	return(h);
}

/*
 * _sym_resinsert()
 *	Insert an already-allocated string into a symbol table
 */
static void
_sym_reinsert(struct symbol *sp, const char *ptr)
{
	int hash = hashval((unsigned const char *)ptr);
	int start, x;

	start = x = hash % sp->sym_size;
	do {
		/*
		 * When find an open slot, this is where we'll put
		 * this new string.
		 */
		if (sp->sym_strings[x] == NULL) {
			sp->sym_strings[x] = ptr;
			return;
		}

		/*
		 * Advance to next slot
		 */
		if (++x >= sp->sym_size) {
			x = 0;
		}
	} while (x != start);

	/*
	 * Shouldn't happen
	 */
	abort();
}

/*
 * sym_lookup()
 *	Turn string into unique pointer
 */
const char *
sym_lookup(struct symbol *sp, const char *ptr)
{
	int hash = hashval((unsigned const char *)ptr);
	int start, x;
	const char *p;
	struct symbol *sp2;

	/*
	 * Scan starting at this hash point for our string
	 */
	start = x = hash % sp->sym_size;
	do {
		/*
		 * If find an open slot, this is where we'll put
		 * this new string.
		 */
		p = sp->sym_strings[x];
		if (!p) {
			return(sp->sym_strings[x] = strdup(ptr));
		}

		/*
		 * See if the entry matches
		 */
		if (!strcmp(p, ptr)) {
			return(p);
		}

		/*
		 * Advance to next slot
		 */
		if (++x >= sp->sym_size) {
			x = 0;
		}
	} while (x != start);

	/*
	 * We are full; resize to next growth increment and
	 * re-insert contents.
	 */
	sp2 = calloc(1, sizeof(struct symbol));
	if (!sp2) {
		return(NULL);
	}

	/*
	 * Choose growth increment
	 */
	if (sp->sym_size*2 > MAX_INCREMENT) {
		x = sp->sym_size + MAX_INCREMENT;
	} else {
		x = sp->sym_size * 2;
	}

	/*
	 * Allocate hash array
	 */
	sp2->sym_strings = calloc(x, sizeof(char *));
	if (sp2->sym_strings == NULL) {
		free(sp2);
		return(NULL);
	}
	sp2->sym_size = x;

	/*
	 * Insert all strings, then switch over to new structure.
	 */
	for (x = 0; x < sp->sym_size; ++x) {
		_sym_reinsert(sp2, sp->sym_strings[x]);
	}
	free(sp->sym_strings);
	*sp = *sp2;
	free(sp2);

	/*
	 * Now recurse to try and fit our requested string in again.
	 */
	return(sym_lookup(sp, ptr));
}