Skip to content

Commit 443bf1f

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 6ae64af commit 443bf1f

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/globals.c

Lines changed: 27 additions & 27 deletions
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,30 @@ 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+
strcpy(al->alias, alias);
566+
hashmap_put(ALIASES_MAP, alias, al);
567+
}
568568
strcpy(al->value, value);
569569
al->disabled = false;
570570
}
571571

572572
char *find_alias(char alias[])
573573
{
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-
}
574+
alias_t *al = hashmap_get(ALIASES_MAP, alias);
575+
if (al && !al->disabled)
576+
return al->value;
578577
return NULL;
579578
}
580579

581580
bool remove_alias(char *alias)
582581
{
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-
}
582+
alias_t *al = hashmap_get(ALIASES_MAP, alias);
583+
if (al && !al->disabled) {
584+
al->disabled = true;
585+
return true;
588586
}
589587
return false;
590588
}
@@ -669,18 +667,20 @@ type_t *add_named_type(char *name)
669667

670668
void add_constant(char alias[], int value)
671669
{
672-
constant_t *constant = &CONSTANTS[constants_idx++];
670+
constant_t *constant = malloc(sizeof(constant_t));
671+
if (!constant) {
672+
printf("Failed to allocate constant_t\n");
673+
return;
674+
}
675+
673676
strcpy(constant->alias, alias);
674677
constant->value = value;
678+
hashmap_put(CONSTANTS_MAP, alias, constant);
675679
}
676680

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

686686
func_t *find_func(char func_name[])
@@ -987,8 +987,8 @@ void global_init()
987987
LABEL_LUT = malloc(MAX_LABEL * sizeof(label_lut_t));
988988
SOURCE = create_source(MAX_SOURCE);
989989
INCLUSION_MAP = hashmap_create(MAX_INCLUSIONS);
990-
ALIASES = malloc(MAX_ALIASES * sizeof(alias_t));
991-
CONSTANTS = malloc(MAX_CONSTANTS * sizeof(constant_t));
990+
ALIASES_MAP = hashmap_create(MAX_ALIASES);
991+
CONSTANTS_MAP = hashmap_create(MAX_CONSTANTS);
992992

993993
elf_code = malloc(MAX_CODE);
994994
elf_data = malloc(MAX_DATA);
@@ -1020,8 +1020,8 @@ void global_release()
10201020
free(LABEL_LUT);
10211021
source_release(SOURCE);
10221022
hashmap_free(INCLUSION_MAP);
1023-
free(ALIASES);
1024-
free(CONSTANTS);
1023+
hashmap_free(ALIASES_MAP);
1024+
hashmap_free(CONSTANTS_MAP);
10251025

10261026
free(elf_code);
10271027
free(elf_data);

0 commit comments

Comments
 (0)