Mercurial > hg > Game > Cerium
view TaskManager/kernel/memory/MemHash.cc @ 638:250c7353ddaf draft
hmmmm
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 19 Nov 2009 18:18:20 +0900 |
parents | 94d82f2c842f |
children | c50f39fbb6ca |
line wrap: on
line source
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "MemHash.h" static unsigned short PRIME[8] = { 0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd, }; int MemHash::hash(memaddr data0) { long data = (long)data0; int value = 0; int n = 0; long key; for (uint32 i = 0; i < sizeof(memaddr) * 2; i ++) { key = data & 0xf; value += key * PRIME[n++ & 7]; data >>= 4; } return value % hashSize; } MemHash::MemHash() { table = (MemorySegmentPtr*)malloc(tableSize); clear(); } int MemHash::put(memaddr key, MemorySegmentPtr data) { int hashval = hash(key); for (int i = 0; i < hashSize/2; i++) { int index = (hashval + i*i)%hashSize; if (table[index] == 0) { // 空の table に入れる table[index] = data; return index; } } return -1; } MemorySegmentPtr MemHash::get(memaddr key) { int hashval = hash(key); for (int i = 0; i < hashSize/2; i++) { int index = (hashval + i*i)%hashSize; if (table[index] != NULL && table[index]->address == key) { return table[index]; } } return NULL; } void MemHash::remove(memaddr key) { int hashval = hash(key); for (int i = 0; i < hashSize/2; i++) { int index = (hashval + i*i)%hashSize; if (table[index] != NULL && table[index]->address == key) { table[index] = NULL; } } } void MemHash::clear(void) { bzero(table, tableSize); } /* end */