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