X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Flabel.c;h=a8baa1df2b585e91c12e7be3d09f799d6cafc338;hp=c705ad11600aa3ffb81bb73825f757a14ad92dc8;hb=6eb54846c27b216d7602a5107d9383f8f4324d71;hpb=5562b8293058464957a1f833777e187a78220ed5 diff --git a/src/label.c b/src/label.c index c705ad1..a8baa1d 100644 --- a/src/label.c +++ b/src/label.c @@ -1,42 +1,63 @@ -#include "casl2.h" +#include +#include +#include +#include + +#include "cerr.h" +#include "cmem.h" +#include "hash.h" #include "assemble.h" -int labelcnt = 0; /* ラベル数 */ -LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */ +static int labelcnt = 0; /* ラベル数 */ +static LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */ -/* プログラム名とラベルに対応するハッシュ値を返す */ +/** + * プログラム名とラベルに対応するハッシュ値を返す + */ unsigned labelhash(const char *prog, const char *label) { HKEY *keys[2]; - int i = 0; + int i = 0, j; + unsigned h; + 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"); + i++; } - keys[i] = malloc(sizeof(HKEY)); + keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key"); keys[i]->type = CHARS; - keys[i]->val.s = strdup(label); - /* ハッシュ値を返す */ - return hash(i+1, keys, LABELTABSIZE); + keys[i]->val.s = strdup_chk(label, "labelhash.key.val"); + h = hash(i+1, keys, LABELTABSIZE); + for(j = 0; j < i + 1; j++) { + FREE(keys[j]->val.s); + FREE(keys[j]); + } + return h; } -/* ラベル表からアドレスを検索する */ +/** + * プログラム名とラベルに対応するアドレスをラベル表から検索する + */ 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) + LABELTAB *p; + + for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) { + if((prog == NULL || (p->prog != NULL && strcmp(prog, p->prog) == 0)) && + strcmp(label, p->label) == 0) { - return np->adr; + return p->adr; } } return 0xFFFF; } -/* プログラム名、ラベル、アドレスをラベル表に追加する */ +/** + * プログラム名、ラベル、アドレスをラベル表に追加する + */ bool addlabel(const char *prog, const char *label, WORD adr) { assert(label != NULL); @@ -49,21 +70,15 @@ bool addlabel(const char *prog, const char *label, WORD adr) return false; } /* メモリを確保 */ - if((np = malloc(sizeof(LABELTAB))) == NULL) { - goto cerr102; - } + np = malloc_chk(sizeof(LABELTAB), "labels.next"); /* プログラム名を設定 */ if(prog == NULL) { np->prog = NULL; } else { - if((np->prog = strdup(prog)) == NULL) { - goto cerr102; - } + np->prog = strdup_chk(prog, "labels.prog"); } /* ラベルを設定 */ - if((np->label = strdup(label)) == NULL) { - goto cerr102; - } + np->label = strdup_chk(label, "labels.label"); /* アドレスを設定 */ np->adr = adr; /* ラベル数を設定 */ @@ -73,30 +88,36 @@ bool addlabel(const char *prog, const char *label, WORD adr) np->next = labels[hashval]; labels[hashval] = np; return true; -cerr102: - setcerr(102, NULL); /* label table is full */ - return false; } +/** + * ラベルを比較した結果を返す + */ int compare_adr(const void *a, const void *b) { return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr; } -/* ラベル表を表示する */ +/** + * ラベル表を表示する + */ void printlabel() { int i, asize = 0; - LABELTAB *np; + LABELTAB *p; LABELARRAY *ar[labelcnt]; for(i = 0; i < LABELTABSIZE; i++) { - for(np = labels[i]; np != NULL; np = np->next) { - assert(np->label != NULL); - 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; + for(p = labels[i]; p != NULL; p = p->next) { + assert(p->label != NULL); + ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]"); + if(p->prog == NULL) { + ar[asize]->prog = NULL; + } else { + ar[asize]->prog = strdup_chk(p->prog, "ar[].prog"); + } + ar[asize]->label = strdup_chk(p->label, "ar[].label"); + ar[asize++]->adr = p->adr; } } qsort(ar, asize, sizeof(*ar), compare_adr); @@ -108,19 +129,20 @@ void printlabel() } } -/* ラベル表を解放する */ +/** + * ラベル表を解放する + */ void freelabel() { int i; - LABELTAB *np, *nq; + LABELTAB *p, *q; + for(i = 0; i < LABELTABSIZE; i++) { - for(np = labels[i]; np != NULL; np = nq) { - nq = np->next; - if(np->prog != NULL) { - free(np->prog); - } - free(np->label); - free(np); + for(p = labels[i]; p != NULL; p = q) { + q = p->next; + FREE(p->prog); + FREE(p->label); + FREE(p); } } }