@@ -28,13 +28,12 @@ int macro_return_idx;
28
28
29
29
/* Global objects */
30
30
31
- macro_t * MACROS ;
32
- int macros_idx = 0 ;
33
-
34
31
/* FUNC_MAP is used to integrate function storing and boost lookup
35
32
* performance, currently it uses FNV-1a hash function to hash function
36
33
* name.
37
34
*/
35
+
36
+ hashmap_t * MACROS_MAP ;
38
37
hashmap_t * FUNC_MAP ;
39
38
hashmap_t * ALIASES_MAP ;
40
39
hashmap_t * CONSTANTS_MAP ;
@@ -669,28 +668,34 @@ bool remove_alias(char *alias)
669
668
670
669
macro_t * add_macro (char * name )
671
670
{
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
+ }
674
681
ma -> disabled = false;
675
682
return ma ;
676
683
}
677
684
678
685
macro_t * find_macro (char * name )
679
686
{
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 ;
684
690
return NULL ;
685
691
}
686
692
687
693
bool remove_macro (char * name )
688
694
{
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;
694
699
}
695
700
return false;
696
701
}
@@ -1086,7 +1091,7 @@ void global_init(void)
1086
1091
{
1087
1092
elf_code_start = ELF_START + elf_header_len ;
1088
1093
1089
- MACROS = malloc (MAX_ALIASES * sizeof ( macro_t ) );
1094
+ MACROS_MAP = hashmap_create (MAX_ALIASES );
1090
1095
TYPES = malloc (MAX_TYPES * sizeof (type_t ));
1091
1096
BLOCK_ARENA = arena_init (DEFAULT_ARENA_SIZE );
1092
1097
INSN_ARENA = arena_init (DEFAULT_ARENA_SIZE );
@@ -1108,7 +1113,7 @@ void global_init(void)
1108
1113
1109
1114
void global_release (void )
1110
1115
{
1111
- free ( MACROS );
1116
+ hashmap_free ( MACROS_MAP );
1112
1117
free (TYPES );
1113
1118
arena_free (BLOCK_ARENA );
1114
1119
arena_free (INSN_ARENA );
0 commit comments