page allocation is wrong
[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 CURR_CDAT (*cdat_stackp)
36 #define CURR_SET set_list[CURR_CDAT->num_sets]
37 #define REF_IDX (num_refs % (refs_per_page * pages_per_chunk))
38 #define PREV_REF (ref_buf[num_ref_chunks] + (REF_IDX * (sizeof (struct ref)) + pagesize - (sizeof (struct ref))))
39 #define CURR_REF (ref_buf[num_ref_chunks] + (REF_IDX * (sizeof (struct ref)) + pagesize))
40 #define ODAT_IDX (num_odats % (odats_per_page * pages_per_chunk))
41 #define CURR_ODAT (odat_buf[num_odat_chunks] + (ODAT_IDX * (sizeof (struct odat)) + pagesize))
42 #define VDAT_IDX (num_vdats % (vdats_per_page * pages_per_chunk))
43 #define CURR_VDAT (vdat_buf[num_vdat_chunks] + (VDAT_IDX * (sizeof (struct vdat)) + pagesize))
44 #define CURR_MODEL (CURR_VDAT.model_list[CURR_VDAT.num_models])
45 #define CURR_LINK (link_buf[num_links])
46 #define CURR_POST (post_buf[num_posts])
47 #define CURR_QUAD (CURR_ODAT->quad_file)
48
49 long pagesize;
50
51 int pages_per_chunk = 10;
52
53 int num_cdats = 0;
54 int curr_max_cdats = PTRS_IN_PAGE;
55
56 struct cdat* cdat_buf[PTRS_IN_PAGE];
57 struct cdat* cdat_stack[PTRS_IN_PAGE];
58 struct cdat** cdat_stackp;
59
60
61 int num_odat_chunks = 0;
62 int num_odats = 0;
63 void* odat_buf[MAX_CHUNKS];
64 long odats_per_page;
65
66 int num_vdat_chunks = 0;
67 int num_vdats = 0;
68 void* vdat_buf[MAX_CHUNKS];
69 long vdats_per_page;
70
71 int num_ref_chunks = 0;
72 int num_refs = 0;
73 void* ref_buf[MAX_CHUNKS];
74 long refs_per_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 pagesize = sysconf(_SC_PAGESIZE);
107 odats_per_page = (sizeof (struct odat)/pagesize);
108 vdats_per_page = (sizeof (struct vdat)/pagesize);
109 refs_per_page = (sizeof (struct ref)/pagesize);
110
111 return 0;
112
113 }
114
115 void
116 ir_quit()
117 {
118 int i;
119
120 for(i = 0; i <= num_odats ; i++)
121 {
122 free(odat_buf[i]);
123 }
124 for(i = 0; i <= num_cdats; i++)
125 {
126 free(cdat_buf[i]);
127 }
128 for(i = 0; i <= num_vdats; i++)
129 {
130 free(vdat_buf[i]);
131 }
132 for(i = 0; i <= num_refs; i++)
133 {
134 free(ref_buf[i]);
135 }
136 for(i = 0; i<= num_links; i++)
137 {
138 free(link_buf[i]);
139 }
140
141 }
142
143 //TODO: FREE MEMORY!
144 struct cdat*
145 alloc_cdat()
146 {
147 num_cdats++;
148 if(curr_max_cdats <= num_cdats)
149 { if( (realloc((void*) cdat_buf, PTRS_IN_PAGE * 4)) == NULL)
150 perror("realloc cdat_buf failed");
151 curr_max_cdats += PTRS_IN_PAGE;
152 if( (realloc( (void*) cdat_stack, PTRS_IN_PAGE * 4)) == NULL) //increase cdat_stack also
153 perror("realloc cdat_stack failed");
154 }
155 if( (CURR_CDAT = (struct cdat*) malloc(sizeof (struct cdat)) ) == NULL )
156 perror("malloc cdat failed");
157
158 return CURR_CDAT;
159
160 }
161 struct odat*
162 alloc_odat
163 ()
164 {
165 num_odats++;
166
167 if(!(num_odats % (odats_per_page * pages_per_chunk))) //chunk is full
168 {
169 num_odat_chunks++;
170 if( ((odat_buf[num_odat_chunks] = malloc(odats_per_page * pages_per_chunk)) == NULL) )
171 perror("malloc odat chunk failed");
172 }
173
174 return CURR_ODAT;
175 }
176
177 void
178 alloc_vdat
179 ()
180 {
181 num_vdats++;
182
183 if(!(num_vdats % (vdats_per_page * pages_per_chunk))) //chunk is full
184 {
185 num_vdat_chunks++;
186 if( ((vdat_buf[num_vdat_chunks] = malloc(vdats_per_page * pages_per_chunk)) == NULL) )
187 perror("malloc vdat chunk failed");
188 }
189
190 }
191
192 struct link*
193 alloc_link
194 ()
195 {
196 num_links++;
197
198 if(num_links >= curr_max_links)
199 { if( (realloc((void*) link_buf, PTRS_IN_PAGE * 4)) == NULL)
200 perror("realloc vdat_buf failed");
201 curr_max_links += PTRS_IN_PAGE;
202 }
203 if((CURR_LINK = (struct link*) malloc(sizeof (struct link))) == NULL)
204 perror("malloc link failed");
205
206 return CURR_LINK;
207 }
208
209 struct ref*
210 alloc_ref
211 ()
212 {
213 num_refs++;
214
215 if(num_refs % 16 == 0)
216 { CURR_POST = CURR_REF;
217 inc_posts();
218 }
219 if(!(num_refs % (refs_per_page * pages_per_chunk))) //chunk is full
220 {
221 num_ref_chunks++;
222 if( ((ref_buf[num_ref_chunks] = malloc(refs_per_page * pages_per_chunk)) == NULL) )
223 perror("malloc ref chunk failed");
224 }
225
226 return CURR_REF;
227 }
228
229 void
230 inc_posts()
231 {
232 if(num_posts >= curr_max_posts)
233 { if( (realloc((void*) ref_buf, PTRS_IN_PAGE * 4)) == NULL)
234 perror("realoc post_buf failed");
235 curr_max_posts += PTRS_IN_PAGE;
236 }
237 if ((CURR_POST = (struct ref*) malloc (sizeof (struct ref))) == NULL)
238 perror("malloc post failed");
239
240 }
241
242 struct cdat*
243 curr_cdat
244 ()
245 {
246 return CURR_CDAT;
247 }
248
249 struct odat*
250 curr_odat
251 ()
252 {
253 return CURR_ODAT;
254 }
255 struct vdat*
256 curr_vdat
257 ()
258 {
259 return CURR_VDAT;
260 }
261 struct set*
262 curr_set
263 ()
264 {
265 return &CURR_CDAT->CURR_SET;
266 }
267 struct ref*
268 prev_ref
269 ()
270 {
271 return PREV_REF;
272 }
273 struct model
274 curr_model
275 ()
276 {
277 return CURR_MODEL;
278 }