// 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;
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);
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
return 0;
}
+unsigned int *mhashGetKeys(unsigned int *numKeys)
+{
+ unsigned int *keys;
+ int i, keyindex;
+ mhashlistnode_t *curr;
+
+ pthread_mutex_lock(&mlookup.locktable);
+
+ *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;
+ }
+ }
+ }
+
+ if (keyindex != *numKeys)
+ printf("mhashGetKeys(): WARNING: incorrect mlookup.numelements value!\n");
+
+ pthread_mutex_unlock(&mlookup.locktable);
+ return keys;
+}
+