-
Notifications
You must be signed in to change notification settings - Fork 1
/
ref.c
110 lines (94 loc) · 2.78 KB
/
ref.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
/*
* dis6502 by Robert Bond, Udi Finkelstein, and Eric Smith
*
* $Id: ref.c 26 2004-01-17 23:28:23Z eric $
* Copyright 2001-2003 Eric Smith <eric@brouhaha.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
#include "dis.h"
#define HTSIZE 0x1000 /* Power of 2 */
#define HTMASK (HTSIZE-1)
struct hashslot {
addr_t addr; /* The key */
struct ref_chain *ref; /* Who references it */
char *name; /* The symbolic name (if it has one) */
};
struct hashslot hashtbl[HTSIZE]; /* the hash table */
static
struct hashslot *hash (addr_t loc, int allocate)
{
int probes;
struct hashslot *hp;
/*
* [phf] The hash function looks like a bad idea at first but since
* loc is from a relatively small range of integers, it's usually
* just fine. There is of course the possibility that we overflow
* the hash table, so maybe we should track load factor and resize?
* Barely 10% for the Apple 1 Basic ROM though...
*/
hp = &hashtbl[loc & HTMASK];
probes = 0;
while (probes < HTSIZE) {
if (hp->addr == loc)
return(hp);
if (hp->name == NULL && hp->ref == NULL) {
if (allocate) {
hp->addr = loc;
return(hp);
} else
return(NULL);
}
hp++;
if (hp == &hashtbl[HTSIZE])
hp = &hashtbl[0];
probes++;
}
crash("Hash table full");
/*NOTREACHED*/
}
void save_ref (addr_t refer, addr_t refee)
{
struct ref_chain *rc;
struct hashslot *hp;
rc = emalloc(sizeof(*rc));
rc->who = refer;
hp = hash(refee, 1);
rc->next = hp->ref;
hp->ref = rc;
}
void save_name (addr_t loc, char *name)
{
struct hashslot *hp;
hp = hash(loc, 1);
hp->name = name;
}
struct ref_chain *get_ref(addr_t loc)
{
struct hashslot *hp;
hp = hash(loc, 0);
if (!hp)
return(NULL);
return(hp->ref);
}
char * get_name(addr_t loc)
{
struct hashslot *hp;
hp = hash(loc, 0);
if (!hp)
return(NULL);
return(hp->name);
}