1 /************************************************************************************************
3 All llookup hash function prototypes returns 0 on sucess and 1 otherwise
4 llookup hash is an array of lhashlistnode_t
5 oid = mid = 0 in a given lhashlistnode_t for each bin in the hash table ONLY if the entry is empty =>
6 the OID's can be any unsigned int except 0
8 Uses pthreads. compile using -lpthread option
9 ***************************************************************************************************/
14 extern unsigned int *hostIpAddrs;
15 extern unsigned int oidsPerBlock;
17 unsigned int lhashCreate(unsigned int size, float loadfactor) {
21 unsigned int lhashInsert(unsigned int oid, unsigned int mid) {
25 unsigned int lhashSearch(unsigned int oid) {
26 if (oidsPerBlock == 0)
27 return hostIpAddrs[0];
29 return hostIpAddrs[oid / oidsPerBlock];
32 unsigned int lhashRemove(unsigned int oid) {
38 lhashtable_t llookup; //Global Hash table
40 // Creates a hash table with size and an array of lhashlistnode_t
41 unsigned int lhashCreate(unsigned int size, float loadfactor) {
42 lhashlistnode_t *nodes;
45 // Allocate space for the hash table
46 if((nodes = calloc(size, sizeof(lhashlistnode_t))) == NULL) {
47 printf("Calloc error %s %d\n", __FILE__, __LINE__);
51 llookup.table = nodes;
53 llookup.numelements = 0; // Initial number of elements in the hash
54 llookup.loadfactor = loadfactor;
55 //Initialize the pthread_mutex variable
56 pthread_mutex_init(&llookup.locktable, NULL);
60 // Assign to oids to bins inside hash table
61 unsigned int lhashFunction(unsigned int oid) {
62 return( oid % (llookup.size));
65 // Insert oid and mid mapping into the hash table
66 unsigned int lhashInsert(unsigned int oid, unsigned int mid) {
69 lhashlistnode_t *ptr, *node;
71 if (llookup.numelements > (llookup.loadfactor * llookup.size)) {
73 newsize = 2 * llookup.size + 1;
74 pthread_mutex_lock(&llookup.locktable);
76 pthread_mutex_unlock(&llookup.locktable);
80 llookup.numelements++;
82 index = lhashFunction(oid);
84 printf("DEBUG(insert) oid = %d, mid =%d, index =%d\n",oid,mid, index);
86 pthread_mutex_lock(&llookup.locktable);
87 if(ptr[index].next == NULL && ptr[index].oid == 0) { // Insert at the first position in the hashtable
90 } else { // Insert in the linked list
91 if ((node = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
92 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
93 pthread_mutex_unlock(&llookup.locktable);
98 node->next = ptr[index].next;
99 ptr[index].next = node;
102 pthread_mutex_unlock(&llookup.locktable);
106 // Return mid for a given oid in the hash table
107 unsigned int lhashSearch(unsigned int oid) {
109 lhashlistnode_t *ptr, *node;
111 ptr = llookup.table; // Address of the beginning of hash table
112 index = lhashFunction(oid);
114 pthread_mutex_lock(&llookup.locktable);
115 while(node != NULL) {
116 if(node->oid == oid) {
117 pthread_mutex_unlock(&llookup.locktable);
122 pthread_mutex_unlock(&llookup.locktable);
126 // Remove an entry from the hash table
127 unsigned int lhashRemove(unsigned int oid) {
129 lhashlistnode_t *curr, *prev;
130 lhashlistnode_t *ptr, *node;
133 index = lhashFunction(oid);
136 pthread_mutex_lock(&llookup.locktable);
137 for (; curr != NULL; curr = curr->next) {
138 if (curr->oid == oid) { // Find a match in the hash table
139 llookup.numelements--; // Decrement the number of elements in the global hashtable
140 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of lhashlistnode_t
143 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of lhashlistnode_t connected
144 curr->oid = curr->next->oid;
145 curr->mid = curr->next->mid;
147 curr->next = curr->next->next;
149 } else { // Regular delete from linked listed
150 prev->next = curr->next;
153 pthread_mutex_unlock(&llookup.locktable);
158 pthread_mutex_unlock(&llookup.locktable);
163 unsigned int lhashResize(unsigned int newsize) {
164 lhashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next lhashlistnodes in a linked list
165 unsigned int oldsize;
166 int isfirst; // Keeps track of the first element in the lhashlistnode_t for each bin in hashtable
168 lhashlistnode_t *newnode;
171 oldsize = llookup.size;
173 if((node = calloc(newsize, sizeof(lhashlistnode_t))) == NULL) {
174 printf("Calloc error %s %d\n", __FILE__, __LINE__);
178 llookup.table = node; //Update the global hashtable upon resize()
179 llookup.size = newsize;
180 llookup.numelements = 0;
182 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
185 while (curr != NULL) { //Inner loop to go through linked lists
186 if (curr->oid == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
187 break; //oid = mid =0 for element if not present within the hash table
190 index = lhashFunction(curr->oid);
191 // Insert into the new table
192 if(llookup.table[index].next == NULL && llookup.table[index].oid == 0) {
193 llookup.table[index].oid = curr->oid;
194 llookup.table[index].mid = curr->mid;
195 llookup.numelements++;
197 if((newnode = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
198 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
201 newnode->oid = curr->oid;
202 newnode->mid = curr->mid;
203 newnode->next = llookup.table[index].next;
204 llookup.table[index].next = newnode;
205 llookup.numelements++;
208 //free the linked list of lhashlistnode_t if not the first element in the hash table
219 free(ptr); //Free the memory of the old hash table