X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=blobdiff_plain;f=src%2Fir.c;h=a175ef3fdfd93e7c47b02f332a4ac3f4e2cfef67;hp=0e524590240176ad0446a11d637546ef126876c6;hb=7dae7dc73dfbabdc895a78738f550f56561da644;hpb=56b5678e46687b800dfbec7291f6860f357a7653 diff --git a/src/ir.c b/src/ir.c index 0e52459..a175ef3 100644 --- a/src/ir.c +++ b/src/ir.c @@ -14,9 +14,13 @@ #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) /* Public */ int ir_init(void); void ir_quit(void); @@ -34,11 +38,15 @@ struct pagenode_t { }; struct pagelist_t { struct pagenode_t* root, * head; - size_t page_size; + size_t pagesize; }; -/* Set link data */ +#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)) +/* Set data mem */ enum dtype { FSDAT, MSDAT, ADAT, LDAT, FBDAT }; -enum ltype { OLINK, MLINK, VLINK, ALINK }; struct ir_namelist_t; struct ir_namelist_t { struct ir_namelist_t* nextsib; @@ -49,11 +57,10 @@ struct ir_classld_t struct ir_namelist_t* namelist, * namelist_head; }; struct ir_setld_t -{ struct classld_t* classld; +{ struct ir_classld_t* classld; 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; @@ -101,48 +108,26 @@ struct ir_set_t }; /* Functions */ static inline -struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*, uint8_t*); +struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*,const uint8_t*); +static inline +union ir_setdata_t* ir_framedata (enum dtype,const uint8_t*,apc_facing,int,int); +static inline +int init_pagelist(struct pagelist_t*,size_t); static -void ir_free_pagenodes(struct pagenode_t*); +void ir_free_pagenodes(struct pagenode_t*); static inline -int bytes_identical(uint8_t*,uint8_t*); +int bytes_identical(const uint8_t*,const uint8_t*); +static inline +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*); -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(); \ - } while (0) -#define wprintf(fmt,...) do { \ - fprintf(stderr, fmt, __VA_ARGS__); \ - do_warn(); \ - } 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.page_size - PL_HEADERSIZE - PL_HEADSIZE(_PL)) -/* Memory */ +uint8_t* name_alloc(const uint8_t*); +static +uint8_t* classname_alloc(const uint8_t*); extern //apc.c -long sys_pagesize; +long sys_pagesize; static struct pagelist_t datapages, namepages; static @@ -163,7 +148,7 @@ int init_pagelist ( struct pagelist_t* pl, size_t size ) -{ pl->page_size = size; +{ pl->pagesize = size; pl->root = (struct pagenode_t*) calloc(size,1); if (pl->root == NULL) return -1; @@ -184,8 +169,8 @@ void ir_quit static void ir_free_pagenodes ( struct pagenode_t* pagenode ) -{ if (pagenode->next != NULL) - ir_free_pages(pagenode->next); +{ if (pagenode->header.next != NULL) + ir_free_pagenodes(pagenode->header.next); free(pagenode); } @@ -201,6 +186,11 @@ 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 ) @@ -212,22 +202,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; } @@ -237,12 +232,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; @@ -250,6 +249,7 @@ 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); @@ -262,12 +262,16 @@ struct ir_set_t* ir_class_addset */ 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; @@ -275,6 +279,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); @@ -288,11 +293,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)) @@ -301,9 +306,10 @@ 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->name = name_alloc(name); + iter->header.data_name = name_alloc(name); return set->frameboxes = iter; } @@ -312,8 +318,21 @@ 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 != '_' && ca == cb); + return (ca == cb); +} + +static inline +int classnames_identical +( const uint8_t* stra, + const uint8_t* strb ) { int ca, cb; do { @@ -388,39 +407,43 @@ void ir_set_assign_ref void ir_data_assign_path ( union ir_setdata_t* setdata, - uint8_t* path + const uint8_t* path ) -{ if (setdata->header.src_filename != NULL) - wprintf("Path override: %s -> %s for setdata %s\n" +{ 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; @@ -430,8 +453,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; @@ -442,6 +467,8 @@ 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; @@ -457,39 +484,45 @@ 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); - setld->namelist.name = name_alloc(name); + setld->namelist = struct_alloc(ir_namelist_t); setld->namelist_head = setld->namelist; - setld->classld = classld; + setld->namelist_head->name = name_alloc(name); + setld->classld = classld; return setld; } struct ir_setld_t* ir_setld_addchild -( ir_setld_t* setld, - uint8_t* name +( struct ir_setld_t* setld, + const uint8_t* name ) -{ setld->namelist_head->nextsib = struct_alloc(ir_namelist_t); - setld->namelist_head->nextsib.name = name_alloc(name); - setld->namelist_head = setld->namelist_head->nextsib; +{ if (setld->namelist == NULL) + { setld->namelist = struct_alloc(ir_namelist_t); + setld->namelist_head = setld->namelist; + } + else + { setld->namelist_head->nextsib = struct_alloc(ir_namelist_t); + setld->namelist_head = setld->namelist_head->nextsib; + } + setld->namelist_head->name = name_alloc(name); return setld; } union ir_setdata_t* ir_link -( enum ltype link_type, - ir_setld_t* setld, - uint8_t* name +( enum ltype link_type, + struct ir_setld_t* setld, + const uint8_t* name ) { struct ir_link_t* link; - int name_len; link = struct_alloc(ir_link_t); link->header.type = LDAT; link->type = link_type; link->classld = setld->classld; link->setld = setld; - if (link_type != ODAT && name != NULL) + if (link_type != OLINK && name != NULL) link->header.data_name = name_alloc(name); return (union ir_setdata_t*) link; } @@ -513,19 +546,43 @@ void* stack_alloc 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 + { namepages.head->header.next = (struct pagenode_t*) calloc(namepages.pagesize,1); + if (namepages.head->header.next == NULL) + eprintf("Memory allocation error\n"); + namepages.head = namepages.head->header.next; + namepages.head->header.head = namepages.head->root; + 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 = namepages.head->header.head; + 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->next = (struct pagenode_t*) calloc(namepages.page_size,1); - if (namepages.head->next == NULL) + { namepages.head->header.next = (struct pagenode_t*) calloc(namepages.pagesize,1); + if (namepages.head->header.next == NULL) eprintf("Memory allocation error\n"); - namepages.head = namepages.head->next; + namepages.head = namepages.head->header.next; namepages.head->header.head = namepages.head->root; goto copy; }