From c8548a1d9afb3c175d824d5024d2288941829180 Mon Sep 17 00:00:00 2001 From: j8takagi Date: Sun, 13 Mar 2011 23:02:32 +0900 Subject: [PATCH] =?utf8?q?Valgrind=E3=81=A7=E7=99=BA=E8=A6=8B=E3=81=97?= =?utf8?q?=E3=81=9F=E5=95=8F=E9=A1=8C=E7=82=B9=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 命令表の作成 エラーリストの開放については要再検討 --- src/casl2.c | 12 +++++++-- src/cerr.c | 13 ++++----- src/cmd.c | 78 +++++++++++++++++++++-------------------------------- src/label.c | 14 +++++----- 4 files changed, 54 insertions(+), 63 deletions(-) diff --git a/src/casl2.c b/src/casl2.c index 14f372c..75902c3 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -37,6 +37,14 @@ CERR cerr_casl2[] = { { 126, "no source file" }, }; +/** + * CASL IIのエラーをエラーリストに追加 + */ +void addcerrlist_casl2() +{ + addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2); +} + /** * アセンブル結果を書き込むファイルの名前 */ @@ -61,7 +69,7 @@ int main(int argc, char *argv[]) "Usage: %s [-slLaAtTdh] [-oO[]] [-M ] [-C ] FILE1[ FILE2 ...]\n"; cerr_init(); - addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2); + addcerrlist_casl2(); addcerrlist_assemble(); addcerrlist_exec(); /* オプションの処理 */ @@ -127,7 +135,7 @@ int main(int argc, char *argv[]) for(pass = FIRST; pass <= SECOND; pass++) { if(pass == FIRST) { create_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を作成 */ - asptr = malloc_chk(sizeof(asptr), "asptr"); /* アセンブル時のプロパティ用の領域確保 */ + asptr = malloc_chk(sizeof(ASPTR), "asptr"); /* アセンブル時のプロパティ用の領域確保 */ } for(i = optind; i < argc; i++) { /* データの格納開始位置 */ diff --git a/src/cerr.c b/src/cerr.c index 6ab5e56..a8dfe0a 100644 --- a/src/cerr.c +++ b/src/cerr.c @@ -105,14 +105,15 @@ void freecerr() { CERRLIST *p = cerrlist, *q; - /* エラーリストを解放 */ - while(p != NULL) { - q = p->next; - free_chk(p, "freecerr.p"); - p = q; - } /* 現在のエラーメッセージを解放 */ free_chk(cerr->msg, "cerr.msg"); /* 現在のエラーを解放 */ free_chk(cerr, "cerr"); + /* エラーリストを解放 */ + for(p = cerrlist; p != NULL; p = q) { + q = p->next; + /* free_chk(p->cerr->msg, "freecerr.p.cerr.msg"); */ + /* free_chk(p->cerr, "freecerr.p.cerr"); */ + free_chk(p, "freecerr.p"); + } } diff --git a/src/cmd.c b/src/cmd.c index 53d3944..fb9ae6f 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -94,25 +94,18 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) */ bool create_cmdtype_code() { - CMDTAB *np; + CMDTAB *p; unsigned hashval; int i; cmdtabsize = comet2cmdsize; cmdtype_code = calloc_chk(cmdtabsize, sizeof(CMDTAB *), "cmdtype_code"); - for(i = 0; i < cmdtabsize; i++) { - *(cmdtype_code + i) = NULL; - } for(i = 0; i < comet2cmdsize; i++) { - np = malloc_chk(sizeof(CMDTAB), "cmdtype_code.np"); - np->cmd = NULL; - np->next = NULL; - /* ハッシュ値の生成 */ - hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); - /* ハッシュ表に値を追加 */ - np->next = cmdtype_code[hashval]; - cmdtype_code[hashval] = np; - np->cmd = &(comet2cmd[i]); + p = malloc_chk(sizeof(CMDTAB), "cmdtype_code.p"); + hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); /* ハッシュ値の生成 */ + p->next = cmdtype_code[hashval]; /* ハッシュ表に値を追加 */ + p->cmd = &comet2cmd[i]; + cmdtype_code[hashval] = p; } return true; } @@ -123,12 +116,12 @@ bool create_cmdtype_code() */ WORD getcmdcode(const char *cmd, CMDTYPE type) { - CMDTAB *np; + CMDTAB *p; assert(cmd != NULL); - for(np = cmdtype_code[hash_cmdtype(cmd, type)]; np != NULL; np = np->next){ - if(strcmp(cmd, np->cmd->name) == 0 && type == np->cmd->type) { - return np->cmd->code; + for(p = cmdtype_code[hash_cmdtype(cmd, type)]; p != NULL; p = p->next){ + if(strcmp(cmd, p->cmd->name) == 0 && type == p->cmd->type) { + return p->cmd->code; } } return 0xFFFF; @@ -140,14 +133,12 @@ WORD getcmdcode(const char *cmd, CMDTYPE type) void free_cmdtype_code() { int i; - CMDTAB *np, *nq; + CMDTAB *p, *q; for(i = 0; i < cmdtabsize; i++) { - np = cmdtype_code[i]; - while(np != NULL) { - nq = np->next; - free_chk(np, "free_cmdtype_code.np"); - np = nq; + for(p = cmdtype_code[i]; p != NULL; p = q) { + q = p->next; + free_chk(p, "free_cmdtype_code.p"); } } free_chk(cmdtype_code, "cmdtype_code"); @@ -173,39 +164,32 @@ unsigned hash_code(WORD code) */ bool create_code_type() { - CMDTAB *np; + CMDTAB *p; unsigned hashval; int i; cmdtabsize = comet2cmdsize; code_type = calloc_chk(cmdtabsize, sizeof(CMDTAB *), "code_type"); - for(i = 0; i < cmdtabsize; i++) { - *(code_type + i) = NULL; - } for(i = 0; i < comet2cmdsize; i++) { - np = malloc_chk(sizeof(CMDTAB), "code_type.np"); - np->cmd = NULL; - np->next = NULL; - /* ハッシュ値の生成 */ - hashval = hash_code((&comet2cmd[i])->code); - /* ハッシュ表に値を追加 */ - np->next = code_type[hashval]; - code_type[hashval] = np; - np->cmd = &comet2cmd[i]; + p = malloc_chk(sizeof(CMDTAB), "code_type.p"); + hashval = hash_code((&comet2cmd[i])->code); /* ハッシュ値の生成 */ + p->next = code_type[hashval]; /* ハッシュ表に値を追加 */ + p->cmd = &comet2cmd[i]; + code_type[hashval] = p; } return true; } /** * 命令コードから命令タイプを返す - * 無効な場合はNONEを返す + * 無効な場合はNOTCMDを返す */ CMDTYPE getcmdtype(WORD code) { - CMDTAB *np; - for(np = code_type[hash_code(code)]; np != NULL; np = np->next) { - if(code == np->cmd->code) { - return np->cmd->type; + CMDTAB *p; + for(p = code_type[hash_code(code)]; p != NULL; p = p->next) { + if(code == p->cmd->code) { + return p->cmd->type; } } return NOTCMD; @@ -217,13 +201,13 @@ CMDTYPE getcmdtype(WORD code) void free_code_type() { int i; - CMDTAB *np, *nq; + CMDTAB *p, *q; for(i = 0; i < cmdtabsize; i++) { - np = code_type[i]; - while(np != NULL) { - nq = np->next; - free_chk(np, "code_type.np"); - np = nq; + p = code_type[i]; + while(p != NULL) { + q = p->next; + free_chk(p, "code_type.p"); + p = q; } } free_chk(code_type, "code_type"); diff --git a/src/label.c b/src/label.c index 9124b5c..a36231b 100644 --- a/src/label.c +++ b/src/label.c @@ -129,16 +129,14 @@ 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_chk(np->prog, "np.prog"); - } - free_chk(np->label, "np.label"); - free_chk(np, "np"); + for(p = labels[i]; p != NULL; p = q) { + q = p->next; + free_chk(p->prog, "freelabel.p.prog"); + free_chk(p->label, "freelabel.p.label"); + free_chk(p, "freelabel.p"); } } } -- 2.18.0