Mercurial > hg > Members > kono > Cerium
view TaskManager/kernel/memory/MemHash.cc @ 388:82cb9368e3ff
MemHash (OS X version)
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 06 Aug 2009 23:30:26 +0900 |
parents | 5e2d30bfbf23 |
children | 60aa3f241b10 |
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 data) { int value = 0; int n = 0; int 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() { //hashSize = 263; //tableSize = sizeof(MemorySegmentPtr)*hashSize; 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 */