Skip to content

Commit aa51873

Browse files
author
icgmilk
committed
Replace MACROS with hashmap
Introduced 'MACROS_MAP' to replace the original 'MACROS' structure, and refactored related logic with hashmap utilities. This change improves memory flexibility and future extensibility.
1 parent 7163605 commit aa51873

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/globals.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ int macro_return_idx;
2828

2929
/* Global objects */
3030

31-
macro_t *MACROS;
32-
int macros_idx = 0;
33-
3431
/* FUNC_MAP is used to integrate function storing and boost lookup
3532
* performance, currently it uses FNV-1a hash function to hash function
3633
* name.
3734
*/
35+
36+
hashmap_t *MACROS_MAP;
3837
hashmap_t *FUNC_MAP;
3938
hashmap_t *ALIASES_MAP;
4039
hashmap_t *CONSTANTS_MAP;
@@ -557,28 +556,34 @@ bool remove_alias(char *alias)
557556

558557
macro_t *add_macro(char *name)
559558
{
560-
macro_t *ma = &MACROS[macros_idx++];
561-
strcpy(ma->name, name);
559+
macro_t *ma = hashmap_get(MACROS_MAP, name);
560+
if (!ma) {
561+
ma = malloc(sizeof(macro_t));
562+
if (!ma) {
563+
printf("Failed to allocate macro_t\n");
564+
return NULL;
565+
}
566+
strcpy(ma->name, name);
567+
hashmap_put(MACROS_MAP, name, ma);
568+
}
562569
ma->disabled = false;
563570
return ma;
564571
}
565572

566573
macro_t *find_macro(char *name)
567574
{
568-
for (int i = 0; i < macros_idx; i++) {
569-
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name))
570-
return &MACROS[i];
571-
}
575+
macro_t *ma = hashmap_get(MACROS_MAP, name);
576+
if (ma && !ma->disabled)
577+
return ma;
572578
return NULL;
573579
}
574580

575581
bool remove_macro(char *name)
576582
{
577-
for (int i = 0; i < macros_idx; i++) {
578-
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name)) {
579-
MACROS[i].disabled = true;
580-
return true;
581-
}
583+
macro_t *ma = hashmap_get(MACROS_MAP, name);
584+
if (ma) {
585+
ma->disabled = true;
586+
return true;
582587
}
583588
return false;
584589
}
@@ -974,7 +979,7 @@ void global_init(void)
974979
{
975980
elf_code_start = ELF_START + elf_header_len;
976981

977-
MACROS = malloc(MAX_ALIASES * sizeof(macro_t));
982+
MACROS_MAP = hashmap_create(MAX_ALIASES);
978983
TYPES = malloc(MAX_TYPES * sizeof(type_t));
979984
BLOCK_ARENA = arena_init(DEFAULT_ARENA_SIZE);
980985
INSN_ARENA = arena_init(DEFAULT_ARENA_SIZE);
@@ -996,7 +1001,7 @@ void global_init(void)
9961001

9971002
void global_release(void)
9981003
{
999-
free(MACROS);
1004+
hashmap_free(MACROS_MAP);
10001005
free(TYPES);
10011006
arena_free(BLOCK_ARENA);
10021007
arena_free(INSN_ARENA);

0 commit comments

Comments
 (0)