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