1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-11-12 22:45:54 +05:30

C: reverse-ramdisk.c: fix memory usage in tf_alloc

This commit is contained in:
Intel A80486DX2-66 2024-02-22 21:10:55 +03:00
parent 9c47cde541
commit bae1b1a301
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -13,7 +13,6 @@
* GCC/Clang/TCC: Compile with -DTEST to set macro TEST as defined, with
* -DDEBUG to enable debug mode
*
* To-Do: Test: Fix the bug with invalid next size to reallocate
* To-Do: Test: Automate the test verification
*
* Author: Intel A80486DX2-66
@ -109,7 +108,17 @@ int tf_alloc(size_t n, size_t type_size) {
return -1;
}
// Allocate memory for the TempFile struct
// Allocate/reallocate memory for all TempFiles
temp_files = temp_files == NULL ?
malloc(sizeof(TempFile))
:
realloc(temp_files, (num_temp_files + 1) * sizeof(TempFile));
if (temp_files == NULL) {
LINE_FAIL(-2);
return -1;
}
// Allocate memory for this TempFile
TempFile* temp_file = malloc(sizeof(TempFile));
if (temp_file == NULL) {
LINE_FAIL(-2);
@ -122,16 +131,6 @@ int tf_alloc(size_t n, size_t type_size) {
temp_file->file_path = strdup(file_path);
temp_file->file = file;
// Allocate/reallocate memory for the temp_files structure
temp_files = temp_files == NULL ?
malloc(sizeof(TempFile))
:
realloc(temp_files, num_temp_files * sizeof(TempFile));
if (temp_files == NULL) {
LINE_FAIL(-2);
return -1;
}
// Add the temp file to the array
temp_files[num_temp_files++] = *temp_file;