ucs4_t
[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 set*
27 curr_set(void);
28 struct ref*
29 curr_ref(void);
30 struct model*
31 curr_model(void);
32 void
33 inc_posts(void);
34
35 #define PAGES_PER_CHUNK 16
36
37 //"type free" chunk stacking
38 struct chunk_stack
39 { void* chunks[MAX_CHUNKS];
40 void* *csp; //chunk stack pointer
41 void* dsp[MAX_CHUNKS]; //dat stack pointer (per chunk)
42 int chunk_size; //size of a chunk (including its forfeited page)
43 int max_dats; //number of dats per chunk for this stack
44 } ocs, vcs, ccs, rcs, lcs, pcs; //odat, vdat, and cdat, ref, link, post stacks
45
46 //type safety handled by macro expansion (do not call these directly from code, make dependent macros for access to these)
47 #define CHUNKS_LEN(STACK) ((STACK).csp - (STACK).chunks)
48 #define CURRENT_CHUNK(STACK) ((STACK).chunks[CHUNKS_LEN(STACK) - 1])
49 #define CHUNKS_FULL(STACK) ( (STACK).csp >= \
50 (STACK).chunks + MAX_CHUNKS * (STACK).chunk_size)
51 #define CURRENT_DSP(STACK,TYPE) ((TYPE*) ((STACK).dsp[CHUNKS_LEN(STACK) - 1]))
52 #define DATA_FULL(STACK,TYPE) ((void*) CURRENT_DSP(STACK,TYPE) >= \
53 (CURRENT_CHUNK(STACK) + (STACK).chunk_size))
54 #define CSP_PUSH(STACK) (*(++(STACK).csp) = malloc((STACK).chunk_size))
55 #define CURRENT_DATP(STACK,TYPE) (((TYPE**)(STACK).dsp)[CHUNKS_LEN(STACK) - 1])
56 #define PREVIOUS_DATP(STACK,TYPE) (((TYPE**)(STACK).dsp)[CHUNKS_LEN(STACK) - 2])
57 #define ALLOC_DAT(STACK,TYPE) (++CURRENT_DATP(STACK,TYPE))
58 #define INIT_STACK(STACK,TYPE) \
59 { int i; \
60 (STACK).chunk_size = PAGES_PER_CHUNK * pagesize; \
61 (STACK).max_dats = (STACK).chunk_size / sizeof (TYPE); \
62 CSP_PUSH(STACK); \
63 for( i = 0; i < MAX_CHUNKS; i++){ \
64 (STACK).dsp[i] += pagesize; \
65 } \
66 }
67 //Stack-specific macros (called directly from code (safety enforcement)
68 #define INIT_ODAT() (INIT_STACK(ocs, struct odat))
69 #define CURRENT_ODAT() (CURRENT_DATP(ocs,struct odat))
70 #define ODAT_FULL() (DATA_FULL(ocs,struct odat))
71 #define ODAT_ALLOC() (ALLOC_DAT(ocs,struct odat))
72 #define OCS_FULL() (CHUNKS_FULL(ocs))
73 #define INIT_VDAT() (INIT_STACK(vcs, struct vdat))
74 #define CURRENT_VDAT() (CURRENT_DATP(vcs,struct vdat))
75 #define VDAT_FULL() (DATA_FULL(vcs,struct vdat))
76 #define VDAT_ALLOC() (ALLOC_DAT(vcs,struct vdat))
77 #define VCS_FULL() (CHUNKS_FULL(vcs))
78 #define INIT_CDAT() (INIT_STACK(ccs, struct cdat))
79 #define CURRENT_CDAT() (CURRENT_DATP(ccs,struct cdat))
80 #define CDAT_FULL() (DATA_FULL(ccs, struct cdat))
81 #define CDAT_ALLOC() (ALLOC_DAT(ccs, struct cdat))
82 #define CCS_FULL() (CHUNKS_FULL(ccs))
83 #define INIT_LINK() (INIT_STACK(lcs, struct link))
84 #define CURRENT_LINK() (CURRENT_DATP(lcs,struct link))
85 #define LDAT_FULL() (DATA_FULL(lcs, struct link))
86 #define LDAT_ALLOC() (ALLOC_DAT(lcs, struct link))
87 #define LCS_FULL() (CHUNKS_FULL(lcs))
88 #define INIT_POST() (INIT_STACK(rcs, struct ref))
89 #define CURRENT_POST() (CURRENT_DATP(pcs,struct ref))
90 #define POST_FULL() (DATA_FULL(pcs,struct ref))
91 #define POST_ALLOC() (ALLOC_DAT(pcs,struct ref))
92 #define PCS_FULL() (CHUNKS_FULL(pcs))
93 #define INIT_REF() (INIT_STACK(rcs, struct ref))
94 #define CURRENT_REF() (CURRENT_DATP(rcs,struct ref))
95 #define PREVIOUS_REF() (PREVIOUS_DATP(rcs, struct ref))
96 #define REF_FULL() (DATA_FULL(rcs,struct ref))
97 #define REF_ALLOC() (ALLOC_DAT(rcs,struct ref))
98 #define RCS_FULL() (CHUNKS_FULL(rcs))
99 //Metadata
100 #define CURRENT_SET() (CURRENT_CDAT()->set_list[CURRENT_CDAT()->num_sets])
101 #define CURRENT_MODEL() (CURRENT_VDAT()->model_list[CURRENT_VDAT()->num_models])
102
103
104 #define CURR_QUAD (CURR_ODAT->quad_file)
105
106 long pagesize;
107
108 int num_cdats = 0;
109
110 struct cdat* cdat_stack[MAX_CLASSES];
111 struct cdat** cdat_stackp;
112
113 int num_odats = 0;
114
115 int num_vdats = 0;
116
117 int num_refs = 0;
118 uint64_t ss_ref_id = 0x00FFFFFF; /* system space for ref_ids */
119
120 int num_posts = 0;
121
122 int num_links = 0;
123
124
125 /* The initalization function of the IR. */
126 int
127 ir_init()
128 {
129
130 char root[4] = "root";
131
132 pagesize = sysconf(_SC_PAGESIZE);
133
134 INIT_CDAT();
135 *cdat_stackp = CURRENT_CDAT();
136 memmove((*cdat_stackp)->name, root, 32);
137
138 INIT_ODAT();
139 INIT_VDAT();
140 INIT_LINK();
141 INIT_REF();
142 INIT_POST();
143
144
145 return 0;
146
147 }
148
149 void
150 ir_quit()
151 {
152 int i;
153
154 for(i = 0; i < CHUNKS_LEN(ccs) ; i++)
155 {
156 free(ccs.chunks[i]);
157 }
158 for(i = 0; i < CHUNKS_LEN(ocs); i++)
159 {
160 free(ocs.chunks[i]);
161 }
162 for(i = 0; i < CHUNKS_LEN(vcs) ; i++)
163 {
164 free(vcs.chunks[i]);
165 }
166 for(i = 0; i < CHUNKS_LEN(rcs); i++)
167 {
168 free(rcs.chunks[i]);
169 }
170 for(i = 0; i < CHUNKS_LEN(lcs); i++)
171 {
172 free(lcs.chunks[i]);
173 }
174 for(i = 0; i < CHUNKS_LEN(pcs); i++)
175 {
176 free(pcs.chunks[i]);
177 }
178
179 }
180
181 struct cdat*
182 alloc_cdat()
183 {
184 num_cdats++;
185 if(CDAT_FULL())
186 { if(CCS_FULL())
187 { fprintf(stderr, "You have allocated to many (%d) cdats ", num_cdats);
188 exit(EXIT_FAILURE);
189 }
190 else
191 CSP_PUSH(ccs);
192 }
193 else
194 CDAT_ALLOC();
195
196 return CURRENT_CDAT();
197 }
198
199 //these should probably be inline
200 struct odat*
201 alloc_odat
202 ()
203 {
204 num_odats++;
205 if(ODAT_FULL())
206 { if(!OCS_FULL())
207 { fprintf(stderr, "You have allocated to many (%d) odats ", num_odats);
208 exit(EXIT_FAILURE);
209 }
210 else
211 CSP_PUSH(ocs);
212 }
213 else
214 ODAT_ALLOC();
215
216 return CURRENT_ODAT();
217 }
218
219 void
220 alloc_vdat
221 ()
222 { num_vdats++;
223 if(VDAT_FULL())
224 { if(!VCS_FULL())
225 { fprintf(stderr, "You have allocated to many (%d) vdats ", num_vdats);
226 exit(EXIT_FAILURE);
227 }
228 else
229 CSP_PUSH(vcs);
230 }
231 else
232 VDAT_ALLOC();
233 }
234
235 struct link*
236 alloc_link
237 ()
238 { num_links++;
239 if(LDAT_FULL())
240 { if(!LCS_FULL())
241 { fprintf(stderr, "You have allocated to many (%d) links ", num_links);
242 exit(EXIT_FAILURE);
243 }
244 else
245 CSP_PUSH(lcs);
246 }
247 else
248 LDAT_ALLOC();
249
250 return CURRENT_LINK();
251
252 }
253
254 struct ref*
255 alloc_ref
256 ()
257 { num_refs++;
258 if(REF_FULL())
259 { if(!RCS_FULL())
260 { fprintf(stderr, "You have allocated to many (%d) refs ", num_refs);
261 exit(EXIT_FAILURE);
262 }
263 else
264 CSP_PUSH(rcs);
265 }
266 else
267 REF_ALLOC();
268
269
270 if(num_refs % 16 == 0)
271 { CURRENT_POST() = CURRENT_REF();
272 inc_posts();
273 }
274
275 return CURRENT_REF();
276 }
277
278 void
279 inc_posts()
280 { num_posts++;
281 if(POST_FULL())
282 { if(!PCS_FULL())
283 { fprintf(stderr, "You have allocated to many (%d) refs ", num_posts);
284 exit(EXIT_FAILURE);
285 }
286 else
287 CSP_PUSH(pcs);
288 }
289 else
290 POST_ALLOC();
291
292 }
293
294 struct cdat*
295 curr_cdat
296 ()
297 {
298 return (*cdat_stackp);
299 }
300
301 struct odat*
302 curr_odat
303 ()
304 {
305 return CURRENT_ODAT();
306 }
307 struct vdat*
308 curr_vdat
309 ()
310 {
311 return CURRENT_VDAT();
312 }
313 struct set*
314 curr_set
315 ()
316 {
317 return &CURRENT_SET();
318 }
319 struct ref*
320 curr_ref
321 ()
322 {
323 return CURRENT_REF();
324 }
325 struct model*
326 curr_model
327 ()
328 {
329 return &CURRENT_MODEL();
330 }