0
|
1 #include <stdio.h>
|
|
2 #include <string.h>
|
|
3 #include <stdlib.h>
|
|
4 #include "MemHash.h"
|
|
5
|
|
6 unsigned int
|
|
7 MemHash::hash(memaddr data0)
|
|
8 {
|
|
9 unsigned long data = (unsigned long)data0;
|
|
10 #if ABIBIT>32
|
|
11 register int i_PeRlHaSh = 8;
|
|
12 #else
|
|
13 register int i_PeRlHaSh = 4;
|
|
14 #endif
|
|
15 register uint32 hash_PeRlHaSh = 0;
|
|
16 while (i_PeRlHaSh--) {
|
|
17 hash_PeRlHaSh += data & 0xff;
|
|
18 hash_PeRlHaSh += (hash_PeRlHaSh << 10);
|
|
19 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6);
|
|
20 data >>= 8;
|
|
21 }
|
|
22 hash_PeRlHaSh += (hash_PeRlHaSh << 3);
|
|
23 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11);
|
|
24 return (hash_PeRlHaSh + (hash_PeRlHaSh << 15));
|
|
25 }
|
|
26
|
|
27 #if 0
|
|
28 static unsigned short PRIME[8] = {
|
|
29 0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd,
|
|
30 };
|
|
31
|
|
32 int
|
|
33 MemHash::hash(memaddr data0)
|
|
34 {
|
|
35 unsigned long data = (unsigned long)data0;
|
|
36 int value = 0;
|
|
37 int n = 0;
|
|
38 long key;
|
|
39
|
|
40 for (uint32 i = 0; i < sizeof(memaddr) * 2; i ++) {
|
|
41 key = data & 0xf;
|
|
42 value += key * PRIME[n++ & 7];
|
|
43 data >>= 4;
|
|
44 }
|
|
45 printf("hash value %0x => %0x\n",data0, value);
|
|
46
|
|
47 return value % hashSize;
|
|
48 }
|
|
49 #endif
|
|
50
|
|
51 MemHash::MemHash()
|
|
52 {
|
|
53 table = (MemorySegmentPtr*)malloc(tableSize);
|
|
54 clear();
|
|
55 }
|
|
56
|
|
57 int
|
|
58 MemHash::put(memaddr key, MemorySegmentPtr data)
|
|
59 {
|
|
60 int hashval = hash(key);
|
|
61
|
|
62 for (int i = 0; i < hashSize/2; i++) {
|
|
63 int index = (hashval + i*i)%hashSize;
|
|
64
|
|
65 if (table[index] == 0) { // 空の table に入れる
|
|
66 table[index] = data;
|
|
67 return index;
|
|
68 }
|
|
69 }
|
|
70
|
|
71 return -1;
|
|
72 }
|
|
73
|
|
74 MemorySegmentPtr
|
|
75 MemHash::get(memaddr key)
|
|
76 {
|
|
77 unsigned int hashval = hash(key);
|
|
78
|
|
79 for (int i = 0; i < hashSize/2; i++) {
|
|
80 int index = (hashval + i*i)%hashSize;
|
|
81
|
|
82 if (table[index] != NULL &&
|
|
83 table[index]->address == key) {
|
|
84 //printf("get hash value %0x\n",index);
|
|
85 return table[index];
|
|
86 }
|
|
87 }
|
|
88
|
|
89 return NULL;
|
|
90 }
|
|
91
|
|
92 void
|
|
93 MemHash::remove(memaddr key)
|
|
94 {
|
|
95 unsigned int hashval = hash(key);
|
|
96
|
|
97 for (int i = 0; i < hashSize/2; i++) {
|
|
98 int index = (hashval + i*i)%hashSize;
|
|
99
|
|
100 if (table[index] != NULL &&
|
|
101 table[index]->address == key) {
|
|
102 table[index] = NULL;
|
|
103 }
|
|
104 }
|
|
105 }
|
|
106
|
|
107 void
|
|
108 MemHash::clear(void)
|
|
109 {
|
|
110 bzero(table, tableSize);
|
|
111 }
|
|
112
|
|
113 /* end */
|