Untest code for trans commit
[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 plistnode_t *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 NULL;
41                 }
42                 ptr->mid = mid;
43                 ptr->obj[ptr->index] = oid;
44                 ptr->index++;
45                 ptr->next = pile;
46                 pile = ptr;
47         }
48         return pile;
49 }
50
51 //Count the number of machine groups
52 int pCount(plistnode_t *pile) {
53         plistnode_t *tmp;
54         int pcount = 0;
55         tmp = pile;
56         while(tmp != NULL) {
57                 pcount++;
58                 tmp = tmp->next;
59         }
60         return pcount;
61 }
62
63 //Make a list of mid's for each machine group
64 int pListMid(plistnode_t *pile, unsigned int *list) {
65         int i = 0;
66         plistnode_t *tmp;
67         tmp = pile;
68         while (tmp != NULL) {
69                 list[i] = tmp->mid;
70                 i++;
71         }
72         return 0;
73 }
74
75 // Return objects for a given mid
76 unsigned int *pSearch(plistnode_t *pile, unsigned int mid) {
77         plistnode_t *tmp;
78         tmp = pile;
79         while(tmp != NULL) {
80                 if(tmp->mid == mid) {
81                         return(tmp->obj);
82                 }
83                 tmp = tmp->next;
84         }
85         return NULL;
86 }
87
88 //Delete the entire pile
89 void pDelete(plistnode_t *pile) {
90         plistnode_t *next, *tmp;
91         tmp = pile;
92         while(tmp != NULL) {
93                 next = tmp->next;
94                 free(tmp->obj);
95                 free(tmp);
96                 tmp = next;
97         }
98         return;
99 }