A quad is now a file
[henge/webcc.git] / src / apc / irmem.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <apc/ir.h>
8 #include <unistd.h>
9
10 struct cdat*
11 alloc_cdat(void);
12 struct odat*
13 alloc_odat(void);
14 void
15 alloc_vdat(void);
16 struct link*
17 alloc_link(void);
18 struct ref*
19 alloc_ref(void);
20 struct cdat*
21 curr_cdat(void);
22 struct odat*
23 curr_odat(void);
24 struct vdat*
25 curr_vdat(void);
26 struct ele*
27 curr_ele(void);
28 struct set*
29 curr_set(void);
30 struct ref*
31 prev_ref(void);
32 struct model
33 curr_model(void);
34 void
35 inc_posts(void);
36
37 #define CURR_CDAT (*cdat_stackp)
38 #define CURR_SET set_list[CURR_CDAT->num_sets]
39 #define CURR_ELE ele_list[CURR_CDAT->CURR_SET.num_ele]
40 #define PREV_REF (ref_buf[num_refs-1])
41 #define CURR_REF (ref_buf[num_refs])
42 #define PREV_ODAT (odat_buf[num_odats-1])
43 #define CURR_ODAT (odat_buf[num_odats])
44 #define CURR_VDAT (vdat_buf[num_vdats])
45 #define PREV_VDAT (vdat_buf[num_vdats-1])
46 #define CURR_MODEL (CURR_VDAT->model_list[CURR_VDAT->num_models])
47 #define CURR_LINK (link_buf[num_links])
48 #define CURR_POST (post_buf[num_posts])
49 #define CURR_QUAD (CURR_ODAT->quad_file)
50
51
52 int num_cdats = 0;
53 int curr_max_cdats = PTRS_IN_PAGE;
54
55 struct cdat* cdat_buf[PTRS_IN_PAGE];
56 struct cdat* cdat_stack[PTRS_IN_PAGE];
57 struct cdat** cdat_stackp;
58
59
60 int num_odats = -1;
61 int curr_max_odats = PTRS_IN_PAGE;
62 struct odat* odat_buf[PTRS_IN_PAGE];
63
64
65 int num_vdats = -1;
66 int curr_max_vdats = PTRS_IN_PAGE;
67 struct vdat* vdat_buf[PTRS_IN_PAGE];
68
69
70 int num_refs = -1;
71 int curr_max_refs = PTRS_IN_PAGE;
72 struct ref* ref_buf[PTRS_IN_PAGE];
73 uint64_t ss_ref_id = 0x00FFFFFF; /* system space for ref_ids */
74
75 int num_posts = -1;
76 int curr_max_posts = PTRS_IN_PAGE;
77 struct ref* post_buf[PTRS_IN_PAGE];
78
79
80 int num_links = -1;
81 int curr_max_links = PTRS_IN_PAGE;
82 struct link* link_buf[PTRS_IN_PAGE];
83
84
85 /* The initalization function of the IR. */
86 int
87 ir_init()
88 {
89
90 /* Init root cdat and stack */
91 char root[4] = "root";
92
93 if( (cdat_buf[num_cdats] = (struct cdat*) malloc(sizeof(struct cdat))) == NULL)
94 {
95 perror("malloc root class failed\n");
96 return -1;
97 }
98 cdat_buf[num_cdats]->idx = num_cdats;
99 memmove(cdat_buf[num_cdats]->name, root, 4);
100
101 cdat_stackp = cdat_stack;
102 *cdat_stackp++ = cdat_buf[num_cdats++];
103
104 return 0;
105
106 }
107
108 void
109 ir_quit()
110 {
111 int i;
112
113 for(i = 0; i <= num_odats ; i++)
114 {
115 free(odat_buf[i]);
116 }
117 for(i = 0; i <= num_cdats; i++)
118 {
119 free(cdat_buf[i]);
120 }
121 for(i = 0; i <= num_vdats; i++)
122 {
123 free(vdat_buf[i]);
124 }
125 for(i = 0; i <= num_refs; i++)
126 {
127 free(ref_buf[i]);
128 }
129 for(i = 0; i<= num_links; i++)
130 {
131 free(link_buf[i]);
132 }
133
134 }
135
136 //TODO: FREE MEMORY!
137 struct cdat*
138 alloc_cdat()
139 {
140 num_cdats++;
141 if(curr_max_cdats <= num_cdats)
142 { if( (realloc((void*) cdat_buf, PTRS_IN_PAGE * 4)) == NULL)
143 perror("realloc cdat_buf failed");
144 curr_max_cdats += PTRS_IN_PAGE;
145 if( (realloc( (void*) cdat_stack, PTRS_IN_PAGE * 4)) == NULL) //increase cdat_stack also
146 perror("realloc cdat_stack failed");
147 }
148 if( (CURR_CDAT = (struct cdat*) malloc(sizeof (struct cdat)) ) == NULL )
149 perror("malloc cdat failed");
150
151 return CURR_CDAT;
152
153 }
154 struct odat*
155 alloc_odat
156 ()
157 {
158
159 num_odats++;
160 if(num_odats >= curr_max_odats)
161 { if( (realloc((void*) odat_buf, PTRS_IN_PAGE * 4)) == NULL)
162 perror("realloc odat_buf failed");
163 curr_max_odats += PTRS_IN_PAGE;
164 }
165 if( (CURR_ODAT = (struct odat*) malloc(sizeof (struct odat))) == NULL)
166 perror("malloc odat failed");
167
168 return CURR_ODAT;
169 }
170
171 void
172 alloc_vdat
173 ()
174 {
175 num_vdats++;
176 if(num_vdats >= curr_max_vdats)
177 { if( (realloc((void*) vdat_buf, PTRS_IN_PAGE * 4)) == NULL)
178 perror("realloc vdat_buf failed");
179 curr_max_vdats += PTRS_IN_PAGE;
180 }
181 if((CURR_VDAT = (struct vdat*) malloc(sizeof (struct vdat))) == NULL)
182 perror("malloc vdat failed");
183
184 }
185
186 struct link*
187 alloc_link
188 ()
189 {
190 num_links++;
191
192 if(num_links >= curr_max_links)
193 { if( (realloc((void*) link_buf, PTRS_IN_PAGE * 4)) == NULL)
194 perror("realloc vdat_buf failed");
195 curr_max_links += PTRS_IN_PAGE;
196 }
197 if((CURR_LINK = (struct link*) malloc(sizeof (struct link))) == NULL)
198 perror("malloc link failed");
199
200 return CURR_LINK;
201 }
202
203 struct ref*
204 alloc_ref
205 ()
206 {
207 num_refs++;
208
209 if(num_refs % 16 == 0)
210 { CURR_POST = CURR_REF;
211 inc_posts();
212 }
213
214 if(num_refs >= curr_max_refs)
215 { if( (realloc((void*) ref_buf, PTRS_IN_PAGE * 4)) == NULL)
216 perror("realloc ref_buf failed");
217 curr_max_refs += PTRS_IN_PAGE;
218 }
219 if((CURR_REF = (struct ref*) malloc(sizeof (struct ref))) == NULL)
220 perror("malloc ref failed");
221
222 return CURR_REF;
223 }
224
225 void
226 inc_posts()
227 {
228 if(num_posts >= curr_max_posts)
229 { if( (realloc((void*) ref_buf, PTRS_IN_PAGE * 4)) == NULL)
230 perror("realoc post_buf failed");
231 curr_max_posts += PTRS_IN_PAGE;
232 }
233 if ((CURR_POST = (struct ref*) malloc (sizeof (struct ref))) == NULL)
234 perror("malloc post failed");
235
236 }
237
238 struct cdat*
239 curr_cdat
240 ()
241 {
242 return CURR_CDAT;
243 }
244
245 struct odat*
246 curr_odat
247 ()
248 {
249 return CURR_ODAT;
250 }
251 struct vdat*
252 curr_vdat
253 ()
254 {
255 return CURR_VDAT;
256 }
257 struct set*
258 curr_set
259 ()
260 {
261 return &CURR_CDAT->CURR_SET;
262 }
263 struct ele*
264 curr_ele
265 ()
266 {
267 return &CURR_CDAT->CURR_SET.CURR_ELE;
268 }
269 struct ref*
270 prev_ref
271 ()
272 {
273 return PREV_REF;
274 }
275 struct model
276 curr_model
277 ()
278 {
279 return CURR_MODEL;
280 }