server portions done except a few things
[IRC.git] / Robust / src / Runtime / DSTM / interface / plookup.c
1 #include "plookup.h"
2 extern int classsize[];
3
4 plistnode_t *pCreate(int objects) {
5         plistnode_t *pile;
6         
7         //Create main structure
8         if((pile = calloc(1, sizeof(plistnode_t))) == NULL) {
9                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
10                 return NULL;
11         }       
12         pile->next = NULL;
13         if ((pile->oidmod = calloc(objects, sizeof(unsigned int))) == NULL) {
14                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
15                 return NULL;
16         }
17         if ((pile->oidread = calloc(objects, sizeof(unsigned int))) == NULL) {
18                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
19                 return NULL;
20         }
21         pile->nummod = pile->numread = pile->sum_bytes = 0;
22         if ((pile->objread = calloc(objects, sizeof(int) + sizeof(short))) == NULL) {
23                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
24                 return NULL;
25         }
26         pile->objmodified = NULL;
27         pile->nummod = pile->numread = pile->sum_bytes = 0;
28
29         return pile;
30 }
31
32 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs) {
33         plistnode_t *ptr, *tmp;
34         int found = 0, offset;
35
36         tmp = pile;
37         //Add oid into a machine that is a part of the pile linked list structure
38         while(tmp != NULL) {
39                 if (tmp->mid == mid) {
40                         if ((headeraddr->status & DIRTY) == 1) {
41                                 tmp->oidmod[tmp->nummod] = headeraddr->oid;
42                                 tmp->nummod = tmp->nummod + 1;
43                                 tmp->sum_bytes += sizeof(objheader_t) + classsize[headeraddr->type];
44                         } else {
45                                 tmp->oidread[tmp->numread] = headeraddr->oid;
46                                 offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
47                                 memcpy(tmp->objread + offset, &headeraddr->oid, sizeof(unsigned int));
48                                 offset += sizeof(unsigned int);
49                                 memcpy(tmp->objread + offset, &headeraddr->version, sizeof(short));
50                                 tmp->numread = tmp->numread + 1;
51                         }
52                         found = 1;
53                         break;
54                 }
55                 tmp = tmp->next;
56         }
57         //Add oid for any new machine 
58         if (!found) {
59                 if((ptr = pCreate(num_objs)) == NULL) {
60                         return NULL;
61                 }
62                 ptr->mid = mid;
63                 if ((headeraddr->status & DIRTY) == 1) {
64                         ptr->oidmod[ptr->nummod] = headeraddr->oid;
65                         ptr->nummod = ptr->nummod + 1;
66                         ptr->sum_bytes += sizeof(objheader_t) + classsize[headeraddr->type];
67                 } else {
68                         ptr->oidread[ptr->numread] = headeraddr->oid;
69                         memcpy(ptr->objread, &headeraddr->oid, sizeof(unsigned int));
70                         //printf("DEBUG -> objread oid is %d\n", *(ptr->objread));
71                         memcpy(ptr->objread + sizeof(unsigned int), &headeraddr->version, sizeof(short));
72                         ptr->numread = ptr->numread + 1;
73                 }
74                 ptr->next = pile;
75                 pile = ptr;
76         }
77         return pile;
78 }
79
80 //Count the number of machine groups
81 int pCount(plistnode_t *pile) {
82         plistnode_t *tmp;
83         int pcount = 0;
84         tmp = pile;
85         while(tmp != NULL) {
86                 pcount++;
87                 tmp = tmp->next;
88         }
89         return pcount;
90 }
91
92 //Make a list of mid's for each machine group
93 int pListMid(plistnode_t *pile, unsigned int *list) {
94         int i = 0;
95         plistnode_t *tmp;
96         tmp = pile;
97         while (tmp != NULL) {
98                 list[i] = tmp->mid;
99                 i++;
100                 tmp = tmp->next;
101         }
102         return 0;
103 }
104
105 //Delete the entire pile
106 void pDelete(plistnode_t *pile) {
107         plistnode_t *next, *tmp;
108         tmp = pile;
109         while(tmp != NULL) {
110                 next = tmp->next;
111                 free(tmp->oidmod);
112                 free(tmp->oidread);
113                 free(tmp->objread);
114                 free(tmp);
115                 tmp = next;
116         }
117         return;
118 }