remove some codes for scheduling
[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                   int tmpsize;
54                   
55                   if (STATUS(headeraddr) & NEW) {
56                     tmp->oidcreated[tmp->numcreated] = OID(headeraddr);
57                     tmp->numcreated = tmp->numcreated + 1;
58                     GETSIZE(tmpsize, headeraddr);
59                     tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
60                   }else if (STATUS(headeraddr) & DIRTY) {
61                     tmp->oidmod[tmp->nummod] = OID(headeraddr);
62                     tmp->nummod = tmp->nummod + 1;
63                     GETSIZE(tmpsize, headeraddr);
64                     tmp->sum_bytes += sizeof(objheader_t) + tmpsize;
65                   } else {
66                     offset = (sizeof(unsigned int) + sizeof(short)) * tmp->numread;
67                     *((unsigned int *)(tmp->objread + offset))=OID(headeraddr);
68                     offset += sizeof(unsigned int);
69                     memcpy(tmp->objread + offset, &headeraddr->version, sizeof(short));
70                     tmp->numread = tmp->numread + 1;
71                   }
72                   found = 1;
73                   break;
74                 }
75                 tmp = tmp->next;
76         }
77         //Add oid for any new machine 
78         if (!found) {
79           int tmpsize;
80           if((ptr = pCreate(num_objs)) == NULL) {
81             return NULL;
82           }
83           ptr->mid = mid;
84           if (STATUS(headeraddr) & NEW) {
85             ptr->oidcreated[ptr->numcreated] = OID(headeraddr);
86             ptr->numcreated = ptr->numcreated + 1;
87             GETSIZE(tmpsize, headeraddr);
88             ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
89           } else if (STATUS(headeraddr) & DIRTY) {
90             ptr->oidmod[ptr->nummod] = OID(headeraddr);
91             ptr->nummod = ptr->nummod + 1;
92             GETSIZE(tmpsize, headeraddr);
93             ptr->sum_bytes += sizeof(objheader_t) + tmpsize;
94           } else {
95             *((unsigned int *)ptr->objread)=OID(headeraddr);
96             memcpy(ptr->objread + sizeof(unsigned int), &headeraddr->version, sizeof(short));
97             ptr->numread = ptr->numread + 1;
98           }
99           ptr->next = pile;
100           pile = ptr;
101         }
102         
103         /* Clear Flags */
104
105         STATUS(headeraddr) &= ~(NEW);
106         STATUS(headeraddr) &= ~(DIRTY);
107
108         return pile;
109 }
110
111 //Count the number of machine piles
112 int pCount(plistnode_t *pile) {
113         plistnode_t *tmp;
114         int pcount = 0;
115         tmp = pile;
116         while(tmp != NULL) {
117                 pcount++;
118                 tmp = tmp->next;
119         }
120         return pcount;
121 }
122
123 //Make a list of mid's for each machine group
124 int pListMid(plistnode_t *pile, unsigned int *list) {
125         int i = 0;
126         plistnode_t *tmp;
127         tmp = pile;
128         while (tmp != NULL) {
129                 list[i] = tmp->mid;
130                 i++;
131                 tmp = tmp->next;
132         }
133         return 0;
134 }
135
136 //Delete the entire pile
137 void pDelete(plistnode_t *pile) {
138         plistnode_t *next, *tmp;
139         tmp = pile;
140         while(tmp != NULL) {
141                 next = tmp->next;
142                 free(tmp->oidmod);
143                 free(tmp->oidcreated);
144                 free(tmp->objread);
145                 free(tmp);
146                 tmp = next;
147         }
148         return;
149 }