Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* bag.c -- ARMulator support code: ARM6 Instruction Emulator. |
2 | Copyright (C) 1994 Advanced RISC Machines Ltd. | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
380d9419 | 16 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
c906108c SS |
17 | |
18 | /********************************************************************/ | |
19 | /* bag.c: */ | |
20 | /* Offers a data structure for storing and getting pairs of number. */ | |
21 | /* The numbers are stored together, put one can be looked up by */ | |
22 | /* quoting the other. If a new pair is entered and one of the */ | |
23 | /* numbers is a repeat of a previous pair, then the previos pair */ | |
24 | /* is deleted. */ | |
25 | /********************************************************************/ | |
26 | ||
27 | #include "bag.h" | |
6d358e86 | 28 | #include <stdlib.h> |
c906108c SS |
29 | |
30 | #define HASH_TABLE_SIZE 256 | |
31 | #define hash(x) (((x)&0xff)^(((x)>>8)&0xff)^(((x)>>16)&0xff)^(((x)>>24)&0xff)) | |
32 | ||
dfcd3bfb JM |
33 | typedef struct hashentry |
34 | { | |
c906108c SS |
35 | struct hashentry *next; |
36 | int first; | |
37 | int second; | |
dfcd3bfb JM |
38 | } |
39 | Hashentry; | |
c906108c SS |
40 | |
41 | Hashentry *lookupbyfirst[HASH_TABLE_SIZE]; | |
42 | Hashentry *lookupbysecond[HASH_TABLE_SIZE]; | |
43 | ||
dfcd3bfb JM |
44 | void |
45 | addtolist (Hashentry ** add, long first, long second) | |
46 | { | |
47 | while (*add) | |
48 | add = &((*add)->next); | |
c906108c | 49 | /* Malloc will never fail? :o( */ |
dfcd3bfb | 50 | (*add) = (Hashentry *) malloc (sizeof (Hashentry)); |
c906108c SS |
51 | (*add)->next = (Hashentry *) 0; |
52 | (*add)->first = first; | |
53 | (*add)->second = second; | |
54 | } | |
55 | ||
dfcd3bfb JM |
56 | void |
57 | killwholelist (Hashentry * p) | |
58 | { | |
c906108c SS |
59 | Hashentry *q; |
60 | ||
dfcd3bfb JM |
61 | while (p) |
62 | { | |
63 | q = p; | |
64 | p = p->next; | |
65 | free (q); | |
66 | } | |
c906108c SS |
67 | } |
68 | ||
6d358e86 NC |
69 | static void |
70 | removefromlist (Hashentry ** p, long first) | |
dfcd3bfb | 71 | { |
c906108c SS |
72 | Hashentry *q; |
73 | ||
dfcd3bfb JM |
74 | while (*p) |
75 | { | |
76 | if ((*p)->first == first) | |
77 | { | |
78 | q = (*p)->next; | |
79 | free (*p); | |
80 | *p = q; | |
81 | return; | |
82 | } | |
83 | p = &((*p)->next); | |
c906108c | 84 | } |
c906108c SS |
85 | } |
86 | ||
dfcd3bfb JM |
87 | void |
88 | BAG_putpair (long first, long second) | |
89 | { | |
c906108c SS |
90 | long junk; |
91 | ||
dfcd3bfb JM |
92 | if (BAG_getfirst (&junk, second) != NO_SUCH_PAIR) |
93 | BAG_killpair_bysecond (second); | |
94 | addtolist (&lookupbyfirst[hash (first)], first, second); | |
95 | addtolist (&lookupbysecond[hash (second)], first, second); | |
c906108c SS |
96 | } |
97 | ||
dfcd3bfb JM |
98 | Bag_error |
99 | BAG_getfirst (long *first, long second) | |
100 | { | |
c906108c SS |
101 | Hashentry *look; |
102 | ||
dfcd3bfb JM |
103 | look = lookupbysecond[hash (second)]; |
104 | while (look) | |
105 | if (look->second == second) | |
106 | { | |
107 | *first = look->first; | |
108 | return NO_ERROR; | |
109 | } | |
c906108c SS |
110 | return NO_SUCH_PAIR; |
111 | } | |
112 | ||
dfcd3bfb JM |
113 | Bag_error |
114 | BAG_getsecond (long first, long *second) | |
115 | { | |
c906108c SS |
116 | Hashentry *look; |
117 | ||
dfcd3bfb JM |
118 | look = lookupbyfirst[hash (first)]; |
119 | while (look) | |
120 | { | |
121 | if (look->first == first) | |
122 | { | |
123 | *second = look->second; | |
124 | return NO_ERROR; | |
125 | } | |
126 | look = look->next; | |
c906108c | 127 | } |
c906108c SS |
128 | return NO_SUCH_PAIR; |
129 | } | |
130 | ||
dfcd3bfb JM |
131 | Bag_error |
132 | BAG_killpair_byfirst (long first) | |
133 | { | |
c906108c SS |
134 | long second; |
135 | ||
dfcd3bfb | 136 | if (BAG_getsecond (first, &second) == NO_SUCH_PAIR) |
c906108c | 137 | return NO_SUCH_PAIR; |
6d358e86 NC |
138 | removefromlist (&lookupbyfirst[hash (first)], first); |
139 | removefromlist (&lookupbysecond[hash (second)], first); | |
c906108c SS |
140 | return NO_ERROR; |
141 | } | |
142 | ||
dfcd3bfb JM |
143 | Bag_error |
144 | BAG_killpair_bysecond (long second) | |
145 | { | |
c906108c | 146 | long first; |
dfcd3bfb JM |
147 | |
148 | if (BAG_getfirst (&first, second) == NO_SUCH_PAIR) | |
c906108c | 149 | return NO_SUCH_PAIR; |
6d358e86 NC |
150 | removefromlist (&lookupbyfirst[hash (first)], first); |
151 | removefromlist (&lookupbysecond[hash (second)], first); | |
c906108c SS |
152 | return NO_ERROR; |
153 | } | |
154 | ||
dfcd3bfb JM |
155 | void |
156 | BAG_newbag () | |
157 | { | |
c906108c SS |
158 | int i; |
159 | ||
dfcd3bfb JM |
160 | for (i = 0; i < 256; i++) |
161 | { | |
162 | killwholelist (lookupbyfirst[i]); | |
163 | killwholelist (lookupbysecond[i]); | |
164 | lookupbyfirst[i] = lookupbysecond[i] = (Hashentry *) 0; | |
165 | } | |
c906108c | 166 | } |