2 \brief IR Memory Implementation
3 \details Intermediary memory management
6 ----------------------------------------------------------------------------*/
9 #include <stdint.h> //uint64_t
10 #include <string.h> //memmove
11 #include <stdlib.h> //malloc
14 #define CURR_CDAT (*cdat_stackp)
15 #define CURR_SET set_list[CURR_CDAT->num_sets]
16 #define CURR_ELE ele_list[CURR_CDAT->CURR_SET.num_ele]
17 #define PREV_REF (ref_buf[num_refs-1])
18 #define CURR_REF (ref_buf[num_refs])
19 #define PREV_ODAT (odat_buf[num_odats-1])
20 #define CURR_ODAT (odat_buf[num_odats])
21 #define CURR_VDAT (vdat_buf[num_vdats])
22 #define PREV_VDAT (vdat_buf[num_vdats-1])
23 #define CURR_MODEL model_list[CURR_VDAT->num_models]
24 #define CURR_LINK (link_buf[num_links])
25 #define CURR_POST (post_buf[num_posts])
28 int curr_max_cdats
= PTRS_IN_PAGE
;
31 int curr_max_odats
= PTRS_IN_PAGE
;
35 int curr_max_vdats
= PTRS_IN_PAGE
;
38 int curr_max_refs
= PTRS_IN_PAGE
;
39 uint64_t ss_ref_id
= 0x00FFFFFF; /* system space for ref_ids */
42 int curr_max_links
= PTRS_IN_PAGE
;
45 int curr_max_posts
= PTRS_IN_PAGE
;
50 /* Init root cdat and stack */
51 char root
[4] = "root";
53 cdat_buf
[num_cdats
] = (struct cdat
*) malloc(sizeof(struct cdat
) );
54 cdat_buf
[num_cdats
]->idx
= 0;
55 memmove(cdat_buf
[num_cdats
]->name
, root
, 4);
57 cdat_stackp
= cdat_stack
;
58 *cdat_stackp
= cdat_buf
[num_cdats
++];
61 if( (CURR_ODAT
= (struct odat
*) malloc(sizeof(struct odat
))) == NULL
)
62 perror("malloc first odat failed");
65 if( (CURR_VDAT
= (struct vdat
*) malloc(sizeof(struct vdat
))) == NULL
)
66 perror("malloc first vdat failed");
69 if( (CURR_REF
= (struct ref
*) malloc(sizeof(struct ref
))) == NULL
)
70 perror("malloc first ref failed");
73 if( (CURR_LINK
= (struct link
*) malloc(sizeof(struct link
))) == NULL
)
74 perror("malloc first link failed");
77 if( (CURR_POST
= (struct ref
*) malloc(sizeof(struct ref
))) == NULL
)
78 perror("malloc first post failed");
86 if(curr_max_cdats
<= num_cdats
)
88 if( (realloc((void*) cdat_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
89 perror("realloc cdat_buf failed");
90 curr_max_cdats
+= PTRS_IN_PAGE
;
91 if( (realloc( (void*) cdat_stack
, PTRS_IN_PAGE
* 4)) == NULL
) //increase cdat_stack also
92 perror("realloc cdat_stack failed");
94 if( (cdat_buf
[num_cdats
] = (struct cdat
*) malloc(sizeof (struct cdat
)) ) == NULL
)
95 perror("malloc cdat failed");
100 /* Dynamically allocate memory for a class data structure,
101 or cdat, after a class has been identified in a grammar.
102 We also create a stack of class pointers so that
103 we can access the cdat during processing of that
104 cdats sets and elements, a requirement because the
105 nature of recursive classes prevents us from accessing
106 the cdat based on the previous index into cdat_buf,
107 which is a list of all allocated cdats*/
109 push_cdat(char* name
)
114 memmove(cdat_buf
[num_cdats
]->name
, name
, 32);
115 cdat_buf
[num_cdats
]->idx
= num_cdats
;
117 /* Set the cdat as a class of the previous cdat */
118 (*cdat_stackp
)->class_list
[(*cdat_stackp
)->num_classes
++] = cdat_buf
[num_cdats
];
120 /* Push the cdat onto the cdat_stack */
121 *++cdat_stackp
= cdat_buf
[num_cdats
++];
136 if(num_posts
>= curr_max_posts
)
138 if( (realloc((void*) post_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
139 perror("realloc post_buf failed");
140 curr_max_posts
+= PTRS_IN_PAGE
;
142 if( (CURR_POST
= (struct ref
*) malloc(sizeof (struct ref
))) == NULL
)
144 perror("malloc post failed");
152 if(num_odats
>= curr_max_odats
)
154 if( (realloc((void*) odat_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
155 perror("realloc odat_buf failed");
156 curr_max_odats
+= PTRS_IN_PAGE
;
158 if( (CURR_ODAT
= (struct odat
*) malloc(sizeof (struct odat
))) == NULL
)
160 perror("malloc odat failed");
169 if(num_vdats
>= curr_max_vdats
)
171 if( (realloc((void*) vdat_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
172 perror("realloc vdat_buf failed");
173 curr_max_vdats
+= PTRS_IN_PAGE
;
175 if((CURR_VDAT
= (struct vdat
*) malloc(sizeof (struct vdat
))) == NULL
)
177 perror("malloc vdat failed");
186 if(num_links
>= curr_max_links
)
188 if( (realloc((void*) link_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
189 perror("realloc vdat_buf failed");
190 curr_max_links
+= PTRS_IN_PAGE
;
192 if((CURR_LINK
= (struct link
*) malloc(sizeof (struct link
))) == NULL
)
194 perror("malloc link failed");
202 if(num_refs
% 16 == 0)
204 CURR_POST
= CURR_REF
;
209 if(num_refs
>= curr_max_refs
)
211 if( (realloc((void*) ref_buf
, PTRS_IN_PAGE
* 4)) == NULL
)
212 perror("realloc ref_buf failed");
213 curr_max_refs
+= PTRS_IN_PAGE
;
215 if((CURR_REF
= (struct ref
*) malloc(sizeof (struct ref
))) == NULL
)
216 perror("malloc ref failed");
220 insert_set_label(char* name
, uint64_t ref_id
)
224 memmove(CURR_CDAT
->CURR_SET
.name
,name
,32);
225 memmove(&CURR_CDAT
->CURR_SET
.ref_id
,&ref_id
,64);
229 insert_set_olink(uint64_t ref_id
)
231 CURR_CDAT
->CURR_SET
.cdat_idx
= CURR_CDAT
->idx
;
232 CURR_CDAT
->CURR_SET
.ref_id
= ref_id
; /* Will be resolved to offset
233 when link is processed */
235 CURR_LINK
->link_t
.olink
.ref_id
= ref_id
;
236 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
237 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
++;
238 CURR_LINK
->ele_idx
= -1;
244 insert_set_vlink(uint64_t ref_id
, char* anim_name
)
246 /* Insert vlink into link_stack so that it gets processed at
248 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
249 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
251 CURR_LINK
->link_t
.vlink
.ref_id
= ref_id
;
252 memmove(CURR_LINK
->link_t
.vlink
.anim_name
, anim_name
, 32);
258 insert_set_svlink(uint64_t ref_id
)
261 /* Insert vlink into link_stack so that it gets processed at
263 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
264 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
266 CURR_LINK
->link_t
.svlink
.ref_id
= ref_id
;
270 /* At the point of reducing to a set, most of the
271 sets odat information has already been populated
272 during the reduction of its right hand side
273 non terminals (hitbox, root, quad_list). */
279 ref_id
= CURR_CDAT
->CURR_SET
.ref_id
;
281 CURR_CDAT
->CURR_SET
.cdat_idx
= CURR_CDAT
->idx
;
282 memmove(CURR_ODAT
->name
, CURR_CDAT
->CURR_SET
.name
, 32);
283 CURR_CDAT
->num_sets
++;
285 CURR_ODAT
->cdat_idx
= CURR_CDAT
->idx
;
286 CURR_ODAT
->refp
= CURR_REF
;
289 CURR_REF
->lastref
= PREV_REF
;
290 PREV_REF
->nextref
= CURR_REF
;
291 CURR_REF
->odatp
= CURR_ODAT
;
294 if(ref_id
== -1) /* user did not define a ref_id so */
295 { /* use a ref_id from system_space */
300 CURR_REF
->ref_id
= ref_id
;
311 PREV_ODAT
->vdat_id
= num_vdats
; //NULL for vlink, svlink
315 /* Populates both the odat name and ref_id
318 insert_ele_label(char* name
, uint64_t ref_id
)
320 memmove(CURR_CDAT
->CURR_SET
.CURR_ELE
.name
, name
, 32);
321 memmove(&CURR_CDAT
->CURR_SET
.ele_list
[CURR_CDAT
->CURR_SET
.ref_id
].ref_id
, &ref_id
, 64);
325 insert_ele_olink(uint64_t ref_id
)
327 CURR_CDAT
->CURR_SET
.CURR_ELE
.cdat_idx
= CURR_CDAT
->idx
;
328 CURR_CDAT
->CURR_SET
.CURR_ELE
.ref_id
= ref_id
; /* Will be resolved to offset
329 when link is processed */
331 CURR_LINK
->link_t
.olink
.ref_id
= ref_id
;
332 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
333 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
++;
334 CURR_LINK
->ele_idx
= CURR_CDAT
->CURR_SET
.num_ele
++;
338 insert_ele_vlink(uint64_t ref_id
, char* anim_name
)
341 /* Insert vlink into link_stack so that it gets processed at
343 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
345 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
346 CURR_LINK
->ele_idx
= CURR_CDAT
->CURR_SET
.num_ele
;
347 CURR_LINK
->link_t
.vlink
.ref_id
= ref_id
;
348 memmove(CURR_LINK
->link_t
.vlink
.anim_name
, anim_name
, 32);
353 insert_ele_svlink(uint64_t ref_id
)
356 CURR_LINK
->cdat_idx
= CURR_CDAT
->idx
;
358 CURR_LINK
->set_idx
= CURR_CDAT
->num_sets
;
359 CURR_LINK
->ele_idx
= CURR_CDAT
->CURR_SET
.num_ele
;
360 CURR_LINK
->link_t
.svlink
.ref_id
= ref_id
;
364 //Insert element into odat_buf and cdatpages
371 ref_id
= CURR_CDAT
->CURR_SET
.CURR_ELE
.ref_id
;
373 CURR_CDAT
->CURR_SET
.CURR_ELE
.cdat_idx
= CURR_CDAT
->idx
;
374 memmove(CURR_ODAT
->name
,CURR_CDAT
->CURR_SET
.CURR_ELE
.name
, 32);
375 CURR_CDAT
->CURR_SET
.num_ele
++;
377 CURR_ODAT
->cdat_idx
= CURR_CDAT
->idx
;
378 CURR_ODAT
->refp
= CURR_REF
;
380 if(ref_id
== -1) /* user did not define a ref_id so */
381 { /* use a ref_id from system_space */
386 CURR_REF
->ref_id
= ref_id
;
394 insert_framesheet(char direction
, char* name
, uint64_t ref_id
, int height
, int width
, int num_frames
)
396 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].height
= height
;
397 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].width
= width
;
398 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].num_frames
= num_frames
;
399 CURR_VDAT
->num_models
++;
402 #define CURR_QUAD (CURR_ODAT->quad_list[CURR_ODAT->num_quads])
404 insert_quad(int x
, int y
, int z
, uint64_t ref_id
)
409 CURR_QUAD
.ref_id
= ref_id
;
410 CURR_ODAT
->num_quads
++;
413 /* Inserting the hitbox into the set
414 odat. Elements that don't have
415 a hitbox will use the sets root. */
417 insert_hitbox(int hitbox
)
419 CURR_ODAT
->hitbox
= hitbox
;
422 /* Inserting the root into the set
423 odat. Elements that don't have
424 a root will use the sets root. */
426 insert_root(int x
, int y
, int z
)
429 CURR_ODAT
->root
.x
= x
;
430 CURR_ODAT
->root
.y
= y
;
431 CURR_ODAT
->root
.z
= z
;
435 insert_frame_pointer(char direction
, void* frame
)
437 CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].frames
[CURR_VDAT
->CURR_MODEL
.spritesheet
[(int)direction
].num_frames
++] = frame
;