File: finger_tbl.c

package info (click to toggle)
xview 3.2p1.4-28.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 26,680 kB
  • ctags: 34,403
  • sloc: ansic: 241,397; yacc: 1,435; sh: 1,086; makefile: 148; lex: 76; perl: 54; asm: 50; cpp: 15
file content (407 lines) | stat: -rw-r--r-- 12,243 bytes parent folder | download | duplicates (6)
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
#ifndef lint
#ifdef sccs
static char     sccsid[] = "@(#)finger_tbl.c 20.21 93/06/28";
#endif
#endif

/*
 *	(c) Copyright 1989 Sun Microsystems, Inc. Sun design patents 
 *	pending in the U.S. and foreign countries. See LEGAL NOTICE 
 *	file for terms of the license.
 */

/*
 * Utilities for manipulation of finger tables.
 * 
 * See finger_tbl.h for descriptions of what the routines do.
 */

#include <xview/base.h>
#include <xview/pkg.h>
#include <xview/attrol.h>
#include <xview_private/primal.h>
#include <xview_private/finger_tbl.h>
#ifdef SVR4 
#include <stdlib.h> 
#endif /* SVR4 */

static void
ft_validate_first_infinity(finger_table)
    register ft_handle finger_table;
{
    register Es_index *seq_i_addr = 0;
    register int    addr_delta = finger_table->sizeof_element;
    register int    first_infinity = finger_table->first_infinity;
    int             save_last_bounding_index;

    if (first_infinity < finger_table->last_plus_one) {
	seq_i_addr = FT_ADDR(finger_table, first_infinity, addr_delta);
	if (*seq_i_addr == ES_INFINITY) {
	    while ((first_infinity > 0) &&
		   (seq_i_addr = FT_PREV_ADDR(seq_i_addr, addr_delta)) &&
		   (*seq_i_addr == ES_INFINITY)) {
		first_infinity--;
	    }
	} else if ((seq_i_addr = FT_NEXT_ADDR(seq_i_addr, addr_delta)) &&
		   ((*seq_i_addr) == ES_INFINITY)) {
	    first_infinity++;
	} else {
	    seq_i_addr = 0;
	}
    }
    if (seq_i_addr == 0) {
	save_last_bounding_index = finger_table->last_bounding_index;
	first_infinity =
	    ft_bounding_index(finger_table, ES_INFINITY - 1);
	if (first_infinity < finger_table->last_plus_one)
	    first_infinity++;
	finger_table->last_bounding_index = save_last_bounding_index;
    }
    finger_table->first_infinity = first_infinity;
}

Pkg_private void
ft_add_delta(finger_table, from, delta)
    ft_object       finger_table;
    int             from;
    long int        delta;
{
    register int    ft_index;
    register Es_index *seq_i_addr;
    register int    addr_delta = finger_table.sizeof_element;

    /*
     * ALERT: this routine assumes 'from' is < finger_table.last_plus_one:
     * calling procedures should check for this before calling this routine
     */

    seq_i_addr = FT_ADDR(&finger_table, from, addr_delta);
    if (*seq_i_addr != ES_INFINITY) {
	for (ft_index = from; ft_index < finger_table.last_plus_one;
	     ft_index++) {
	    if (*seq_i_addr == ES_INFINITY)
		break;
	    *seq_i_addr = *seq_i_addr + delta;
	    seq_i_addr = FT_NEXT_ADDR(seq_i_addr, addr_delta);
	}
    }
}

Pkg_private     ft_object
ft_create(last_plus_one, sizeof_client_data)
    int             last_plus_one, sizeof_client_data;
{
    ft_object       result;

    /*
     * Guarantee that result.sizeof_element meets all alignment restrictions
     * by adding trailing padding when necessary.
     */
    result.sizeof_element = sizeof(Es_index) + sizeof_client_data;
    while (result.sizeof_element % (sizeof(Es_index)))
	result.sizeof_element++;
    result.last_plus_one = last_plus_one;
    result.seq = (Es_index *) calloc(
		       (unsigned) last_plus_one + 1, result.sizeof_element);
    result.last_bounding_index = 0;
    result.first_infinity = 0;
    return (result);
}

Pkg_private void
ft_destroy(table)
    ft_handle       table;
{
    free((char *) table->seq);
    table->seq = 0;
    table->last_plus_one = 0;
}

Pkg_private void
ft_expand(table, by)
    register ft_handle table;
    int             by;
{
    int             old_last_plus_one = table->last_plus_one;

    /* this routine should not be called if 'by' is == 0 and */
    /* it is the responsibility of the calling procedure to check */
    table->last_plus_one += by;
    table->seq = (Es_index *) realloc((char *) table->seq, (unsigned) table->last_plus_one * (unsigned) table->sizeof_element);
    if (by > 0) {
	if (table->last_plus_one > old_last_plus_one)
	    ft_set(*table, old_last_plus_one,
		   table->last_plus_one, ES_INFINITY,
		   (char *) 0);
    }
}

Pkg_private void
ft_shift_up(table, first, last_plus_one, expand_by)
    register ft_handle table;
    int             first, last_plus_one, expand_by;
{
    register int    addr_delta = table->sizeof_element, shift_count,
			stop_plus_one;
    register Es_index *seq_i_addr;

    ft_validate_first_infinity(table);
    if (expand_by > 0) {
	stop_plus_one = table->last_plus_one - (last_plus_one - 1 - first);
	if (table->first_infinity >= stop_plus_one) {
	    if (expand_by != 0)
		ft_expand(table, expand_by);
	}
    }
    shift_count = table->first_infinity - first;
    shift_count = MIN(shift_count, table->last_plus_one - last_plus_one);
    if (shift_count > 0) {
	seq_i_addr = FT_ADDR(table, first, addr_delta);
	XV_BCOPY((char *) seq_i_addr,
	      ((char *) FT_ADDR(table, last_plus_one, addr_delta)),
	      addr_delta * (shift_count));
    }
    if (table->first_infinity < table->last_plus_one)
	table->first_infinity += (last_plus_one - first);
}

Pkg_private void
ft_shift_out(table, first, last_plus_one)
    ft_handle       table;
    int             first, last_plus_one;
{
    register int    addr_delta = table->sizeof_element, to_move;
    register char  *first_addr, *lpo_addr;

    ft_validate_first_infinity(table);
    if (last_plus_one < table->first_infinity) {
	to_move = table->first_infinity - last_plus_one;
	first_addr = ((char *) table->seq + first * addr_delta);
	lpo_addr = ((char *) table->seq + last_plus_one * addr_delta);
	XV_BCOPY(lpo_addr, first_addr, to_move * addr_delta);
    } else
	to_move = 0;
    if (table->last_plus_one > first + to_move)
	ft_set(*table, first + to_move, table->first_infinity,
	       ES_INFINITY, (char *) 0);
    table->first_infinity = first + to_move;
}

Pkg_private void
ft_set(finger_table, first, last_plus_one, to, client_data)
    ft_object       finger_table;
    int             first, last_plus_one;
    Es_index        to;
    char           *client_data;
{
    register int    ft_index;
    register Es_index *seq_i_addr;
    register int    addr_delta = finger_table.sizeof_element;

    /*
     * ALERT: the calling procedure should check to see if 'first' is <
     * 'finger_table.last_plus_one' before calling this prodedure
     */
    seq_i_addr = FT_ADDR(&finger_table, first, addr_delta);
    for (ft_index = first; ft_index < last_plus_one; ft_index++) {
	*seq_i_addr = to;
	if (client_data) {
	    XV_BCOPY(client_data, ((char *) seq_i_addr) + sizeof(Es_index),
		  addr_delta - sizeof(Es_index));
	}
	seq_i_addr = FT_NEXT_ADDR(seq_i_addr, addr_delta);
    }
}

Pkg_private void
ft_set_esi_span(finger_table, first, last_plus_one, to, client_data)
    ft_object       finger_table;
    Es_index        first, last_plus_one, to;
    char           *client_data;
{
    register int    index_of_first = 0, index_of_last_plus_one;
    register Es_index *seq_i_addr = finger_table.seq;
    register int    addr_delta = finger_table.sizeof_element;

    if AN_ERROR
	(finger_table.last_plus_one == 0) {
	return;
	}
    while (first > *seq_i_addr) {
	if (++index_of_first == finger_table.last_plus_one)
	    return;
	seq_i_addr = FT_NEXT_ADDR(seq_i_addr, addr_delta);
    }
    index_of_last_plus_one = index_of_first;
    while (last_plus_one > *seq_i_addr) {
	if (++index_of_last_plus_one == finger_table.last_plus_one)
	    break;
	seq_i_addr = FT_NEXT_ADDR(seq_i_addr, addr_delta);
    }
    if (finger_table.last_plus_one > index_of_first)
	ft_set(finger_table, index_of_first,
	       index_of_last_plus_one, to, client_data);
}

Pkg_private int
ft_bounding_index(finger_table, pos)
    register Ft_table finger_table;
    Es_index        pos;
/*
 * The 3.0 version of this code used linear search with no caching, but a
 * table of pieces can get to be 100-1000 elements long, and then linear
 * search is way too slow.
 */
{
    register Es_index *seq_i_addr = finger_table->seq;
    register int    addr_delta = finger_table->sizeof_element;
    register int    index, start, stop_plus_one;

    stop_plus_one = finger_table->last_plus_one;
    if (pos < *seq_i_addr || stop_plus_one == 0) {
	index = stop_plus_one;
	finger_table->last_bounding_index = index;
	return (index);
    }
    /* Assert: seq[0] <= pos && 0 < stop_plus_one */
    /* Check the cache */
    index = finger_table->last_bounding_index;
    if (index < stop_plus_one) {
	seq_i_addr = FT_ADDR(finger_table, index, addr_delta);
	if (*seq_i_addr <= pos) {
	    if (index + 1 == stop_plus_one) {
		finger_table->last_bounding_index = index;
		return (index);
	    }
	    seq_i_addr = FT_NEXT_ADDR(seq_i_addr, addr_delta);
	    if (pos < *seq_i_addr) {
		finger_table->last_bounding_index = index;
		return (index);
	    }
	}
    }
    /* No luck, so do the search */
    index = stop_plus_one - 1;
    seq_i_addr = FT_ADDR(finger_table, index, addr_delta);
    if (*seq_i_addr <= pos) {
	finger_table->last_bounding_index = index;
	return (index);
    }
    /*
     * Assert: pos < seq[stop_plus_one-1] && 1 < stop_plus_one (else would
     * have goto'd above)
     */
    start = 0;
    /* Assert: seq[start] <= pos && start+1 == 1 < stop_plus_one */
    FOREVER {
	index = (start + stop_plus_one) / 2;
	/* Assert: start+1 <= index <= stop_plus_one-1 */
	seq_i_addr = FT_ADDR(finger_table, index, addr_delta);
	if (pos < *seq_i_addr) {
	    if (index + 1 == stop_plus_one) {
		/*
		 * Assert: start+1 == index (else contradiction), so
		 * seq[start] <= pos < seq[index], and we are done.
		 */
		index = start;
		finger_table->last_bounding_index = index;
		return (index);
	    }
	    stop_plus_one = index + 1;
	    /* Assert: start+1 < index+1 == (new)stop_plus_one */
	} else {
	    start = index;
	    /*
	     * Assert: (new)start == index < stop_plus_one-1, else seq[index
	     * == stop_plus_one-1] > pos, a contradiction
	     */
	}
	/* Assert: seq[start] <= pos < seq[stop_plus_one-1] */
    }
}

Pkg_private int
ft_index_for_position(finger_table, pos)
    ft_object       finger_table;
    Es_index        pos;
{
    register int    ft_index;
    register Es_index *seq_i_addr = finger_table.seq;
    register int    addr_delta = finger_table.sizeof_element;

    for (ft_index = 0; ft_index < finger_table.last_plus_one; ft_index++) {
	if (*seq_i_addr == pos)
	    return (ft_index);
	if (*seq_i_addr > pos)
	    break;
	seq_i_addr = FT_NEXT_ADDR(seq_i_addr, addr_delta);
    }
    return (finger_table.last_plus_one);
}

Pkg_private     Es_index
ft_position_for_index(finger_table, index)
    ft_object       finger_table;
    int             index;
{
    register Es_index *seq_i_addr;
    register int    addr_delta = finger_table.sizeof_element;

    if (index >= finger_table.last_plus_one)
	return (ES_CANNOT_SET);
    seq_i_addr = FT_ADDR(&finger_table, index, addr_delta);
    return (*seq_i_addr);
}

#ifdef DEBUG
Pkg_private	int
fprintf_ft(finger_table)
    ft_object       finger_table;
{
    register Es_index *seq_i_addr = finger_table.seq;
    register int    addr_delta = finger_table.sizeof_element;
    register int    i, cd_i;
    register int   *client_data_ptr;
    FILE           *out_file = stderr;

    if (finger_table.last_plus_one > 999) {
	(void) fprintf(out_file,
		       "You passed the ft_handle, not the ft_object!\n");
	return (FALSE);
    }
    (void) fprintf(out_file,
		   "last_plus_one = %d, sizeof_element = %d, seq = 0x%lx\n",
		   finger_table.last_plus_one,
		   finger_table.sizeof_element,
		   finger_table.seq
	);
    (void) fprintf(out_file, "seq[] pos  client_data\n");
    for (i = 0; i < finger_table.last_plus_one; i++) {
	(void) fprintf(out_file, "%2d  ", i);
	switch (*seq_i_addr) {
	  case ES_INFINITY:
	    (void) fprintf(out_file, "  INF  ");
	    break;
	  case ES_CANNOT_SET:
	    (void) fprintf(out_file, " ~SET  ");
	    break;
	  default:
	    if (*seq_i_addr < 100000)
		(void) fprintf(out_file, "%5d  ", *seq_i_addr);
	    else
		(void) fprintf(out_file, "%d  ", *seq_i_addr);
	    break;
	}
	for (cd_i = 1; cd_i < (addr_delta / (sizeof(*client_data_ptr)));
	     cd_i++) {
	    client_data_ptr = ((int *) seq_i_addr);
	    client_data_ptr += cd_i;
	    (void) fprintf(out_file, "%8X  ", *client_data_ptr);
	}
	(void) fprintf(out_file, "\n");
	seq_i_addr += (addr_delta / sizeof(*seq_i_addr));
    }
    return (TRUE);
}

#endif