X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fstruct.c;fp=src%2Fstruct.c;h=3fb9a30db373afbb16020422930abe6662713a83;hp=4e38f2e0d15e0339c1ccc5bb975d803892625bb5;hb=21b0a348f1e1660a3da0ebf0c4e7009a4ac11c71;hpb=fa9c8dc067f7201e19838ba6b3179ae2a2944ebb diff --git a/src/struct.c b/src/struct.c index 4e38f2e..3fb9a30 100644 --- a/src/struct.c +++ b/src/struct.c @@ -72,6 +72,16 @@ enum { */ static CMDTAB *cmdtype_code[CMDTABSIZE], *code_cmdtype[CMDTABSIZE]; +/** + * 命令の名前とタイプからハッシュ値を生成する + */ +unsigned hash_cmdtype(const char *cmd, CMDTYPE type); + +/** + * 命令コードからハッシュ値を生成する + */ +unsigned hash_code(WORD code); + /** * 命令の名前とタイプからハッシュ値を生成する */ @@ -98,20 +108,26 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) } /** - * 名前とタイプがキーの命令ハッシュ表を作成する + * 命令ハッシュ表を作成する */ -bool create_cmdtype_code() +bool create_cmdtable(CMDTAB_HASH hash) { CMDTAB *p; unsigned hashval; int i; for(i = 0; i < comet2cmdsize; i++) { - hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); /* ハッシュ値の生成 */ - p = malloc_chk(sizeof(CMDTAB), "cmdtype_code"); + p = malloc_chk(sizeof(CMDTAB), "create_cmdtable.p"); p->cmd = &comet2cmd[i]; - p->next = cmdtype_code[hashval]; /* ハッシュ表に値を追加 */ - cmdtype_code[hashval] = p; + if(hash == HASH_CMDTYPE) { + hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); + p->next = cmdtype_code[hashval]; + cmdtype_code[hashval] = p; + } else if(hash == HASH_CODE) { + hashval = hash_code((&comet2cmd[i])->code); + p->next = code_cmdtype[hashval]; + code_cmdtype[hashval] = p; + } } return true; } @@ -136,16 +152,26 @@ WORD getcmdcode(const char *cmd, CMDTYPE type) } /** - * 名前とタイプがキーの命令ハッシュ表を解放する + * 命令ハッシュ表を解放する */ -void free_cmdtype_code() +void free_cmdtable(CMDTAB_HASH hash) { int i; CMDTAB *p, *q; for(i = 0; i < CMDTABSIZE; i++) { - for(p = cmdtype_code[i]; p != NULL; p = q) { + if(hash == HASH_CMDTYPE) { + p = cmdtype_code[i]; + } else if(hash == HASH_CODE) { + p = code_cmdtype[i]; + } + for( ; p != NULL; p = q) { q = p->next; + if(p == cmdtype_code[i]) { + cmdtype_code[i] = NULL; + } else if (p == code_cmdtype[i]) { + code_cmdtype[i] = NULL; + } FREE(p); } } @@ -168,25 +194,6 @@ unsigned hash_code(WORD code) return h; } -/** - * コードがキーの命令ハッシュ表を作成する - */ -bool create_code_cmdtype() -{ - CMDTAB *p; - unsigned hashval; - int i; - - for(i = 0; i < comet2cmdsize; i++) { - hashval = hash_code((&comet2cmd[i])->code); /* ハッシュ値の生成 */ - p = malloc_chk(sizeof(CMDTAB), "code_cmdtype"); - p->cmd = &comet2cmd[i]; - p->next = code_cmdtype[hashval]; /* ハッシュ表に値を追加 */ - code_cmdtype[hashval] = p; - } - return true; -} - /** * 命令コードから命令の関数ポインタを返す */ @@ -250,21 +257,6 @@ char *grstr(WORD word) return str; } -/** - * コードがキーの命令ハッシュ表を解放する - */ -void free_code_cmdtype() -{ - int i; - CMDTAB *p, *q; - for(i = 0; i < CMDTABSIZE; i++) { - for(p = code_cmdtype[i]; p != NULL; p = q) { - q = p->next; - FREE(p); - } - } -} - /** * COMET II仮想マシンのリセット */