|
|
1.1 root 1: #include <X/mit-copyright.h>
2:
3: /* $Header: XMakeAssoc.c,v 10.4 86/02/01 15:36:32 tony Rel $ */
4: /* Copyright Massachusetts Institute of Technology 1985 */
5:
6: #include "XlibInternal.h"
7:
8: /*
9: * XMakeAssoc - Insert data into an XAssocTable keyed on an XId.
10: * Data is inserted into the table only once. Redundant inserts are
11: * meaningless (but cause no problems). The queue in each association
12: * bucket is sorted (lowest XId to highest XId).
13: */
14: XMakeAssoc(table, x_id, data)
15: register XAssocTable *table;
16: register XId x_id;
17: register caddr_t data;
18: {
19: int hash;
20: register XAssoc *bucket;
21: register XAssoc *entry;
22: register XAssoc *new_entry;
23:
24: /* Hash the XId to get the bucket number. */
25: hash = x_id & (table->size - 1);
26: /* Look up the bucket to get the entries in that bucket. */
27: bucket = &(*table->table)[hash];
28: /* Get the first entry in the bucket. */
29: entry = bucket->next;
30:
31: /* If (entry != bucket), the bucket is empty so make */
32: /* the new entry the first entry in the bucket. */
33: /* if (entry == bucket), the we have to search the */
34: /* bucket. */
35: if (entry != bucket) {
36: /* The bucket isn't empty, begin searching. */
37: /* If we leave the for loop then we have either passed */
38: /* where the entry should be or hit the end of the bucket. */
39: /* In either case we should then insert the new entry */
40: /* before the current value of "entry". */
41: for (entry; entry != bucket; entry = entry->next) {
42: if (entry->x_id == x_id) {
43: /* Entry has the same XId... */
44: if (entry->display == _XlibCurrentDisplay) {
45: /* Entry has the same Display... */
46: /* Therefore there is already an */
47: /* entry with this XId and Display, */
48: /* reset its data value and return. */
49: entry->data = data;
50: return;
51: }
52: /* We found an association with the right */
53: /* id but the wrong display! */
54: continue;
55: }
56: /* If the current entry's XId is greater than the */
57: /* XId of the entry to be inserted then we have */
58: /* passed the location where the new XId should */
59: /* be inserted. */
60: if (entry->x_id > x_id) break;
61: }
62: }
63:
64: /* If we are here then the new entry should be inserted just */
65: /* before the current value of "entry". */
66: /* Create a new XAssoc and load it with new provided data. */
67: new_entry = (XAssoc *)malloc(sizeof(XAssoc));
68: if (new_entry == NULL) {
69: /* Malloc failed! */
70: errno = ENOMEM;
71: _XIOError(_XlibCurrentDisplay);
72: }
73: new_entry->display = _XlibCurrentDisplay;
74: new_entry->x_id = x_id;
75: new_entry->data = data;
76:
77: /* Insert the new entry. */
78: insque(new_entry, entry->prev);
79: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.