@@ -1095,13 +1095,19 @@ void global_init(void)
10951095 elf_code_start = ELF_START + elf_header_len ;
10961096
10971097 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 */
10991100 BLOCK_ARENA = arena_init (DEFAULT_ARENA_SIZE ); /* Variables/blocks */
11001101 INSN_ARENA = arena_init (LARGE_ARENA_SIZE ); /* Instructions - high usage */
11011102 BB_ARENA = arena_init (SMALL_ARENA_SIZE ); /* Basic blocks - low usage */
11021103 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 * ));
11051111 SOURCE = strbuf_create (MAX_SOURCE );
11061112 FUNC_MAP = hashmap_create (DEFAULT_FUNCS_SIZE );
11071113 INCLUSION_MAP = hashmap_create (DEFAULT_INCLUSIONS_SIZE );
@@ -1125,25 +1131,24 @@ void global_release(void)
11251131 lexer_cleanup ();
11261132
11271133 hashmap_free (MACROS_MAP );
1128- free (TYPES );
11291134 arena_free (BLOCK_ARENA );
11301135 arena_free (INSN_ARENA );
11311136 arena_free (BB_ARENA );
11321137 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 */
11401139
1140+ strbuf_free (SOURCE );
11411141 strbuf_free (elf_code );
11421142 strbuf_free (elf_data );
11431143 strbuf_free (elf_header );
11441144 strbuf_free (elf_symtab );
11451145 strbuf_free (elf_strtab );
11461146 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 );
11471152}
11481153
11491154/* Reports an error without specifying a position */
0 commit comments