X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Flabel.c;h=c76c3a7590ee6a5651c186d0573c875a26705d55;hp=8d3aa4b366e45cdc474db1d29cd2a640cbe95bbf;hb=650f92bf8dfdd0095db993f71f9e3867e7119acc;hpb=e24285f0509319319aef28a188b7c01ba7e22bf1 diff --git a/src/label.c b/src/label.c index 8d3aa4b..c76c3a7 100644 --- a/src/label.c +++ b/src/label.c @@ -1,7 +1,7 @@ #include "casl2.h" #include "assemble.h" -int labelcnt = 0; /* ラベル数 */ +int labelcnt = 0; /* ラベル数 */ LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */ /* プログラム名とラベルに対応するハッシュ値を返す */ @@ -9,14 +9,15 @@ unsigned labelhash(const char *prog, const char *label) { HKEY *keys[2]; int i = 0; + if(prog != NULL) { - keys[i] = malloc(sizeof(HKEY)); + keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]"); keys[i]->type = CHARS; - keys[i++]->val.s = strdup(prog); + keys[i]->val.s = strdup_chk(prog, "labelhash.key[].val"); } - keys[i] = malloc(sizeof(HKEY)); + keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]"); keys[i]->type = CHARS; - keys[i]->val.s = strdup(label); + keys[i]->val.s = strdup_chk(label, "labelhash.key[].val"); /* ハッシュ値を返す */ return hash(i+1, keys, LABELTABSIZE); } @@ -24,7 +25,9 @@ unsigned labelhash(const char *prog, const char *label) /* ラベル表からアドレスを検索する */ WORD getlabel(const char *prog, const char *label) { + assert(label != NULL); LABELTAB *np; + for(np = labels[labelhash(prog, label)]; np != NULL; np = np->next) { if((prog == NULL || (np->prog != NULL && strcmp(prog, np->prog) == 0)) && strcmp(label, np->label) == 0) @@ -35,40 +38,42 @@ WORD getlabel(const char *prog, const char *label) return 0xFFFF; } -/* ラベルを表に追加する */ +/* プログラム名、ラベル、アドレスをラベル表に追加する */ bool addlabel(const char *prog, const char *label, WORD adr) { + assert(label != NULL); LABELTAB *np; unsigned hashval; - char *keys[2]; - int i = 0; + /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */ if(getlabel(prog, label) != 0xFFFF) { setcerr(101, label); /* label already defined */ return false; } - np = malloc(sizeof(*np)); - if(np == NULL || (np->label = strdup(label)) == NULL || - (prog != NULL && (np->prog = strdup(prog)) == NULL)) - { - setcerr(102, NULL); /* label table is full */ - return false; - } - if(prog != NULL) { - keys[i++] = strdup(prog); + /* メモリを確保 */ + np = malloc_chk(sizeof(LABELTAB), "labels.next"); + /* プログラム名を設定 */ + if(prog == NULL) { + np->prog = NULL; + } else { + np->prog = strdup_chk(prog, "labels.prog"); } + /* ラベルを設定 */ + np->label = strdup_chk(label, "labels.label"); + /* アドレスを設定 */ + np->adr = adr; + /* ラベル数を設定 */ labelcnt++; - keys[i] = strdup(label); + /* ハッシュ表へ追加 */ hashval = labelhash(prog, label); np->next = labels[hashval]; labels[hashval] = np; - np->adr = adr; return true; } int compare_adr(const void *a, const void *b) { - return (**(const LABELARRAY **)a).adr - (**(const LABELARRAY **)b).adr; + return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr; } /* ラベル表を表示する */ @@ -80,11 +85,15 @@ void printlabel() for(i = 0; i < LABELTABSIZE; i++) { for(np = labels[i]; np != NULL; np = np->next) { - ar[asize] = malloc(sizeof(LABELARRAY *)); - ar[asize]->prog = (np->prog == NULL ? NULL : strdup(np->prog)); - ar[asize]->label = strdup(np->label); - ar[asize]->adr = np->adr; - asize++; + assert(np->label != NULL); + ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]"); + if(np->prog == NULL) { + ar[asize]->prog = NULL; + } else { + ar[asize]->prog = strdup_chk(np->prog, "ar[].prog"); + } + ar[asize]->label = strdup_chk(np->label, "ar[].label"); + ar[asize++]->adr = np->adr; } } qsort(ar, asize, sizeof(*ar), compare_adr); @@ -101,10 +110,13 @@ void freelabel() { int i; LABELTAB *np, *nq; + for(i = 0; i < LABELTABSIZE; i++) { for(np = labels[i]; np != NULL; np = nq) { nq = np->next; - free(np->prog); + if(np->prog != NULL) { + free(np->prog); + } free(np->label); free(np); }