aboutsummaryrefslogtreecommitdiff
path: root/unix/boot/mkpkg/fncache.c
blob: 2053f2feb950e1feb2d8d93143fe4e0e50be28dd (plain) (blame)
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
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//#include "../bootProto.h"


/*
 * FNCACHE -- Maintain a cache of system logical filenames (e.g., <config.h>)
 * and their associated virtual filenames (e.g., "host$hlib/config.h").
 * This can greatly reduce the amount of time required to resolve references
 * to system include files in dependency file lists.
 *
 * External entry points:
 *
 *   nc = m_sysfile (lname, fname, maxch)	# return file name
 *	   m_fninit (debug)			# initialize cache
 */

#define	MAX_FILES	20		/* size of the cache		*/
#define	SZ_LNAME	32		/* size of logical name		*/
#define	SZ_FNAME	32		/* size of virtual file name	*/
#define	EOS		'\0'

struct _sysfile {			/* cache list element structure	*/
	struct	_sysfile *uplnk;
	struct	_sysfile *dnlnk;
	int	nrefs;			/* number of references		*/
	int	chksum;			/* speeds searches		*/
	char	lname[SZ_LNAME+1];	/* logical name			*/
	char	fname[SZ_FNAME+1];	/* file name			*/
};

struct	_sysfile fncache[MAX_FILES];	/* the cache			*/
struct	_sysfile *fn_head;		/* doubly linked list		*/
struct	_sysfile *fn_tail;
int	fn_hits, fn_misses;

struct	_sysfile *fn_unlink();
struct	_sysfile *fn_tohead();
struct	_sysfile *fn_totail();


extern  int  os_sysfile (char *sysfile, char *fname, int maxch);

int   m_sysfile (char *lname, char *fname, int maxch);
void  m_fninit (int debug);
int   fn_chksum (char *s);
int   fn_strncpy (char *out, char *in, int maxch);



/* M_SYSFILE -- Search for the named system file and return the virtual file
 * name in the output string if the system file is found.  This is functionally
 * equivalent to os_sysfile().
 */
int
m_sysfile (
  char	*lname,			/* logical name of system file	*/
  char	*fname,			/* receives virtual file name	*/
  int	maxch 
)
{
	register struct _sysfile *fn;
	register int	chksum;
	int	fnlen;

	/* Look in the cache first.  For a small cache a linear search is
	 * plenty fast enough.
	 */
	chksum = fn_chksum (lname);
	for (fn=fn_head;  fn != NULL;  fn=fn->dnlnk)
	    if (fn->chksum == chksum && strcmp (lname, fn->lname) == 0) {
		fn_tohead (fn_unlink (fn));
		fn->nrefs++;
		fn_hits++;
		return (fn_strncpy (fname, fn->fname, maxch));
	    }

	/* Cache miss.  Don't put in cache it name is too long.
	 */
	fn_misses++;
	fnlen = os_sysfile (lname, fname, maxch);
	if (fnlen > SZ_FNAME || strlen(lname) > SZ_LNAME)
	    return (fnlen);

	/* Put fname in the cache.  Reuse slot at tail of list.
	 */
	fn = fn_tohead (fn_unlink (fn_tail));
	strcpy (fn->lname, lname);
	strcpy (fn->fname, fname);
	fn->chksum = fn_chksum (lname);
	fn->nrefs  = 1;

	return (fnlen);
}


/* M_FNINIT -- Initialize (clear) the sysfile cache.
 */
void
m_fninit (int debug)
{
	register struct _sysfile *fn;
	register int	i;
	int	total;

	if (debug) {
	    char    lname[SZ_FNAME+1];

	    total = fn_hits + fn_misses;
	    printf ("file name cache: %d hits, %d misses, %d%% of %d\n",
		fn_hits, fn_misses, (total ? fn_hits * 100 / total : 0), total);

	    for (fn=fn_head;  fn != NULL;  fn=fn->dnlnk)
		if (fn->lname[0]) {
		    sprintf (lname, "<%s>", fn->lname);
		    printf ("%3d (%05d) %-20s => %s\n",
			fn->nrefs, fn->chksum, lname, fn->fname);
		}

	    fn_hits   = 0;
	    fn_misses = 0;

	    fflush (stdout);
	}

	fn = fn_head = fn_tail = &fncache[0];
	fn->uplnk   = NULL;
	fn->dnlnk   = NULL;
	fn->nrefs   = 0;
	fn->chksum  = -1;
	fn->lname[0] = EOS;

	for (i=1;  i < MAX_FILES;  i++) {
	    fn = fn_tohead (&fncache[i]);
	    fn->lname[0] = EOS;
	    fn->chksum   = -1;
	    fn->nrefs    = 0;
	}
}


/* FN_TOHEAD -- Link a sysfile struct at the head of the list.
 */
struct _sysfile *
fn_tohead (register struct _sysfile *fn)
{
	if (fn != fn_head) {
	    fn->uplnk = NULL;
	    fn->dnlnk = fn_head;
	    fn_head->uplnk = fn;
	    fn_head = fn;
	}

	return (fn);
}


/* FN_TOTAIL -- Link a sysfile struct at the tail of the list.
 */
struct _sysfile *
fn_totail (register struct _sysfile *fn)
{
	if (fn != fn_tail) {
	    fn->uplnk = fn_tail;
	    fn->dnlnk = NULL;
	    fn_tail->dnlnk = fn;
	    fn_tail = fn;
	}

	return (fn);
}


/* FN_UNLINK -- Unlink an sysfile struct.
 */
struct _sysfile *
fn_unlink (register struct _sysfile *fn)
{
	if (fn == fn_head)
	    fn_head = fn->dnlnk;
	if (fn == fn_tail)
	    fn_tail = fn->uplnk;

	if (fn->uplnk)
	    fn->uplnk->dnlnk = fn->dnlnk;
	if (fn->dnlnk)
	    fn->dnlnk->uplnk = fn->uplnk;
	
	return (fn);
}


/* FN_CHKSUM -- Compute the checksum of a character string.
 */
int
fn_chksum (char *s)
{
	register int sum=0;

	while (*s)
	    sum += *s++;

	return (sum);
}


/* FN_STRNCPY -- Copy up to maxch characters from a string and return the
 * number of characters copied as the function value.
 */
int
fn_strncpy (
  char	*out,
  char	*in,
  int	maxch 
)
{
	register char	*ip, *op;
	register int	n;

	for (ip=in, op=out, n=maxch;  --n >= 0 && (*op++ = *ip++);  )
	    ;
	return (op-1 - out);
}