Trans commit initial version
[IRC.git] / Robust / src / Runtime / DSTM / interface / plookup.c
1  #include "plookup.h"
2
3 plistnode_t *pCreate(int objects) {
4         plistnode_t *pile;
5         
6         //Create main structure
7         if((pile = calloc(1, sizeof(plistnode_t))) == NULL) {
8                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
9                 return NULL;
10         }       
11         pile->next = NULL;
12         //Create array of objects
13         if((pile->obj = calloc(objects, sizeof(unsigned int))) == NULL) {
14                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
15                 return NULL;
16         }
17         pile->index = 0;
18         pile->vote = 0;
19         return pile;
20 }
21
22 unsigned int pInsert(plistnode_t *pile, unsigned int mid, unsigned int oid, int num_objs) {
23         plistnode_t *ptr, *tmp;
24         int found = 0;
25
26         tmp = pile;
27         //Add oid into a machine that is a part of the pile linked list structure
28         while(tmp != NULL) {
29                 if (tmp->mid == mid) {
30                         tmp->obj[tmp->index] = oid;
31                         tmp->index++;
32                         found = 1;
33                         break;
34                 }
35                 tmp = tmp->next;
36         }
37         //Add oid for any new machine 
38         if (!found) {
39                 if((ptr = pCreate(num_objs)) == NULL) {
40                         return 1;
41                 }
42                 ptr->mid = mid;
43                 ptr->obj[ptr->index] = oid;
44                 ptr->index++;
45                 ptr->next = pile;
46                 pile = ptr;
47         }
48         return 0;
49 }
50
51 // Return objects for a given mid
52 unsigned int *pSearch(plistnode_t *pile, unsigned int mid) {
53         plistnode_t *tmp;
54         tmp = pile;
55         while(tmp != NULL) {
56                 if(tmp->mid == mid) {
57                         return(tmp->obj);
58                 }
59                 tmp = tmp->next;
60         }
61         return NULL;
62 }
63
64 //Delete the entire pile
65 void pDelete(plistnode_t *pile) {
66         plistnode_t *next, *tmp;
67         tmp = pile;
68         while(tmp != NULL) {
69                 next = tmp->next;
70                 free(tmp->obj);
71                 free(tmp);
72                 tmp = next;
73         }
74         return;
75 }