X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=blobdiff_plain;f=src%2Fir.c;h=706f677fcbe46ed44fa107d6f955e4110bc2c866;hp=0f8a46160f641c39ee696874e8fc041ba80fb23a;hb=ee18200a9d3817728d6d09745cd29900649d4508;hpb=6035780f8e8c7ba47ae517ad6cf7e47515e847fd diff --git a/src/ir.c b/src/ir.c index 0f8a461..706f677 100644 --- a/src/ir.c +++ b/src/ir.c @@ -14,12 +14,19 @@ #include //u8_* functions #include //uint8_t as a char #include //u32_cpy +#include //ulc_fprintf /* Local */ +#include "print.h" #include "apc.h" #include "ir.h" +#undef do_error +#define do_error(...) exit(-1) +#define XXH_PRIVATE_API +#include "../xxHash/xxhash.h" /* Public */ int ir_init(void); void ir_quit(void); +void ir_test(void); int ir_linker(void); int ir_condenser(void); /* Memory allocation structures */ @@ -36,6 +43,13 @@ struct pagelist_t { struct pagenode_t* root, * head; size_t pagesize; }; +#define SYS_PAGESIZE (sys_pagesize) +#define NAME_PAGESIZE (APC_NAME_MAX * 1024) +#define PL_HEADERSIZE (sizeof(struct pagenode_header_t)) +#define PL_HEADSIZE(_PL) (_PL.head->header.head - _PL.head->root) +#define PL_HEADMEM(_PL) (_PL.pagesize - PL_HEADERSIZE - PL_HEADSIZE(_PL)) +/* Set data mem */ +enum dtype { FSDAT, MSDAT, ADAT, LDAT, FBDAT }; struct ir_namelist_t; struct ir_namelist_t { struct ir_namelist_t* nextsib; @@ -50,7 +64,6 @@ struct ir_setld_t long long ref; struct ir_namelist_t* namelist, * namelist_head; }; -/* Set data mem */ struct ir_setdata_header_t { enum dtype type; uint8_t* src_filename, * data_name; @@ -90,7 +103,7 @@ struct ir_class_t struct ir_set_t { struct ir_set_t* nextchild, * nextsib; struct ir_class_t* class; - long long ref; + uint32_t ref; uint8_t* name; struct ir_framebox_t* frameboxes; struct ir_simplex_t* audio; @@ -98,108 +111,91 @@ struct ir_set_t }; /* Functions */ static inline -int init_pagelist(struct pagelist_t*,size_t); +struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*,const uint8_t*); static inline -struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*, uint8_t*); -static -void ir_free_pagenodes(struct pagenode_t*); +union ir_setdata_t* ir_framedata (enum dtype,const uint8_t*,apc_facing,int,int); +static inline +int bytes_identical(const uint8_t*,const uint8_t*); static inline -int bytes_identical(uint8_t*,uint8_t*); +int classnames_identical(const uint8_t*,const uint8_t*); static -void* stack_alloc(size_t); +void* stack_alloc(size_t); +#define struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T))) static -uint8_t* name_alloc(uint8_t*); +uint8_t* name_alloc(const uint8_t*); +static +uint8_t* classname_alloc(const uint8_t*); static inline -union ir_setdata_t* ir_framedata (enum dtype,uint8_t*,apc_facing,int,int); -/* Function-Like Macros */ -#define do_warn() do { \ - } while (0) -#define wprint(str) do { \ - fprintf(stderr, str); \ - do_warn(); \ +void* pagelist_pop(struct pagelist_t*,size_t); +#define pagelist_alloc(pagelist) do { \ + pagelist.head->header.next = (struct pagenode_t*) calloc(pagelist.pagesize,1); \ + if (pagelist.head->header.next == NULL) \ + eprintf("Memory allocation error\n"); \ + pagelist.head = pagelist.head->header.next; \ + pagelist.head->header.head = pagelist.head->root; \ } while (0) -#define wprintf(fmt,...) do { \ - fprintf(stderr, fmt, __VA_ARGS__); \ - do_warn(); \ +#define pagelist_init(pagelist,size) do { \ + pagelist.pagesize = size; \ + pagelist.root = (struct pagenode_t*) calloc(size,1); \ + if (pagelist.root == NULL) \ + eprintf("Memory allocation error\n"); \ + pagelist.root->header.head = pagelist.root->root; \ + pagelist.head = pagelist.root; \ } while (0) -#define do_error() do { \ - exit(-1); \ - } while (0) -#define eprint(str) do { \ - fprintf(stderr, str); \ - do_error(); \ - } while (0) -#define eprintf(fmt,...) do { \ - fprintf(stderr, fmt, __VA_ARGS__); \ - do_error(); \ - } while (0) -#define struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T))) -#define DATA_PAGESIZE (sys_pagesize) -#define NAME_PAGESIZE (APC_NAME_MAX * 1024) -#define PL_HEADERSIZE (sizeof(struct pagenode_header_t)) -#define PL_HEADSIZE(_PL) (_PL.head->header.head - _PL.head->root) -#define PL_HEADMEM(_PL) (_PL.pagesize - PL_HEADERSIZE - PL_HEADSIZE(_PL)) -/* Memory */ +static +void pagenode_free(struct pagenode_t*); +#define REFHASH(ref) (XXH32(&ref, sizeof(uint32_t), 0xCEED) & 0xCFF) + extern //apc.c -long sys_pagesize; +long sys_pagesize; static -struct pagelist_t datapages, namepages; +struct pagelist_t datapages, namepages, refhashpages; static struct ir_class_t root_class = { .name = (uint8_t*)"." }; /* Init */ int ir_init ( void ) -{ if (init_pagelist(&datapages, (size_t)DATA_PAGESIZE)) - eprint("Memory allocation error\n"); - if (init_pagelist(&namepages, (size_t)NAME_PAGESIZE)) - eprint("Memory allocation error\n"); - return 0; -} - -static inline -int init_pagelist -( struct pagelist_t* pl, - size_t size -) -{ pl->pagesize = size; - pl->root = (struct pagenode_t*) calloc(size,1); - if (pl->root == NULL) - return -1; - pl->root->header.head = pl->root->root; - pl->head = pl->root; +{ pagelist_init(datapages, (size_t)SYS_PAGESIZE); + pagelist_init(namepages, (size_t)NAME_PAGESIZE); + pagelist_init(refhashpages, (size_t)SYS_PAGESIZE); return 0; } -/* Quit/Cleanup - Recursively clean pagenode linked list -*/ +/* Quit/Cleanup */ void ir_quit ( void ) -{ ir_free_pagenodes(datapages.root); - ir_free_pagenodes(namepages.root); +{ pagenode_free(datapages.root); + pagenode_free(namepages.root); + pagenode_free(refhashpages.root); } +/* Recursively clean pagenode linked list, freeing last first */ static -void ir_free_pagenodes +void pagenode_free ( struct pagenode_t* pagenode ) { if (pagenode->header.next != NULL) - ir_free_pagenodes(pagenode->header.next); + pagenode_free(pagenode->header.next); free(pagenode); } -/* Link -*/ +/* Link */ int ir_linker ( void ) -{ return 0; } +{ + return 0; +} -/* Condense -*/ +/* Condense */ int ir_condenser ( void ) { return 0; } +/* Return the class's name string */ +uint8_t* ir_class_name +( struct ir_class_t* class ) +{ return class->name; } + /* Return a pointer to the root class */ struct ir_class_t* ir_class_root ( void ) @@ -211,22 +207,27 @@ struct ir_class_t* ir_class_root */ struct ir_class_t* ir_class_addchild ( struct ir_class_t* class, - uint8_t* name + const uint8_t* name ) { struct ir_class_t* iter; if (class->nextchild == NULL) - return class->nextchild = struct_alloc(ir_class_t); + goto alloc; iter = class->nextchild; + if (iter->name == NULL) + eprintf("Null name pointer in class %p\n", iter); + if (name == NULL) + eprintf("Null child added to class %s\n", iter->name); check: - if (bytes_identical(iter->name, name)) + if (classnames_identical(iter->name, name)) return iter; if (iter->nextsib != NULL) { iter = iter->nextsib; goto check; } + alloc: iter = struct_alloc(ir_class_t); iter->nextsib = class->nextchild; - iter->name = name_alloc(name); + iter->name = classname_alloc(name); return class->nextchild = iter; } @@ -236,12 +237,16 @@ struct ir_class_t* ir_class_addchild */ struct ir_set_t* ir_class_addset ( struct ir_class_t* class, - uint8_t* name + const uint8_t* name ) { struct ir_set_t* iter; if (class->root_set == NULL) - return class->root_set = struct_alloc(ir_set_t); + goto alloc; iter = class->root_set; + if (iter->name == NULL) + eprintf("Null name pointer in class %p\n", iter); + if (name == NULL) + eprintf("Null set added to class %U\n", iter->name); check: if (bytes_identical(iter->name, name)) return iter; @@ -249,24 +254,43 @@ struct ir_set_t* ir_class_addset { iter = iter->nextsib; goto check; } + alloc: iter = struct_alloc(ir_set_t); iter->nextsib = class->root_set; iter->name = name_alloc(name); return class->root_set = iter; } +struct ir_set_t* ir_set_from_ref +( uint32_t ref ) +{ uint16_t hash; + struct ir_set_t** iters; + struct pagenode_t* iterp; + iterp = refhashpages.root; + hash = REFHASH(ref); + do + iters = ((struct ir_set_t**) iterp->root) + hash; + while (*iters != NULL && (*iters)->ref != ref && (iterp = iterp->header.next) != NULL); + return *iters; +} + + /* Add a set to a set Attempts to create a new subset of the specified set, returning the child if it already exists */ struct ir_set_t* ir_set_addchild ( struct ir_set_t* set, - uint8_t* name + const uint8_t* name ) { struct ir_set_t* iter; if (set->nextchild == NULL) - return set->nextchild = struct_alloc(ir_set_t); + goto alloc; iter = set->nextchild; + if (iter->name == NULL) + eprintf("Null name pointer in set %p\n", iter); + if (name == NULL) + eprintf("Null child added to set %s\n", iter->name); check: if (bytes_identical(iter->name, name)) return iter; @@ -274,6 +298,7 @@ struct ir_set_t* ir_set_addchild { iter = iter->nextsib; goto check; } + alloc: iter = struct_alloc(ir_set_t); iter->nextsib = set->nextchild; iter->name = name_alloc(name); @@ -287,11 +312,11 @@ struct ir_set_t* ir_set_addchild static inline struct ir_framebox_t* ir_set_add_framebox ( struct ir_set_t* set, - uint8_t* name + const uint8_t* name ) { struct ir_framebox_t* iter; if (set->frameboxes == NULL) - return set->frameboxes = struct_alloc(ir_framebox_t); + goto alloc; iter = set->frameboxes; check: if (bytes_identical(iter->header.data_name, name)) @@ -300,6 +325,7 @@ struct ir_framebox_t* ir_set_add_framebox { iter = (struct ir_framebox_t*) iter->header.nextsib; goto check; } + alloc: iter = struct_alloc(ir_framebox_t); iter->header.nextsib = (union ir_setdata_t*) set->frameboxes; iter->header.data_name = name_alloc(name); @@ -311,20 +337,31 @@ struct ir_framebox_t* ir_set_add_framebox */ static inline int bytes_identical -( uint8_t* stra, - uint8_t* strb +( const uint8_t* stra, + const uint8_t* strb ) { int ca, cb; do { ca = *stra++; cb = *strb++; - } while (ca && ca == cb); + } while (ca && ca != '_' && ca == cb); return (ca == cb); } -/* Assign Setdata to Set +static inline +int classnames_identical +( const uint8_t* stra, + const uint8_t* strb +) +{ int ca, cb; + do { + ca = *stra++; + cb = *strb++; + } while (ca && ca == cb); + return (ca == cb); +} - */ +/* Assign Setdata to Set */ void ir_set_assign_data ( struct ir_set_t* set, union ir_setdata_t* setdata @@ -376,50 +413,78 @@ void ir_set_assign_data void ir_set_assign_ref ( struct ir_set_t* set, - long long ref + uint32_t ref ) -{ if (set->ref != 0) - wprintf("Ref override: 0x%lx -> 0x%lx for set %s\n", - (long unsigned) set->ref, (long unsigned) ref, set->name); +{ uint16_t hash, oldhash; + struct ir_set_t** iters; + struct pagenode_t* iterp; + uint32_t oldref; + oldref = set->ref; + oldhash = 0; + hash = REFHASH(ref); + iterp = refhashpages.root; + check_depth: + iters = ((struct ir_set_t**) iterp->root) + hash; + if (*iters == NULL || *iters == set) + *iters = set; + else + { if (iterp->header.next == NULL) + pagelist_alloc(refhashpages); + iterp = iterp->header.next; + goto check_depth; + } + if (oldref != 0) + { wprintf("Ref override: 0x%x -> 0x%x for set %s\n", oldref, ref, set->name); + if (oldhash != 0) + *iters = NULL; + else + { oldhash = hash; + hash = REFHASH(oldref); + goto check_depth; + } + } set->ref = ref; - //TODO: reflist_add(set); } void ir_data_assign_path ( union ir_setdata_t* setdata, - uint8_t* path + const uint8_t* path ) -{ if (setdata->header.src_filename != NULL) +{ if (path == NULL) + eprintf("Null path in data %s\n", setdata->header.data_name); + if (setdata->header.src_filename != NULL) wprintf("Path override: %s -> %s for setdata %s\n", setdata->header.src_filename, path, setdata->header.data_name); setdata->header.src_filename = name_alloc(path); } union ir_setdata_t* ir_framesheet -( uint8_t* name, - apc_facing d, - int width, - int height +( const uint8_t* name, + apc_facing d, + int width, + int height ) { return ir_framedata(FSDAT, name, d, width, height); } union ir_setdata_t* ir_mapsheet -( uint8_t* name, - apc_facing d, - int width, - int height +( const uint8_t* name, + apc_facing d, + int width, + int height ) { return ir_framedata(MSDAT, name, d, width, height); } static inline union ir_setdata_t* ir_framedata -( enum dtype type, - uint8_t* name, - apc_facing d, - int width, - int height +( enum dtype type, + const uint8_t* name, + apc_facing d, + int width, + int height ) { struct ir_framedata_t* framedata = struct_alloc(ir_framedata_t); + if (name == NULL) + eprintf("Null name in set allocation\n"); framedata->header.type = type; framedata->header.data_name = name_alloc(name); framedata->frameinfo.facing = d; @@ -429,8 +494,10 @@ union ir_setdata_t* ir_framedata } union ir_setdata_t* ir_audio -( uint8_t* name ) +( const uint8_t* name ) { struct ir_simplex_t* audio = struct_alloc(ir_simplex_t); + if (name == NULL) + eprintf("Null audio\n"); audio->header.type = ADAT; audio->header.data_name = name_alloc(name); return (union ir_setdata_t*) audio; @@ -441,13 +508,15 @@ union ir_setdata_t* ir_audio struct ir_classld_t* ir_classld_from_class ( struct ir_class_t* class ) { struct ir_classld_t* classld; + if (class == NULL) + eprintf("Null class in classld\n"); classld = struct_alloc(ir_classld_t); classld->root_class = class; return classld; } struct ir_setld_t* ir_setld_from_ref -( long long ref ) +( uint32_t ref ) { struct ir_setld_t* setld; setld = struct_alloc(ir_setld_t); setld->ref = ref; @@ -456,7 +525,7 @@ struct ir_setld_t* ir_setld_from_ref struct ir_setld_t* ir_setld_from_classld ( struct ir_classld_t* classld, - uint8_t* name + const uint8_t* name ) { struct ir_setld_t* setld; setld = struct_alloc(ir_setld_t); @@ -469,7 +538,7 @@ struct ir_setld_t* ir_setld_from_classld struct ir_setld_t* ir_setld_addchild ( struct ir_setld_t* setld, - uint8_t* name + const uint8_t* name ) { if (setld->namelist == NULL) { setld->namelist = struct_alloc(ir_namelist_t); @@ -486,7 +555,7 @@ struct ir_setld_t* ir_setld_addchild union ir_setdata_t* ir_link ( enum ltype link_type, struct ir_setld_t* setld, - uint8_t* name + const uint8_t* name ) { struct ir_link_t* link; link = struct_alloc(ir_link_t); @@ -499,41 +568,108 @@ union ir_setdata_t* ir_link return (union ir_setdata_t*) link; } +static inline +void* pagelist_pop +( struct pagelist_t* pagelist, + size_t size +) +{ size_t headsize = PL_HEADSIZE((*pagelist)); + if (!headsize) + { free(pagelist->head); + pagelist->head = pagelist->root; + while (pagelist->head->header.next != NULL) + pagelist->head = pagelist->head->header.next; + } + if (headsize < size) + eprintf("Attempted to pop unaligned value from pagelist\n"); + pagelist->head->header.head -= size; + return pagelist->head->header.head; +} static void* stack_alloc ( size_t bytes ) -{ if (!bytes) //valid behavior to attain current head - return datapages.head->header.head; - if (PL_HEADMEM(datapages) < bytes) - { datapages.head->header.next = (struct pagenode_t*) calloc(datapages.pagesize,1); - if (datapages.head->header.next == NULL) - eprint("Memory allocation error \n"); - datapages.head = datapages.head->header.next; - datapages.head->header.head = datapages.head->root; - } +{ if (PL_HEADMEM(datapages) < bytes) + pagelist_alloc(datapages); datapages.head->header.head += bytes; return (void*) datapages.head->header.head - bytes; } static uint8_t* name_alloc -( uint8_t* name_src ) -{ uint8_t* iter, * name; - int head_mem; +( const uint8_t* name_src ) +{ const uint8_t* iter; + uint8_t* name; + int head_mem; + copy: + name = (uint8_t*)namepages.head->header.head; + iter = name_src; + for (head_mem = PL_HEADMEM(namepages); *iter && *iter != '_' && head_mem; head_mem--) + *(namepages.head->header.head)++ = *iter++; + if (head_mem == 0) //not enough room + { pagelist_alloc(namepages); + goto copy; + } + *(namepages.head->header.head)++ = '\0'; + return name; +} + +static +uint8_t* classname_alloc +( const uint8_t* name_src ) +{ const uint8_t* iter; + uint8_t* name; + int head_mem; copy: name = (uint8_t*)namepages.head->header.head; iter = name_src; for (head_mem = PL_HEADMEM(namepages); *iter && head_mem; head_mem--) *(namepages.head->header.head)++ = *iter++; if (head_mem == 0) //not enough room - { namepages.head->header.next = (struct pagenode_t*) calloc(namepages.pagesize,1); - if (namepages.head->header.next == NULL) - eprint("Memory allocation error\n"); - namepages.head = namepages.head->header.next; - namepages.head->header.head = namepages.head->root; + { pagelist_alloc(namepages); goto copy; } *(namepages.head->header.head)++ = '\0'; return name; } + +#define pdepth() do { for (i = 0; i < depth * 2; i++) putchar(' '); } while (0) +void ir_test(void) +{ struct ir_class_t* class, * classbuf; + struct ir_set_t* set, * set_child; + int depth, i, olddepth; + depth = olddepth = 0; + classbuf = NULL; + for(class = ir_class_root(); class != NULL; class = class->nextchild) + {handle_class: + pdepth(); + uprintf("%U\n",class->name); + old_depth = depth; + set = class->root_set; + handle_set: + while(set != NULL) + { pdepth();uprintf("|"); + pdepth(); + uprintf("-[%U]",set->name); + set_child = set->nextchild; + while (set_child != NULL) + { depth++; + uprintf("--[%U]",set_child->name); + set_child = set_child->nextchild; + } + uprintf("\n"); + set = set->nextsib; + } + if (class->nextsib != NULL) + { if (classbuf == NULL) + classbuf = class; + class = class->nextsib; + goto handle_class; + } + if (classbuf != NULL) + class = classbuf; + classbuf = NULL; + uprintf("\n"); + depth = olddepth + 1; + } +}