1 /* bag.c -- ARMulator support code: ARM6 Instruction Emulator.
2 Copyright (C) 1994 Advanced RISC Machines Ltd.
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.
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.
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
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /********************************************************************/
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 */
25 /********************************************************************/
29 #define HASH_TABLE_SIZE 256
30 #define hash(x) (((x)&0xff)^(((x)>>8)&0xff)^(((x)>>16)&0xff)^(((x)>>24)&0xff))
32 typedef struct hashentry {
33 struct hashentry *next;
38 Hashentry *lookupbyfirst[HASH_TABLE_SIZE];
39 Hashentry *lookupbysecond[HASH_TABLE_SIZE];
41 void addtolist(Hashentry **add, long first, long second) {
42 while (*add) add = &((*add)->next);
43 /* Malloc will never fail? :o( */
44 (*add) = (Hashentry *) malloc(sizeof(Hashentry));
45 (*add)->next = (Hashentry *) 0;
46 (*add)->first = first;
47 (*add)->second = second;
50 void killwholelist(Hashentry *p) {
60 void removefromlist(Hashentry **p, long first, long second) {
64 if ((*p)->first == first) {
74 void BAG_putpair(long first, long second) {
77 if (BAG_getfirst(&junk, second) != NO_SUCH_PAIR)
78 BAG_killpair_bysecond(second);
79 addtolist(&lookupbyfirst[hash(first)], first, second);
80 addtolist(&lookupbysecond[hash(second)], first, second);
83 Bag_error BAG_getfirst(long *first, long second) {
86 look = lookupbysecond[hash(second)];
87 while(look) if (look->second == second) {
94 Bag_error BAG_getsecond(long first, long *second) {
97 look = lookupbyfirst[hash(first)];
99 if (look->first == first) {
100 *second = look->second;
108 Bag_error BAG_killpair_byfirst(long first) {
111 if (BAG_getsecond(first, &second) == NO_SUCH_PAIR)
113 removefromlist(&lookupbyfirst[hash(first)], first, second);
114 removefromlist(&lookupbysecond[hash(second)], first, second);
118 Bag_error BAG_killpair_bysecond(long second) {
121 if (BAG_getfirst(&first, second) == NO_SUCH_PAIR)
123 removefromlist(&lookupbyfirst[hash(first)], first, second);
124 removefromlist(&lookupbysecond[hash(second)], first, second);
131 for (i = 0; i < 256; i++) {
132 killwholelist(lookupbyfirst[i]);
133 killwholelist(lookupbysecond[i]);
134 lookupbyfirst[i] = lookupbysecond[i] = (Hashentry *) 0;