From 5f13e2b92707358f5a9a297afdfcbb7851fdc736 Mon Sep 17 00:00:00 2001 From: ken Date: Thu, 19 Jan 2017 16:27:33 -0800 Subject: [PATCH] ir treewalk fixes --- src/apc.c | 37 +++++++++-- src/ir.c | 187 +++++++++++++++++++++++++++++++++--------------------- 2 files changed, 145 insertions(+), 79 deletions(-) diff --git a/src/apc.c b/src/apc.c index a066765..df4810e 100644 --- a/src/apc.c +++ b/src/apc.c @@ -20,6 +20,7 @@ #include //getopt, sysconf /* Internal */ #include "parser.tab.h" //bison +#include "ir.h" #define DEFAULT_PAGESIZE 4096 const char* cargs['Z'] = {0}; @@ -64,12 +65,6 @@ int main #define DONE -1 #define SCANPATH (cargs['d'] ? cargs['d'] : "./") { int opt; - if ((sys_pagesize = sysconf(_SC_PAGESIZE)) == 0) - sys_pagesize = DEFAULT_PAGESIZE; - if (ir_init()) - { perror("init"); - exit(EXIT_FAILURE); - } getopt: switch (opt = getopt(argc, argv, OPTS)) { case 'd' : @@ -89,10 +84,40 @@ int main case DONE: break; } + if ((sys_pagesize = sysconf(_SC_PAGESIZE)) == 0) + sys_pagesize = DEFAULT_PAGESIZE; + if (ir_init()) + { perror("init"); + exit(EXIT_FAILURE); + } +#if 0 if (scanner_scanpath(SCANPATH)) { perror("scanner"); exit(EXIT_FAILURE); } +#endif + ir_class class; + ir_set set; + int i, j; + char wordbuf[0xFFF]; + for (class = ir_class_root(), i = 0, j = 0; i < 100; i++) + { sprintf(wordbuf,"Class%i",i); + class = ir_class_addchild(class,(uint8_t*)wordbuf); + sprintf(wordbuf,"c%i-s%i",i,j++); + set = ir_class_addset(class, (uint8_t*)wordbuf); + sprintf(wordbuf,"c%i-s%i",i,j++); + ir_class_addset(class, (uint8_t*)wordbuf); + sprintf(wordbuf,"c%i-s%i",i,j++); + ir_class_addset(class, (uint8_t*)wordbuf); + sprintf(wordbuf,"c%i-sc%i",i,j=0); + set = ir_set_addchild(set, (uint8_t*)wordbuf); + sprintf(wordbuf,"c%i-sc%i",i,++j); + ir_set_addchild(set, (uint8_t*)wordbuf); + sprintf(wordbuf,"c%i-sc%i",i,++j); + set = ir_set_addchild(set, (uint8_t*)wordbuf); + sprintf(wordbuf,"c%i-sb%i",i,j=0); + ir_set_addchild(set, (uint8_t*)wordbuf); + } ir_test(); ir_linker(); ir_condenser(); diff --git a/src/ir.c b/src/ir.c index 706f677..e88ac36 100644 --- a/src/ir.c +++ b/src/ir.c @@ -102,7 +102,6 @@ struct ir_class_t }; struct ir_set_t { struct ir_set_t* nextchild, * nextsib; - struct ir_class_t* class; uint32_t ref; uint8_t* name; struct ir_framebox_t* frameboxes; @@ -111,7 +110,7 @@ struct ir_set_t }; /* Functions */ static inline -struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*,const uint8_t*); +struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*,uint8_t*); static inline union ir_setdata_t* ir_framedata (enum dtype,const uint8_t*,apc_facing,int,int); static inline @@ -121,26 +120,30 @@ int classnames_identical(const uint8_t*,const uint8_t*); static void* stack_alloc(size_t); #define struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T))) +#define struct_clear(_S) (memset((_S), 0, sizeof(*(_S)))) static uint8_t* name_alloc(const uint8_t*); static uint8_t* classname_alloc(const uint8_t*); static inline void* pagelist_pop(struct pagelist_t*,size_t); +#define $($)#$ #define pagelist_alloc(pagelist) do { \ - pagelist.head->header.next = (struct pagenode_t*) calloc(pagelist.pagesize,1); \ + pagelist.head->header.next = (struct pagenode_t*) malloc(pagelist.pagesize); \ if (pagelist.head->header.next == NULL) \ eprintf("Memory allocation error\n"); \ + struct_clear(pagelist.head->header.next); \ pagelist.head = pagelist.head->header.next; \ pagelist.head->header.head = pagelist.head->root; \ } while (0) #define pagelist_init(pagelist,size) do { \ pagelist.pagesize = size; \ - pagelist.root = (struct pagenode_t*) calloc(size,1); \ + pagelist.root = (struct pagenode_t*) malloc(size); \ if (pagelist.root == NULL) \ eprintf("Memory allocation error\n"); \ - pagelist.root->header.head = pagelist.root->root; \ + struct_clear(pagelist.root); \ pagelist.head = pagelist.root; \ + pagelist.head->header.head = pagelist.head->root; \ } while (0) static void pagenode_free(struct pagenode_t*); @@ -211,7 +214,11 @@ struct ir_class_t* ir_class_addchild ) { struct ir_class_t* iter; if (class->nextchild == NULL) - goto alloc; + { class->nextchild = struct_alloc(ir_class_t); + struct_clear(class->nextchild); + class->nextchild->name = classname_alloc(name); + return class->nextchild; + } iter = class->nextchild; if (iter->name == NULL) eprintf("Null name pointer in class %p\n", iter); @@ -224,11 +231,10 @@ struct ir_class_t* ir_class_addchild { iter = iter->nextsib; goto check; } - alloc: - iter = struct_alloc(ir_class_t); - iter->nextsib = class->nextchild; - iter->name = classname_alloc(name); - return class->nextchild = iter; + iter->nextsib = struct_alloc(ir_class_t); + struct_clear(iter->nextsib); + iter->nextsib->name = classname_alloc(name); + return iter->nextsib; } /* Add a set to a class @@ -241,7 +247,11 @@ struct ir_set_t* ir_class_addset ) { struct ir_set_t* iter; if (class->root_set == NULL) - goto alloc; + { class->root_set = struct_alloc(ir_set_t); + struct_clear(class->root_set); + class->root_set->name = name_alloc(name); + return class->root_set; + } iter = class->root_set; if (iter->name == NULL) eprintf("Null name pointer in class %p\n", iter); @@ -254,11 +264,10 @@ 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; + iter->nextsib = struct_alloc(ir_set_t); + struct_clear(iter->nextsib); + iter->nextsib->name = name_alloc(name); + return iter->nextsib; } struct ir_set_t* ir_set_from_ref @@ -285,12 +294,16 @@ struct ir_set_t* ir_set_addchild ) { struct ir_set_t* iter; if (set->nextchild == NULL) - goto alloc; + { set->nextchild = struct_alloc(ir_set_t); + struct_clear(set->nextchild); + set->nextchild->name = name_alloc(name); + return set->nextchild; + } 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); + if (iter->name == NULL) + eprintf("Null name pointer in set %p\n", iter); check: if (bytes_identical(iter->name, name)) return iter; @@ -298,25 +311,30 @@ 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); - return set->nextchild = iter; + iter->nextsib = struct_alloc(ir_set_t); + struct_clear(iter->nextsib); + iter->nextsib->name = name_alloc(name); + return iter->nextsib; } /* Add a framebox to a set Attempts to create a new framebox of the specified set, returning the framebox if it already exists + Name is not allocated, but assigned, unlike other "XXX_add" functions where + name is duplicated into IR's internal array. */ static inline struct ir_framebox_t* ir_set_add_framebox ( struct ir_set_t* set, - const uint8_t* name + uint8_t* name ) { struct ir_framebox_t* iter; if (set->frameboxes == NULL) - goto alloc; + { set->frameboxes = struct_alloc(ir_framebox_t); + struct_clear(set->frameboxes); + set->frameboxes->header.data_name = name; + return set->frameboxes; + } iter = set->frameboxes; check: if (bytes_identical(iter->header.data_name, name)) @@ -325,11 +343,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->header.data_name = name_alloc(name); - return set->frameboxes = iter; + iter->header.nextsib = (union ir_setdata_t*) struct_alloc(ir_framebox_t); + struct_clear(iter->header.nextsib); + iter->header.nextsib->header.data_name = name; + return (struct ir_framebox_t*) (iter->header.nextsib); } /* Match two null-terminated bytestrings @@ -483,6 +500,7 @@ union ir_setdata_t* ir_framedata int height ) { struct ir_framedata_t* framedata = struct_alloc(ir_framedata_t); + struct_clear(framedata); if (name == NULL) eprintf("Null name in set allocation\n"); framedata->header.type = type; @@ -496,6 +514,7 @@ union ir_setdata_t* ir_framedata union ir_setdata_t* ir_audio ( const uint8_t* name ) { struct ir_simplex_t* audio = struct_alloc(ir_simplex_t); + struct_clear(audio); if (name == NULL) eprintf("Null audio\n"); audio->header.type = ADAT; @@ -511,6 +530,7 @@ struct ir_classld_t* ir_classld_from_class if (class == NULL) eprintf("Null class in classld\n"); classld = struct_alloc(ir_classld_t); + struct_clear(classld); classld->root_class = class; return classld; } @@ -519,6 +539,7 @@ struct ir_setld_t* ir_setld_from_ref ( uint32_t ref ) { struct ir_setld_t* setld; setld = struct_alloc(ir_setld_t); + struct_clear(setld); setld->ref = ref; return setld; } @@ -529,7 +550,9 @@ struct ir_setld_t* ir_setld_from_classld ) { struct ir_setld_t* setld; setld = struct_alloc(ir_setld_t); + struct_clear(setld); setld->namelist = struct_alloc(ir_namelist_t); + struct_clear(setld->namelist); setld->namelist_head = setld->namelist; setld->namelist_head->name = name_alloc(name); setld->classld = classld; @@ -542,10 +565,12 @@ struct ir_setld_t* ir_setld_addchild ) { if (setld->namelist == NULL) { setld->namelist = struct_alloc(ir_namelist_t); + struct_clear(setld->namelist); setld->namelist_head = setld->namelist; } else { setld->namelist_head->nextsib = struct_alloc(ir_namelist_t); + struct_clear(setld->namelist_head->nextsib); setld->namelist_head = setld->namelist_head->nextsib; } setld->namelist_head->name = name_alloc(name); @@ -559,6 +584,7 @@ union ir_setdata_t* ir_link ) { struct ir_link_t* link; link = struct_alloc(ir_link_t); + struct_clear(link); link->header.type = LDAT; link->type = link_type; link->classld = setld->classld; @@ -589,10 +615,12 @@ void* pagelist_pop static void* stack_alloc ( size_t bytes ) -{ if (PL_HEADMEM(datapages) < bytes) +{ void* p; + if (PL_HEADMEM(datapages) < bytes) pagelist_alloc(datapages); + p = datapages.head->header.head; datapages.head->header.head += bytes; - return (void*) datapages.head->header.head - bytes; + return p; } static @@ -606,7 +634,7 @@ uint8_t* name_alloc 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 + if (head_mem < 1) //not enough room { pagelist_alloc(namepages); goto copy; } @@ -625,7 +653,7 @@ uint8_t* classname_alloc 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 + if (head_mem < 1) //not enough room { pagelist_alloc(namepages); goto copy; } @@ -633,43 +661,56 @@ uint8_t* classname_alloc return name; } -#define pdepth() do { for (i = 0; i < depth * 2; i++) putchar(' '); } while (0) +static void crawl_class(struct ir_class_t*,int); +static void crawl_set(struct ir_set_t*,int); 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; +{ uprintf("IR From Directory: %s\n",getcwd(NULL,255)); + crawl_class(&root_class,0); +} + +#define pspace(num) for (i = 0; i < (num); i++) putchar(' ') +static +void crawl_class +( struct ir_class_t* class, + int depth +) +{ struct ir_class_t* iter; + /*if(chdir((char*)class->name)) + eprintf("CHDIR %U\n",class->name); + else + wprintf("chdir %U\n",class->name);*/ + if (class->nextchild != NULL) + { iter = class->nextchild; + do { + crawl_class(iter,depth + 1); + /*if (chdir("..")) + eprintf("CHDIR ..\n");*/ + } while ((iter = iter->nextsib) != NULL); + } + wprintf("%U/\n", class->name); + if (class->root_set != NULL) + crawl_set(class->root_set,0); + uprintf("\n%U\\\n",class->name); +} + +static +void crawl_set +( struct ir_set_t* set, + int depth +) +{ struct ir_set_t* iter; + int i = depth * 10; + pspace(i); + i = 0; + for(iter = set->nextchild; iter != NULL; iter = iter->nextchild) + { uprintf("[%8U]", iter->name); + } + for(iter = set->nextchild; iter != NULL; iter = iter->nextchild) + { if (iter->nextsib != NULL) + crawl_set(iter->nextsib, i); + i++; } + for(iter = set->nextchild; iter != NULL; iter = iter->nextchild) + crawl_set(iter, depth + 1); + putchar('\n'); } -- 2.18.0