#include #include #include #include #include #include #include #include struct cdat* alloc_cdat(void); struct odat* alloc_odat(void); void alloc_vdat(void); struct link* alloc_link(void); struct ref* alloc_ref(void); struct cdat* curr_cdat(void); struct odat* curr_odat(void); struct vdat* curr_vdat(void); struct set* curr_set(void); struct ref* prev_ref(void); struct model* curr_model(void); void inc_posts(void); #define PAGES_PER_CHUNK 16 #define CURR_CDAT (*cdat_stackp) //"type free" chunk stacking struct chunk_stack { void* chunks[MAX_CHUNKS]; void* csp; //chunk stack pointer void* dsp[MAX_CHUNKS]; //dat stack pointer (per chunk) int max_dats; //num dats in a chunk } ocs, vcs, ccs, rcs, lcs, pcs; //odat, vdat, and cdat, ref, link, post stacks //type safety handled by macro expansion #define CURRENT_CHUNK(STACK) ( (void*) (STACK).csp - (void*) (STACK).chunks - 1) #define CHUNKS_LEN(STACK) ( (void*) (STACK).csp - (void*) (STACK).chunks) #define CHUNK_FULL(STACK, TYPE) ( (CURRENT_DAT(STACK,TYPE) - (TYPE) CURRENT_CHUNK(STACK)) \ >= (STACK).max_dats ) #define CSP_PUSH(STACK) (++(STACK).csp = malloc(pagesize * PAGES_PER_CHUNK)) #define CURRENT_DAT(STACK,TYPE) ((TYPE) (STACK).dsp[CHUNKS_LEN(STACK)]) #define PREVIOUS_DAT(STACK,TYPE) ((TYPE) (STACK).dsp[CHUNKS_LEN(STACK)]-1) #define INCREMENT_DAT(STACK,TYPE) (++CURRENT_DAT(STACK,TYPE)) //Stack-specific macros #define CURRENT_ODAT() (CURRENT_DAT(ocs,struct odat*)) #define CURRENT_VDAT() (CURRENT_DAT(vcs,struct vdat*)) #define CURRENT_CDAT() (CURRENT_DAT(ccs,struct cdat*)) #define CURRENT_LINK() (CURRENT_DAT(lcs,struct link*)) #define CURRENT_POST() (CURRENT_DAT(pcs,struct ref*)) #define CURRENT_REF() (CURRENT_DAT(rcs,struct ref*)) #define PREVIOUS_REF() (PREVIOUS_DAT(rcs, struct ref*)) //Metadata #define CURRENT_SET() (CURRENT_CDAT()->set_list[CURRENT_CDAT()->num_sets]) #define CURRENT_MODEL() (CURRENT_VDAT()->model_list[CURRENT_VDAT()->num_models]) #define CURR_QUAD (CURR_ODAT->quad_file) long pagesize; int pages_per_chunk = 10; int num_cdats = 0; int curr_max_cdats = PTRS_IN_PAGE; struct cdat* cdat_buf[PTRS_IN_PAGE]; struct cdat* cdat_stack[PTRS_IN_PAGE]; struct cdat** cdat_stackp; int num_odats = 0; int num_vdats = 0; int num_refs = 0; uint64_t ss_ref_id = 0x00FFFFFF; /* system space for ref_ids */ int num_posts = -1; int curr_max_posts = PTRS_IN_PAGE; struct ref* post_buf[PTRS_IN_PAGE]; int num_links = -1; int curr_max_links = PTRS_IN_PAGE; struct link* link_buf[PTRS_IN_PAGE]; /* The initalization function of the IR. */ int ir_init() { /* Init root cdat and stack */ char root[4] = "root"; if( (cdat_buf[num_cdats] = (struct cdat*) malloc(sizeof(struct cdat))) == NULL) { perror("malloc root class failed\n"); return -1; } cdat_buf[num_cdats]->idx = num_cdats; memmove(cdat_buf[num_cdats]->name, root, 4); cdat_stackp = cdat_stack; *cdat_stackp++ = cdat_buf[num_cdats++]; pagesize = sysconf(_SC_PAGESIZE); return 0; } void ir_quit() { int i; for(i = 0; i <= num_odats ; i++) { } for(i = 0; i <= num_cdats; i++) { } for(i = 0; i <= num_vdats; i++) { } for(i = 0; i <= num_refs; i++) { } for(i = 0; i<= num_links; i++) { } for(i = 0; i<= num_posts; i++) { } } //TODO: FREE MEMORY! struct cdat* alloc_cdat() { num_cdats++; if(curr_max_cdats <= num_cdats) { if( (realloc((void*) cdat_buf, PTRS_IN_PAGE * 4)) == NULL) perror("realloc cdat_buf failed"); curr_max_cdats += PTRS_IN_PAGE; if( (realloc( (void*) cdat_stack, PTRS_IN_PAGE * 4)) == NULL) //increase cdat_stack also perror("realloc cdat_stack failed"); } if( (CURR_CDAT = (struct cdat*) malloc(sizeof (struct cdat)) ) == NULL ) perror("malloc cdat failed"); return CURR_CDAT; } struct odat* alloc_odat () { if(CHUNK_FULL(ocs, struct odat*)) CSP_PUSH(ocs); else INCREMENT_DAT(ocs, struct odat*); return CURRENT_ODAT(); } void alloc_vdat () { num_vdats++; if(CHUNK_FULL(vcs, struct vdat*)) CSP_PUSH(vcs); else INCREMENT_DAT(vcs, struct vdat*); } struct link* alloc_link () { num_links++; if(CHUNK_FULL(lcs, struct link*)) CSP_PUSH(lcs); else INCREMENT_DAT(lcs, struct link*); return CURRENT_LINK(); } struct ref* alloc_ref () { num_refs++; if(CHUNK_FULL(rcs, struct link*)) CSP_PUSH(rcs); else INCREMENT_DAT(rcs, struct link*); if(num_refs % 16 == 0) { CURRENT_POST() = CURRENT_REF(); inc_posts(); } return CURRENT_REF(); } void inc_posts() { num_posts++; if(CHUNK_FULL(pcs, struct ref*)) {CSP_PUSH(pcs);} else INCREMENT_DAT(pcs, struct ref*); } struct cdat* curr_cdat () { return CURR_CDAT; } struct odat* curr_odat () { return CURRENT_ODAT(); } struct vdat* curr_vdat () { return CURRENT_VDAT(); } struct set* curr_set () { return &CURRENT_SET(); } struct ref* prev_ref () { return PREVIOUS_REF(); } struct model* curr_model () { return &CURRENT_MODEL(); }