X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Flabel.c;h=1b9b806d4e4d2ee96eb1d8f8291917e5c8c7d8ed;hp=790a29aca0be0f2a26bd61e249cf2a0376bd99c1;hb=555c5e8b851becc08ba661a9cb19f617d5a00c12;hpb=2b714046a6f26036d03ae2d5e8b410cf83b4526c diff --git a/src/label.c b/src/label.c index 790a29a..1b9b806 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); } @@ -26,6 +27,7 @@ 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) @@ -48,17 +50,21 @@ bool addlabel(const char *prog, const char *label, WORD adr) setcerr(101, label); /* label already defined */ return false; } - /* プログラム名、ラベル、アドレスを設定。メモリーを確保できない場合はエラー発生 */ - if((np = malloc(sizeof(LABELTAB))) == NULL || (np->label = strdup(label)) == NULL || - (prog != NULL && (np->prog = strdup(prog)) == NULL)) - { - setcerr(102, NULL); /* label table is full */ - return false; + /* メモリを確保 */ + 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++; - /* ハッシュ表の設定 */ + /* ハッシュ表へ追加 */ hashval = labelhash(prog, label); np->next = labels[hashval]; labels[hashval] = np; @@ -80,9 +86,13 @@ void printlabel() 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] = 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; } } @@ -100,14 +110,15 @@ void freelabel() { int i; LABELTAB *np, *nq; + 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); + free_chk(np->label, "np.label"); + free_chk(np, "np"); } } }