Object creation within transaction works now. Yay!
[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         if ((pile->oidcreated = calloc(objects, sizeof(unsigned int))) == NULL) {
25                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
26                 free(pile);
27                 free(pile->oidmod);
28                 return NULL;
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                 free(pile->oidcreated);
35                 return NULL;
36         }
37
38         pile->nummod = pile->numread = pile->numcreated = pile->sum_bytes = 0;
39         pile->next = NULL;
40         return pile;
41 }
42
43 /* This function inserts necessary information into 
44  * a machine pile data structure */
45 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs) {
46         plistnode_t *ptr, *tmp;
47         int found = 0, offset;
48
49         tmp = pile;
50         //Add oid into a machine that is already present in the pile linked list structure
51         while(tmp != NULL) {
52                 if (tmp->mid == mid) {
53                         if (STATUS(headeraddr) & NEW) {
54                                 tmp->oidcreated[tmp->numcreated] = OID(headeraddr);
55                                 tmp->numcreated = tmp->numcreated + 1;
56                                 tmp->sum_bytes += sizeof(objheader_t) + classsize[TYPE(headeraddr)];
57                         }       else if (STATUS(headeraddr) & DIRTY) {
58                                 tmp->oidmod[tmp->nummod] = OID(headeraddr);
59                                 tmp->nummod = tmp->nummod + 1;
60                                 tmp->sum_bytes += sizeof(objheader_t) + classsize[TYPE(headeraddr)];
61                         } else {
62                                 offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
63                                 *((unsigned int *)(tmp->objread + offset))=OID(headeraddr);
64                                 offset += sizeof(unsigned int);
65                                 memcpy(tmp->objread + offset, &headeraddr->version, sizeof(short));
66                                 tmp->numread = tmp->numread + 1;
67                         }
68                         found = 1;
69                         break;
70                 }
71                 tmp = tmp->next;
72         }
73         //Add oid for any new machine 
74         if (!found) {
75                 if((ptr = pCreate(num_objs)) == NULL) {
76                         return NULL;
77                 }
78                 ptr->mid = mid;
79                 if (STATUS(headeraddr) & NEW) {
80                         ptr->oidcreated[ptr->numcreated] = OID(headeraddr);
81                         ptr->numcreated = ptr->numcreated + 1;
82                         ptr->sum_bytes += sizeof(objheader_t) + classsize[TYPE(headeraddr)];
83                 } else if (STATUS(headeraddr) & DIRTY) {
84                         ptr->oidmod[ptr->nummod] = OID(headeraddr);
85                         ptr->nummod = ptr->nummod + 1;
86                         ptr->sum_bytes += sizeof(objheader_t) + classsize[TYPE(headeraddr)];
87                 } else {
88                         *((unsigned int *)ptr->objread)=OID(headeraddr);
89                         memcpy(ptr->objread + sizeof(unsigned int), &headeraddr->version, sizeof(short));
90                         ptr->numread = ptr->numread + 1;
91                 }
92                 ptr->next = pile;
93                 pile = ptr;
94         }
95
96         STATUS(headeraddr) &= ~(NEW);
97         STATUS(headeraddr) &= ~(DIRTY);
98
99         return pile;
100 }
101
102 //Count the number of machine piles
103 int pCount(plistnode_t *pile) {
104         plistnode_t *tmp;
105         int pcount = 0;
106         tmp = pile;
107         while(tmp != NULL) {
108                 pcount++;
109                 tmp = tmp->next;
110         }
111         return pcount;
112 }
113
114 //Make a list of mid's for each machine group
115 int pListMid(plistnode_t *pile, unsigned int *list) {
116         int i = 0;
117         plistnode_t *tmp;
118         tmp = pile;
119         while (tmp != NULL) {
120                 list[i] = tmp->mid;
121                 i++;
122                 tmp = tmp->next;
123         }
124         return 0;
125 }
126
127 //Delete the entire pile
128 void pDelete(plistnode_t *pile) {
129         plistnode_t *next, *tmp;
130         tmp = pile;
131         while(tmp != NULL) {
132                 next = tmp->next;
133                 free(tmp->oidmod);
134                 //free(tmp->oidread);
135                 free(tmp->objread);
136                 free(tmp);
137                 tmp = next;
138         }
139         return;
140 }