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