7bf79ba64a8de33a71ede200f2bdd203113591c1
[IRC.git] / Robust / src / Runtime / DSTM / interface / plookup.c
1 #include "plookup.h"
2 extern int classsize[];
3
4 //NOTE: "pile" ptr points to the head of the linked list of the machine pile data structures 
5
6 /* This function creates a new pile data structure to hold
7  * obj ids of objects modified or read inside a transaction,
8  * no of objects read and no of objects modified
9  * that belong to a single machine */
10
11 plistnode_t *pCreate(int objects) {
12         plistnode_t *pile;
13         
14         //Create main structure
15         if((pile = calloc(1, sizeof(plistnode_t))) == NULL) {
16                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
17                 return NULL;
18         }       
19         if ((pile->oidmod = calloc(objects, sizeof(unsigned int))) == NULL) {
20                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
21                 free(pile);
22                 return NULL;
23         }
24         /*
25         if ((pile->oidread = calloc(objects, sizeof(unsigned int))) == NULL) {
26                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
27                 return NULL;
28         }
29         */
30         if ((pile->objread = calloc(objects, sizeof(unsigned int) + sizeof(short))) == NULL) {
31                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
32                 free(pile);
33                 free(pile->oidmod);
34                 return NULL;
35         }
36
37         pile->nummod = pile->numread = pile->sum_bytes = 0;
38         pile->next = NULL;
39         return pile;
40 }
41
42 /* This function inserts necessary information into 
43  * a machine pile data structure */
44 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs) {
45         plistnode_t *ptr, *tmp;
46         int found = 0, offset;
47
48         tmp = pile;
49         //Add oid into a machine that is already present in the pile linked list structure
50         while(tmp != NULL) {
51                 if (tmp->mid == mid) {
52                         if (STATUS(headeraddr) & DIRTY) {
53                                 tmp->oidmod[tmp->nummod] = OID(headeraddr);
54                                 tmp->nummod = tmp->nummod + 1;
55                                 tmp->sum_bytes += sizeof(objheader_t) + classsize[TYPE(headeraddr)];
56                         } else {
57                 //              tmp->oidread[tmp->numread] = OID(headeraddr);
58                                 offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
59                                 *((unsigned int *)(tmp->objread + offset))=OID(headeraddr);
60                                 offset += sizeof(unsigned int);
61                                 memcpy(tmp->objread + offset, &headeraddr->version, sizeof(short));
62                                 tmp->numread = tmp->numread + 1;
63                         }
64                         found = 1;
65                         break;
66                 }
67                 tmp = tmp->next;
68         }
69         //Add oid for any new machine 
70         if (!found) {
71                 if((ptr = pCreate(num_objs)) == NULL) {
72                         return NULL;
73                 }
74                 ptr->mid = mid;
75                 if (STATUS(headeraddr) & DIRTY) {
76                         ptr->oidmod[ptr->nummod] = OID(headeraddr);
77                         ptr->nummod = ptr->nummod + 1;
78                         ptr->sum_bytes += sizeof(objheader_t) + classsize[TYPE(headeraddr)];
79                 } else {
80                 //      ptr->oidread[ptr->numread] = OID(headeraddr);
81                         *((unsigned int *)ptr->objread)=OID(headeraddr);
82                         memcpy(ptr->objread + sizeof(unsigned int), &headeraddr->version, sizeof(short));
83                         ptr->numread = ptr->numread + 1;
84                 }
85                 ptr->next = pile;
86                 pile = ptr;
87         }
88
89         return pile;
90 }
91
92 //Count the number of machine piles
93 int pCount(plistnode_t *pile) {
94         plistnode_t *tmp;
95         int pcount = 0;
96         tmp = pile;
97         while(tmp != NULL) {
98                 pcount++;
99                 tmp = tmp->next;
100         }
101         return pcount;
102 }
103
104 //Make a list of mid's for each machine group
105 int pListMid(plistnode_t *pile, unsigned int *list) {
106         int i = 0;
107         plistnode_t *tmp;
108         tmp = pile;
109         while (tmp != NULL) {
110                 list[i] = tmp->mid;
111                 i++;
112                 tmp = tmp->next;
113         }
114         return 0;
115 }
116
117 //Delete the entire pile
118 void pDelete(plistnode_t *pile) {
119         plistnode_t *next, *tmp;
120         tmp = pile;
121         while(tmp != NULL) {
122                 next = tmp->next;
123                 free(tmp->oidmod);
124                 //free(tmp->oidread);
125                 free(tmp->objread);
126                 free(tmp);
127                 tmp = next;
128         }
129         return;
130 }