Skip to content

Commit 0799baf

Browse files
author
icgmilk
committed
Replace ALIASES and CONSTANTS with hashmap
Use existing hashmap utilities to replace ALIASES and CONSTANTS. Previously, "find_alias" and "find_constant" performed linear searches (O(n)). With the hashmap, search time is reduced to O(1) on average.
1 parent 793531c commit 0799baf

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

src/globals.c

+31-27
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ int macros_idx = 0;
3838
* we have to initially create large amount of buckets.
3939
*/
4040
hashmap_t *FUNCS_MAP;
41+
hashmap_t *ALIASES_MAP;
42+
hashmap_t *CONSTANTS_MAP;
4143

4244
type_t *TYPES;
4345
int types_idx = 0;
@@ -66,12 +68,6 @@ int elf_offset = 0;
6668

6769
regfile_t REGS[REG_CNT];
6870

69-
alias_t *ALIASES;
70-
int aliases_idx = 0;
71-
72-
constant_t *CONSTANTS;
73-
int constants_idx = 0;
74-
7571
source_t *SOURCE;
7672

7773
hashmap_t *INCLUSION_MAP;
@@ -563,28 +559,34 @@ block_t *add_block(block_t *parent, func_t *func, macro_t *macro)
563559

564560
void add_alias(char *alias, char *value)
565561
{
566-
alias_t *al = &ALIASES[aliases_idx++];
567-
strcpy(al->alias, alias);
562+
alias_t *al = hashmap_get(ALIASES_MAP, alias);
563+
if (!al) {
564+
al = malloc(sizeof(alias_t));
565+
if (!al) {
566+
printf("Failed to allocate alias_t\n");
567+
return;
568+
}
569+
strcpy(al->alias, alias);
570+
hashmap_put(ALIASES_MAP, alias, al);
571+
}
568572
strcpy(al->value, value);
569573
al->disabled = false;
570574
}
571575

572576
char *find_alias(char alias[])
573577
{
574-
for (int i = 0; i < aliases_idx; i++) {
575-
if (!ALIASES[i].disabled && !strcmp(alias, ALIASES[i].alias))
576-
return ALIASES[i].value;
577-
}
578+
alias_t *al = hashmap_get(ALIASES_MAP, alias);
579+
if (al && !al->disabled)
580+
return al->value;
578581
return NULL;
579582
}
580583

581584
bool remove_alias(char *alias)
582585
{
583-
for (int i = 0; i < aliases_idx; i++) {
584-
if (!ALIASES[i].disabled && !strcmp(alias, ALIASES[i].alias)) {
585-
ALIASES[i].disabled = true;
586-
return true;
587-
}
586+
alias_t *al = hashmap_get(ALIASES_MAP, alias);
587+
if (al && !al->disabled) {
588+
al->disabled = true;
589+
return true;
588590
}
589591
return false;
590592
}
@@ -669,18 +671,20 @@ type_t *add_named_type(char *name)
669671

670672
void add_constant(char alias[], int value)
671673
{
672-
constant_t *constant = &CONSTANTS[constants_idx++];
674+
constant_t *constant = malloc(sizeof(constant_t));
675+
if (!constant) {
676+
printf("Failed to allocate constant_t\n");
677+
return;
678+
}
679+
673680
strcpy(constant->alias, alias);
674681
constant->value = value;
682+
hashmap_put(CONSTANTS_MAP, alias, constant);
675683
}
676684

677685
constant_t *find_constant(char alias[])
678686
{
679-
for (int i = 0; i < constants_idx; i++) {
680-
if (!strcmp(CONSTANTS[i].alias, alias))
681-
return &CONSTANTS[i];
682-
}
683-
return NULL;
687+
return hashmap_get(CONSTANTS_MAP, alias);
684688
}
685689

686690
func_t *find_func(char func_name[])
@@ -1006,8 +1010,8 @@ void global_init()
10061010
LABEL_LUT = malloc(MAX_LABEL * sizeof(label_lut_t));
10071011
SOURCE = source_create(MAX_SOURCE);
10081012
INCLUSION_MAP = hashmap_create(MAX_INCLUSIONS);
1009-
ALIASES = malloc(MAX_ALIASES * sizeof(alias_t));
1010-
CONSTANTS = malloc(MAX_CONSTANTS * sizeof(constant_t));
1013+
ALIASES_MAP = hashmap_create(MAX_ALIASES);
1014+
CONSTANTS_MAP = hashmap_create(MAX_CONSTANTS);
10111015

10121016
elf_code = malloc(MAX_CODE);
10131017
elf_data = malloc(MAX_DATA);
@@ -1039,8 +1043,8 @@ void global_release()
10391043
free(LABEL_LUT);
10401044
source_free(SOURCE);
10411045
hashmap_free(INCLUSION_MAP);
1042-
free(ALIASES);
1043-
free(CONSTANTS);
1046+
hashmap_free(ALIASES_MAP);
1047+
hashmap_free(CONSTANTS_MAP);
10441048

10451049
free(elf_code);
10461050
free(elf_data);

0 commit comments

Comments
 (0)