fixed memory issues

This commit is contained in:
Timerix 2025-02-03 22:30:56 +05:00
parent 422d967165
commit 51ef24bb53
7 changed files with 42 additions and 6 deletions

View File

@ -30,17 +30,22 @@
u32 max_len = ALIGN_TO(initial_len, sizeof(void*)/sizeof(T));\ u32 max_len = ALIGN_TO(initial_len, sizeof(void*)/sizeof(T));\
/* branchless version of max(max_len, __List_min_size) */\ /* branchless version of max(max_len, __List_min_size) */\
max_len += (max_len < __List_min_size) * (__List_min_size - max_len);\ max_len += (max_len < __List_min_size) * (__List_min_size - max_len);\
return List_##T##_construct((T*)malloc(initial_len * sizeof(T)), 0, max_len);\ return List_##T##_construct((T*)malloc(max_len * sizeof(T)), 0, max_len);\
}\ }\
\ \
T* List_##T##_expand(List_##T* ptr, u32 count){\ T* List_##T##_expand(List_##T* ptr, u32 count){\
u32 occupied_len = ptr->len;\ u32 occupied_len = ptr->len;\
u32 expanded_max_len = ptr->max_len;\ u32 expanded_max_len = ptr->max_len;\
expanded_max_len += (expanded_max_len < __List_min_size) * (__List_min_size - expanded_max_len);\
ptr->len += count;\ ptr->len += count;\
while(ptr->len > ptr->max_len){\ while(ptr->len > expanded_max_len){\
expanded_max_len *= 2;\ expanded_max_len *= 2;\
}\ }\
ptr->data = (T*)realloc(ptr->data, expanded_max_len * sizeof(T));\ u32 alloc_size = expanded_max_len * sizeof(T);\
if(ptr->data == NULL)\
ptr->data = (T*)malloc(alloc_size);\
else ptr->data = (T*)realloc(ptr->data, alloc_size);\
ptr->max_len = expanded_max_len;\
return ptr->data + occupied_len;\ return ptr->data + occupied_len;\
}\ }\
\ \

View File

@ -40,7 +40,14 @@ void Section_init(Section* sec, str name){
} }
void Section_free(Section* sec){ void Section_free(Section* sec){
for(u32 i = 0; i < sec->data.len; i++){
free(sec->data.data[i].data.data);
}
free(sec->data.data); free(sec->data.data);
for(u32 i = 0; i < sec->code.len; i++){
free(sec->code.data[i].args.data);
}
free(sec->code.data); free(sec->code.data);
} }

View File

@ -98,17 +98,21 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
} }
if(debug_log){ if(debug_log){
printf("----------------------------------[%s]---------------------------------\n", source_file_name); printf("===========================[%s]===========================\n", source_file_name);
fputs(cmp->code.data, stdout); fputs(cmp->code.data, stdout);
fputc('\n', stdout); fputc('\n', stdout);
} }
if(debug_log)
printf("===================================[lexing]===================================\n");
bool success = Compiler_lex(cmp); bool success = Compiler_lex(cmp);
if(debug_log){ if(debug_log){
printf("------------------------------------[lines]-----------------------------------\n"); printf("------------------------------------[lines]------------------------------------\n");
for(u32 i = 0; i < cmp->line_lengths.len; i++){ for(u32 i = 0; i < cmp->line_lengths.len; i++){
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]); printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
} }
printf("------------------------------------[tokens]-----------------------------------\n"); printf("------------------------------------[tokens]-----------------------------------\n");
for(u32 i = 0; i < cmp->tokens.len; i++){ for(u32 i = 0; i < cmp->tokens.len; i++){
Token t = cmp->tokens.data[i]; Token t = cmp->tokens.data[i];
@ -126,17 +130,22 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
free(tokstr); free(tokstr);
} }
} }
if(!success){ if(!success){
fclose(f); fclose(f);
return false; return false;
} }
if(debug_log)
printf("===================================[parsing]===================================\n");
success = Compiler_parse(cmp); success = Compiler_parse(cmp);
if(!success){ if(!success){
fclose(f); fclose(f);
return false; return false;
} }
if(debug_log)
printf("==================================[compiling]==================================\n");
success = compileFile(cmp, f); success = compileFile(cmp, f);
fclose(f); fclose(f);
if(success){ if(success){

View File

@ -89,6 +89,7 @@ static void parseDataDefinition(Compiler* cmp, str instr_name, DataDefinition* d
} }
free(_instr_name_zero_terminated.data); free(_instr_name_zero_terminated.data);
ddf->element_size = _element_size_bits / 8; ddf->element_size = _element_size_bits / 8;
ddf->data = List_u8_alloc(32);
Token tok = cmp->tokens.data[++cmp->tok_i]; Token tok = cmp->tokens.data[++cmp->tok_i];
if(tok.type != TokenType_Name){ if(tok.type != TokenType_Name){
@ -165,6 +166,7 @@ static void parseOperation(Compiler* cmp, str instr_name, Operation* operPtr){
} }
operPtr->opcode = instr->opcode; operPtr->opcode = instr->opcode;
operPtr->args = List_Argument_alloc(8);
Argument arg = (Argument){ .type = ArgumentType_Unset, .value.i = 0 }; Argument arg = (Argument){ .type = ArgumentType_Unset, .value.i = 0 };
str tok_str = str_null; str tok_str = str_null;
str processed_str = str_null; str processed_str = str_null;

View File

@ -51,5 +51,15 @@ const Instruction* Instruction_getByName(str name){
} }
} }
return HashMap_Instruction_tryGetPtr(instructions_map, name); str name_upper = str_toUpper(name);
Instruction* iptr = HashMap_Instruction_tryGetPtr(instructions_map, name_upper);
free(name_upper.data);
return iptr;
} }
void Instruction_freeSearchStructs(){
if(instructions_map != NULL){
HashMap_Instruction_free(instructions_map);
free(instructions_map);
}
}

View File

@ -35,3 +35,4 @@ typedef struct Instruction {
/// @return ptr to struct or NULL /// @return ptr to struct or NULL
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode); const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
const Instruction* NULLABLE(Instruction_getByName)(str name); const Instruction* NULLABLE(Instruction_getByName)(str name);
void Instruction_freeSearchStructs();

View File

@ -92,6 +92,8 @@ i32 main(const i32 argc, cstr* argv){
exit_code = bootFromImage(image_file); exit_code = bootFromImage(image_file);
} }
// frees global variables to supress valgrind memory leak errors
Instruction_freeSearchStructs();
return exit_code; return exit_code;
} }