projects
/
YACASL2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
malloc周辺を中心としたリファクタリング
[YACASL2.git]
/
src
/
label.c
diff --git
a/src/label.c
b/src/label.c
index
d740b2e
..
c76c3a7
100644
(file)
--- a/
src/label.c
+++ b/
src/label.c
@@
-1,21
+1,23
@@
#include "casl2.h"
#include "assemble.h"
#include "casl2.h"
#include "assemble.h"
-LABELTAB *labels[LABELTABSIZE];
+int labelcnt = 0; /* ラベル数 */
+LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */
/* プログラム名とラベルに対応するハッシュ値を返す */
unsigned labelhash(const char *prog, const char *label)
{
HKEY *keys[2];
int i = 0;
/* プログラム名とラベルに対応するハッシュ値を返す */
unsigned labelhash(const char *prog, const char *label)
{
HKEY *keys[2];
int i = 0;
+
if(prog != NULL) {
if(prog != NULL) {
- keys[i] = malloc
(sizeof(HKEY)
);
+ keys[i] = malloc
_chk(sizeof(HKEY), "labelhash.key[]"
);
keys[i]->type = CHARS;
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]->type = CHARS;
- keys[i]->val.s = strdup
(label
);
+ keys[i]->val.s = strdup
_chk(label, "labelhash.key[].val"
);
/* ハッシュ値を返す */
return hash(i+1, keys, LABELTABSIZE);
}
/* ハッシュ値を返す */
return hash(i+1, keys, LABELTABSIZE);
}
@@
-23,7
+25,9
@@
unsigned labelhash(const char *prog, const char *label)
/* ラベル表からアドレスを検索する */
WORD getlabel(const char *prog, const char *label)
{
/* ラベル表からアドレスを検索する */
WORD getlabel(const char *prog, const char *label)
{
+ assert(label != NULL);
LABELTAB *np;
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)
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)
@@
-34,50
+38,71
@@
WORD getlabel(const char *prog, const char *label)
return 0xFFFF;
}
return 0xFFFF;
}
-/* ã
\83
©ã
\83
\99
ã
\83
«ã
\82
\92
表に追加する */
+/* ã
\83
\97
ã
\83
ã
\82
°ã
\83
©ã
\83
å
\90
\8d
ã
\80
\81
ã
\83
©ã
\83
\99
ã
\83
«ã
\80
\81
ã
\82
¢ã
\83
\89
ã
\83
‹
\82
¹ã
\82
\92
ã
\83
©ã
\83
\99
ã
\83
«
表に追加する */
bool addlabel(const char *prog, const char *label, WORD adr)
{
bool addlabel(const char *prog, const char *label, WORD adr)
{
+ assert(label != NULL);
LABELTAB *np;
unsigned hashval;
LABELTAB *np;
unsigned hashval;
- char *keys[2];
- int i = 0;
+ /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
if(getlabel(prog, label) != 0xFFFF) {
setcerr(101, label); /* label already defined */
return false;
}
if(getlabel(prog, label) != 0xFFFF) {
setcerr(101, label); /* label already defined */
return false;
}
- np = (LABELTAB *) malloc(sizeof(*np));
- if(np == 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");
}
}
- if(prog != NULL) {
- keys[i++] = strdup(prog);
- }
- keys[i] = strdup(label);;
+ /* ラベルを設定 */
+ np->label = strdup_chk(label, "labels.label");
+ /* アドレスを設定 */
+ np->adr = adr;
+ /* ラベル数を設定 */
+ labelcnt++;
+ /* ハッシュ表へ追加 */
hashval = labelhash(prog, label);
np->next = labels[hashval];
labels[hashval] = np;
hashval = labelhash(prog, label);
np->next = labels[hashval];
labels[hashval] = np;
- np->adr = adr;
return true;
}
return true;
}
+int compare_adr(const void *a, const void *b)
+{
+ return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
+}
+
/* ラベル表を表示する */
void printlabel()
{
/* ラベル表を表示する */
void printlabel()
{
- int i;
+ int i
, asize = 0
;
LABELTAB *np;
LABELTAB *np;
+ LABELARRAY *ar[labelcnt];
+
for(i = 0; i < LABELTABSIZE; i++) {
for(np = labels[i]; np != NULL; np = np->next) {
for(i = 0; i < LABELTABSIZE; i++) {
for(np = labels[i]; np != NULL; np = np->next) {
+ assert(np->label != NULL);
+ ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]");
if(np->prog == NULL) {
if(np->prog == NULL) {
-
fprintf(stdout, "%s ---> #%04X\n", np->label, np->adr)
;
+
ar[asize]->prog = NULL
;
} else {
} else {
-
fprintf(stdout, "%s.%s ---> #%04X\n", np->prog, np->label, np->adr
);
+
ar[asize]->prog = strdup_chk(np->prog, "ar[].prog"
);
}
}
+ ar[asize]->label = strdup_chk(np->label, "ar[].label");
+ ar[asize++]->adr = np->adr;
}
}
}
}
+ qsort(ar, asize, sizeof(*ar), compare_adr);
+ for(i = 0; i < asize; i++) {
+ if(ar[i]->prog != NULL) {
+ fprintf(stdout, "%s.", ar[i]->prog);
+ }
+ fprintf(stdout, "%s ---> #%04X\n", ar[i]->label, ar[i]->adr);
+ }
}
/* ラベル表を解放する */
}
/* ラベル表を解放する */
@@
-85,10
+110,13
@@
void freelabel()
{
int i;
LABELTAB *np, *nq;
{
int i;
LABELTAB *np, *nq;
+
for(i = 0; i < LABELTABSIZE; i++) {
for(i = 0; i < LABELTABSIZE; i++) {
- for(np = labels[i]; np != NULL; np = nq){
+ for(np = labels[i]; np != NULL; np = nq)
{
nq = np->next;
nq = np->next;
- free(np->prog);
+ if(np->prog != NULL) {
+ free(np->prog);
+ }
free(np->label);
free(np);
}
free(np->label);
free(np);
}