@@ -1095,13 +1095,19 @@ void global_init(void)
1095
1095
elf_code_start = ELF_START + elf_header_len ;
1096
1096
1097
1097
MACROS_MAP = hashmap_create (MAX_ALIASES );
1098
- TYPES = malloc (MAX_TYPES * sizeof (type_t ));
1098
+
1099
+ /* Initialize arenas first so we can use them for allocation */
1099
1100
BLOCK_ARENA = arena_init (DEFAULT_ARENA_SIZE ); /* Variables/blocks */
1100
1101
INSN_ARENA = arena_init (LARGE_ARENA_SIZE ); /* Instructions - high usage */
1101
1102
BB_ARENA = arena_init (SMALL_ARENA_SIZE ); /* Basic blocks - low usage */
1102
1103
HASHMAP_ARENA = arena_init (DEFAULT_ARENA_SIZE ); /* Hash nodes */
1103
- GENERAL_ARENA = arena_init (SMALL_ARENA_SIZE ); /* Misc - low usage */
1104
- PH2_IR_FLATTEN = malloc (MAX_IR_INSTR * sizeof (ph2_ir_t * ));
1104
+ GENERAL_ARENA =
1105
+ arena_init (DEFAULT_ARENA_SIZE ); /* For TYPES and PH2_IR_FLATTEN */
1106
+
1107
+ /* Use arena allocation for better memory management */
1108
+ TYPES = arena_alloc (GENERAL_ARENA , MAX_TYPES * sizeof (type_t ));
1109
+ PH2_IR_FLATTEN =
1110
+ arena_alloc (GENERAL_ARENA , MAX_IR_INSTR * sizeof (ph2_ir_t * ));
1105
1111
SOURCE = strbuf_create (MAX_SOURCE );
1106
1112
FUNC_MAP = hashmap_create (DEFAULT_FUNCS_SIZE );
1107
1113
INCLUSION_MAP = hashmap_create (DEFAULT_INCLUSIONS_SIZE );
@@ -1125,25 +1131,24 @@ void global_release(void)
1125
1131
lexer_cleanup ();
1126
1132
1127
1133
hashmap_free (MACROS_MAP );
1128
- free (TYPES );
1129
1134
arena_free (BLOCK_ARENA );
1130
1135
arena_free (INSN_ARENA );
1131
1136
arena_free (BB_ARENA );
1132
1137
arena_free (HASHMAP_ARENA );
1133
- arena_free (GENERAL_ARENA );
1134
- free (PH2_IR_FLATTEN );
1135
- strbuf_free (SOURCE );
1136
- hashmap_free (FUNC_MAP );
1137
- hashmap_free (INCLUSION_MAP );
1138
- hashmap_free (ALIASES_MAP );
1139
- hashmap_free (CONSTANTS_MAP );
1138
+ arena_free (GENERAL_ARENA ); /* free TYPES and PH2_IR_FLATTEN */
1140
1139
1140
+ strbuf_free (SOURCE );
1141
1141
strbuf_free (elf_code );
1142
1142
strbuf_free (elf_data );
1143
1143
strbuf_free (elf_header );
1144
1144
strbuf_free (elf_symtab );
1145
1145
strbuf_free (elf_strtab );
1146
1146
strbuf_free (elf_section );
1147
+
1148
+ hashmap_free (FUNC_MAP );
1149
+ hashmap_free (INCLUSION_MAP );
1150
+ hashmap_free (ALIASES_MAP );
1151
+ hashmap_free (CONSTANTS_MAP );
1147
1152
}
1148
1153
1149
1154
/* Reports an error without specifying a position */
0 commit comments