X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fcmd.c;h=add169acb46948e2fe47ba0460d059acc201ef79;hp=7c1e803ea275554c96dfd138fe2c3edb8a6f4d54;hb=555c5e8b851becc08ba661a9cb19f617d5a00c12;hpb=e24285f0509319319aef28a188b7c01ba7e22bf1 diff --git a/src/cmd.c b/src/cmd.c index 7c1e803..add169a 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -1,6 +1,6 @@ #include "casl2.h" -CMDCODEARRAY cmdcodearray[] = { +CMD comet2cmd[] = { { "NOP", NONE, 0x0 }, { "LD", R_ADR_X_, 0x1000 }, { "ST", R_ADR_X, 0x1100 }, @@ -41,47 +41,54 @@ CMDCODEARRAY cmdcodearray[] = { { "RET", NONE, 0x8100 }, }; -int cmdcodesize = ARRAYSIZE(cmdcodearray); -int hashtabsize; -CMDCODETAB **cmdtype_code, **code_type; +int comet2cmdsize = ARRAYSIZE(comet2cmd); +int cmdtabsize; +CMDTAB **cmdtype_code, **code_type; /* 命令と命令タイプからハッシュ値を生成する */ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) { HKEY *keys[2]; + unsigned hashval; /* 命令をセット */ - keys[0] = malloc(sizeof(HKEY)); + keys[0] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[0]"); keys[0]->type = CHARS; - keys[0]->val.s = strdup(cmd); + keys[0]->val.s = strdup_chk(cmd, "keys[0].val.s"); /* 命令タイプをセット */ - keys[1] = malloc(sizeof(HKEY)); + keys[1] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[1]"); keys[1]->type = INT; keys[1]->val.i = (int)(type & 070); + /* ハッシュ値の計算 */ + hashval = hash(2, keys, cmdtabsize); + free_chk(keys[0]->val.s, "keys[0].val.s"); + free_chk(keys[0], "keys[0]"); + free_chk(keys[1], "keys[1]"); /* ハッシュ値を返す */ - return hash(2, keys, cmdcodesize); + return hashval; } /* 命令と命令タイプがキーのハッシュ表を作成する */ bool create_cmdtype_code() { - CMDCODETAB *np; + CMDTAB *np; unsigned hashval; int i; - hashtabsize = cmdcodesize; - cmdtype_code = malloc(cmdcodesize * sizeof(CMDCODETAB *)); - for(i = 0; i < cmdcodesize; i++) { - np = malloc(sizeof(CMDCODETAB)); - if(np == NULL) { - setcerr(122, NULL); /* cannot create hash table */ - return false; - } + 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.next"); + np->cmd = NULL; + np->next = NULL; /* ハッシュ値の生成 */ - hashval = hash_cmdtype((&cmdcodearray[i])->cmd, (&cmdcodearray[i])->type); + hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); /* ハッシュ表に値を追加 */ np->next = cmdtype_code[hashval]; cmdtype_code[hashval] = np; - np->cca = &(cmdcodearray[i]); + np->cmd = &(comet2cmd[i]); } return true; } @@ -90,11 +97,12 @@ bool create_cmdtype_code() /* 無効な場合は0xFFFFを返す */ WORD getcmdcode(const char *cmd, CMDTYPE type) { - CMDCODETAB *np; + CMDTAB *np; + assert(cmd != NULL); for(np = cmdtype_code[hash_cmdtype(cmd, type)]; np != NULL; np = np->next){ - if(strcmp(cmd, np->cca->cmd) == 0 && type == np->cca->type) { - return np->cca->code; + if(strcmp(cmd, np->cmd->name) == 0 && type == np->cmd->type) { + return np->cmd->code; } } return 0xFFFF; @@ -104,15 +112,17 @@ WORD getcmdcode(const char *cmd, CMDTYPE type) void free_cmdtype_code() { int i; - CMDCODETAB *np, *nq; - for(i = 0; i < cmdcodesize; i++){ + CMDTAB *np, *nq; + + for(i = 0; i < cmdtabsize; i++) { np = cmdtype_code[i]; while(np != NULL) { nq = np->next; - free(np); + free_chk(np, "free_cmdtype_code.np"); np = nq; } } + free_chk(cmdtype_code, "cmdtype_code"); } /* 命令コードからハッシュ値を生成する */ @@ -121,33 +131,35 @@ unsigned hash_code(WORD code) HKEY *keys[1]; /* 命令コードをセット */ - keys[0] = malloc(sizeof(HKEY)); + keys[0] = malloc_chk(sizeof(HKEY), "hash_code.key"); keys[0]->type = INT; keys[0]->val.i = (int)(code >> 8); /* ハッシュ値を返す */ - return hash(1, keys, cmdcodesize); + return hash(1, keys, cmdtabsize); } /* 命令コードがキーのハッシュ表を作成する */ bool create_code_type() { - CMDCODETAB *np; + CMDTAB *np; unsigned hashval; int i; - hashtabsize = cmdcodesize; - code_type = malloc(cmdcodesize * sizeof(CMDCODETAB *)); - for(i = 0; i < cmdcodesize; i++) { - if((np = malloc(sizeof(CMDCODETAB))) == NULL) { - setcerr(122, NULL); /* cannot create hash table */ - return false; - } + 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.next"); + np->cmd = NULL; + np->next = NULL; /* ハッシュ値の生成 */ - hashval = hash_code((&cmdcodearray[i])->code); + hashval = hash_code((&comet2cmd[i])->code); /* ハッシュ表に値を追加 */ np->next = code_type[hashval]; code_type[hashval] = np; - np->cca = &cmdcodearray[i]; + np->cmd = &comet2cmd[i]; } return true; } @@ -156,10 +168,10 @@ bool create_code_type() /* 無効な場合はNONEを返す */ CMDTYPE getcmdtype(WORD code) { - CMDCODETAB *np; + CMDTAB *np; for(np = code_type[hash_code(code)]; np != NULL; np = np->next) { - if(code == np->cca->code) { - return np->cca->type; + if(code == np->cmd->code) { + return np->cmd->type; } } return NONE; @@ -169,8 +181,8 @@ CMDTYPE getcmdtype(WORD code) void free_code_type() { int i; - CMDCODETAB *np, *nq; - for(i = 0; i < cmdcodesize; i++){ + CMDTAB *np, *nq; + for(i = 0; i < cmdtabsize; i++) { np = code_type[i]; while(np != NULL) { nq = np->next; @@ -178,4 +190,5 @@ void free_code_type() np = nq; } } + free(code_type); }