Skip to content

Commit e3154e3

Browse files
authored
Merge pull request #209 from icgmilk/fix_hashmap
Replace MACROS with hashmap
2 parents c508a38 + 8fe59b4 commit e3154e3

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#define MAX_STRTAB 65536
3333
#define MAX_HEADER 1024
3434
#define MAX_SECTION 1024
35-
#define MAX_ALIASES 1024
35+
#define MAX_ALIASES 128
3636
#define MAX_CONSTANTS 1024
3737
#define MAX_CASES 128
3838
#define MAX_NESTING 128

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;
@@ -669,28 +668,34 @@ bool remove_alias(char *alias)
669668

670669
macro_t *add_macro(char *name)
671670
{
672-
macro_t *ma = &MACROS[macros_idx++];
673-
strcpy(ma->name, name);
671+
macro_t *ma = hashmap_get(MACROS_MAP, name);
672+
if (!ma) {
673+
ma = malloc(sizeof(macro_t));
674+
if (!ma) {
675+
printf("Failed to allocate macro_t\n");
676+
return NULL;
677+
}
678+
strcpy(ma->name, name);
679+
hashmap_put(MACROS_MAP, name, ma);
680+
}
674681
ma->disabled = false;
675682
return ma;
676683
}
677684

678685
macro_t *find_macro(char *name)
679686
{
680-
for (int i = 0; i < macros_idx; i++) {
681-
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name))
682-
return &MACROS[i];
683-
}
687+
macro_t *ma = hashmap_get(MACROS_MAP, name);
688+
if (ma && !ma->disabled)
689+
return ma;
684690
return NULL;
685691
}
686692

687693
bool remove_macro(char *name)
688694
{
689-
for (int i = 0; i < macros_idx; i++) {
690-
if (!MACROS[i].disabled && !strcmp(name, MACROS[i].name)) {
691-
MACROS[i].disabled = true;
692-
return true;
693-
}
695+
macro_t *ma = hashmap_get(MACROS_MAP, name);
696+
if (ma) {
697+
ma->disabled = true;
698+
return true;
694699
}
695700
return false;
696701
}
@@ -1086,7 +1091,7 @@ void global_init(void)
10861091
{
10871092
elf_code_start = ELF_START + elf_header_len;
10881093

1089-
MACROS = malloc(MAX_ALIASES * sizeof(macro_t));
1094+
MACROS_MAP = hashmap_create(MAX_ALIASES);
10901095
TYPES = malloc(MAX_TYPES * sizeof(type_t));
10911096
BLOCK_ARENA = arena_init(DEFAULT_ARENA_SIZE);
10921097
INSN_ARENA = arena_init(DEFAULT_ARENA_SIZE);
@@ -1108,7 +1113,7 @@ void global_init(void)
11081113

11091114
void global_release(void)
11101115
{
1111-
free(MACROS);
1116+
hashmap_free(MACROS_MAP);
11121117
free(TYPES);
11131118
arena_free(BLOCK_ARENA);
11141119
arena_free(INSN_ARENA);

0 commit comments

Comments
 (0)