X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=blobdiff_plain;f=src%2Fir.c;h=a175ef3fdfd93e7c47b02f332a4ac3f4e2cfef67;hp=f39f85b2371f700a6e8cbc98091f6ee4d62ad17d;hb=7dae7dc73dfbabdc895a78738f550f56561da644;hpb=78ec1b2aecefbd7f6839f54c52b2dd6e3f80bf1a diff --git a/src/ir.c b/src/ir.c index f39f85b..a175ef3 100644 --- a/src/ir.c +++ b/src/ir.c @@ -14,16 +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) /* Public */ int ir_init(void); void ir_quit(void); int ir_linker(void); int ir_condenser(void); /* Memory allocation structures */ -enum dtype { FSDAT, MSDAT, ADAT, LDAT, FBDAT }; struct pagenode_t; struct pagenode_header_t { struct pagenode_t* next; @@ -37,6 +40,13 @@ struct pagelist_t { struct pagenode_t* root, * head; size_t pagesize; }; +#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 }; struct ir_namelist_t; struct ir_namelist_t { struct ir_namelist_t* nextsib; @@ -51,7 +61,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; @@ -99,50 +108,26 @@ struct ir_set_t }; /* Functions */ static inline -int init_pagelist(struct pagelist_t*,size_t); -static inline 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(const uint8_t*,const 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(const uint8_t*); -static inline -union ir_setdata_t* ir_framedata (enum dtype,const 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.pagesize - 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 @@ -152,9 +137,9 @@ struct ir_class_t root_class = { .name = (uint8_t*)"." }; int ir_init ( void ) { if (init_pagelist(&datapages, (size_t)DATA_PAGESIZE)) - eprint("Memory allocation error\n"); + eprintf("Memory allocation error\n"); if (init_pagelist(&namepages, (size_t)NAME_PAGESIZE)) - eprint("Memory allocation error\n"); + eprintf("Memory allocation error\n"); return 0; } @@ -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 ) @@ -215,7 +205,6 @@ struct ir_class_t* ir_class_addchild const uint8_t* name ) { struct ir_class_t* iter; - printf("Class %s, addchild %s\n", class->name, name); if (class->nextchild == NULL) goto alloc; iter = class->nextchild; @@ -224,7 +213,7 @@ struct ir_class_t* ir_class_addchild 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; @@ -233,7 +222,7 @@ struct ir_class_t* ir_class_addchild 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; } @@ -246,14 +235,13 @@ struct ir_set_t* ir_class_addset const uint8_t* name ) { struct ir_set_t* iter; - printf("Class %s, addset %s\n", class->name, name); if (class->root_set == NULL) 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 %s\n", iter->name); + eprintf("Null set added to class %U\n", iter->name); check: if (bytes_identical(iter->name, name)) return iter; @@ -277,7 +265,6 @@ struct ir_set_t* ir_set_addchild const uint8_t* name ) { struct ir_set_t* iter; - printf("Set %s, addchild %s\n", set->name, name); if (set->nextchild == NULL) goto alloc; iter = set->nextchild; @@ -342,6 +329,19 @@ int bytes_identical return (ca == cb); } +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 */ @@ -443,7 +443,7 @@ union ir_setdata_t* ir_framedata ) { struct ir_framedata_t* framedata = struct_alloc(ir_framedata_t); if (name == NULL) - eprint("Null name in set allocation\n"); + eprintf("Null name in set allocation\n"); framedata->header.type = type; framedata->header.data_name = name_alloc(name); framedata->frameinfo.facing = d; @@ -456,7 +456,7 @@ union ir_setdata_t* ir_audio ( const uint8_t* name ) { struct ir_simplex_t* audio = struct_alloc(ir_simplex_t); if (name == NULL) - eprint("Null audio\n"); + eprintf("Null audio\n"); audio->header.type = ADAT; audio->header.data_name = name_alloc(name); return (union ir_setdata_t*) audio; @@ -468,7 +468,7 @@ struct ir_classld_t* ir_classld_from_class ( struct ir_class_t* class ) { struct ir_classld_t* classld; if (class == NULL) - eprint("Null class in classld\n"); + eprintf("Null class in classld\n"); classld = struct_alloc(ir_classld_t); classld->root_class = class; return classld; @@ -536,7 +536,7 @@ void* stack_alloc 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"); + eprintf("Memory allocation error \n"); datapages.head = datapages.head->header.next; datapages.head->header.head = datapages.head->root; } @@ -558,7 +558,30 @@ uint8_t* name_alloc 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"); + 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 = (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) + eprintf("Memory allocation error\n"); namepages.head = namepages.head->header.next; namepages.head->header.head = namepages.head->root; goto copy;