// Creates a machine lookup table with size =" size"
unsigned int mhashCreate(unsigned int size, float loadfactor) {
mhashlistnode_t *nodes;
- int i;
-
// Allocate space for the hash table
if((nodes = calloc(size, sizeof(mhashlistnode_t))) == NULL) {
printf("Calloc error %s %d\n", __FILE__, __LINE__);
ptr = mlookup.table;
mlookup.numelements++;
- index = mhashFunction(key);
#ifdef DEBUG
printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
#endif
pthread_mutex_lock(&mlookup.locktable);
+ index = mhashFunction(key);
if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
ptr[index].key = key;
ptr[index].val = val;
} else { // Insert in the beginning of linked list
if ((node = calloc(1, sizeof(mhashlistnode_t))) == NULL) {
printf("Calloc error %s, %d\n", __FILE__, __LINE__);
+ pthread_mutex_unlock(&mlookup.locktable);
return 1;
}
node->key = key;
- node->val = val ;
+ node->val = val;
node->next = ptr[index].next;
ptr[index].next = node;
}
int index;
mhashlistnode_t *ptr, *node;
+ pthread_mutex_lock(&mlookup.locktable);
ptr = mlookup.table; // Address of the beginning of hash table
index = mhashFunction(key);
node = &ptr[index];
- pthread_mutex_lock(&mlookup.locktable);
while(node != NULL) {
if(node->key == key) {
+ pthread_mutex_unlock(&mlookup.locktable);
return node->val;
}
node = node->next;
mhashlistnode_t *curr, *prev;
mhashlistnode_t *ptr, *node;
+ pthread_mutex_lock(&mlookup.locktable);
ptr = mlookup.table;
index = mhashFunction(key);
curr = &ptr[index];
-
- pthread_mutex_lock(&mlookup.locktable);
for (; curr != NULL; curr = curr->next) {
if (curr->key == key) { // Find a match in the hash table
mlookup.numelements--; // Decrement the number of elements in the global hashtable
prev->next = curr->next;
free(curr);
}
+ pthread_mutex_unlock(&mlookup.locktable);
return 0;
}
prev = curr;
return 0;
}
-#if 0
-// Hash Resize
-vkey resize(obj_addr_table_t * table){
- int newCapacity = 2*(table->size) + 1;
- obj_listnode_t **old;
- //if ((table->hash = (obj_listnode_t **) malloc(sizeof(obj_listnode_t *)*size)) == NULL) {
-}
+unsigned int *mhashGetKeys(unsigned int *numKeys)
+{
+ unsigned int *keys;
+ int i, keyindex;
+ mhashlistnode_t *curr;
-// Hashing for the Key
-int hashKey(unsigned int key, obj_addr_table_t *table) {
- // hash32shiftmult
- int c2=0x27d4eb2d; // a prime or an odd constant
- key = (key ^ 61) ^ (key >> 16);
- key = key + (key << 3);
- key = key ^ (key >> 4);
- key = key * c2;
- key = key ^ (key >> 15);
- printf("The bucket number is %d\n", key % (table->size));
- return (key % (table->size));
-}
+ pthread_mutex_lock(&mlookup.locktable);
-//Add key and its address to the new ob_listnode_t
-vkey addKey(unsigned int key, objheader_t *ptr, obj_addr_table_t *table) {
- int index;
- obj_listnode_t *node;
-
- table->numelements++;
- if(table->numelements > (table->loadfactor * table->size)){
- //TODO : check if table is nearly full and then resize
+ *numKeys = mlookup.numelements;
+ keys = calloc(*numKeys, sizeof(unsigned int));
+
+ keyindex = 0;
+ for (i = 0; i < mlookup.size; i++)
+ {
+ if (mlookup.table[i].key != 0)
+ {
+ curr = &mlookup.table[i];
+ while (curr != NULL)
+ {
+ keys[keyindex++] = curr->key;
+ curr = curr->next;
+ }
+ }
}
- index = hashKey(key,table);
- if ((node = (obj_listnode_t *) malloc(sizeof(obj_listnode_t))) == NULL) {
- printf("Malloc error %s %d\n", __FILE__, __LINE__);
- exit(-1);
- }
- node->key = key;
- node->object = ptr;
- node->next = table->hash[index];
- table->hash[index] = node;
- return;
-}
-// Get the address of the object header for a given key
-objheader_t *findKey(unsigned int key, obj_addr_table_t *table) {
- int index;
- obj_listnode_t *ptr;
+ if (keyindex != *numKeys)
+ printf("mhashGetKeys(): WARNING: incorrect mlookup.numelements value!\n");
- index = hashKey(key,table);
- ptr = table->hash[index];
- while(ptr != NULL) {
- if (ptr->key == key) {
- return ptr->object;
- }
- ptr = ptr->next;
- }
- return NULL;
+ pthread_mutex_unlock(&mlookup.locktable);
+ return keys;
}
-// Remove the pointer to the object header from a linked list of obj_listnode_t given an key
-int removeKey(unsigned int key, obj_addr_table_t *table) {
- int index;
- obj_listnode_t *curr, *prev; // prev points to previous node and curr points to the node to be deleted
- index = hashKey(key,table);
- prev = curr = table->hash[index];
- for (; curr != NULL; curr = curr->next) {
- if (curr->key == key) { // Find a match in the hash table
- table->numelements--;
- prev->next = curr->next;
- if (table->hash[index] == curr) { // Special case when there is one element pointed by the hash table
- table->hash[index] = NULL;
- }
- free(curr);
- return 0;
- }
- prev = curr;
- }
- return -1;
-}
-
-#endif