From 05e3956a0cd8996234a0945eea040a32810f0b54 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Mon, 9 Nov 2015 11:22:51 +0100 Subject: [PATCH] Minor changes in code to improve readability (freeing some character strings earlier) --- src/obiarray.c | 2 +- src/obidms.c | 6 ++--- src/obidmscolumn.c | 66 ++++++++++++---------------------------------- src/obidmscolumn.h | 2 ++ 4 files changed, 23 insertions(+), 53 deletions(-) diff --git a/src/obiarray.c b/src/obiarray.c index 7828762..628670c 100644 --- a/src/obiarray.c +++ b/src/obiarray.c @@ -988,7 +988,7 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) } -int obi_close_array(OBIDMS_array_p array) +int obi_close_array(OBIDMS_array_p array) // TODO when do we call it? (as multiple threads can be using the same array) { int ret_val = 0; diff --git a/src/obidms.c b/src/obidms.c index 2004a7a..20e69c8 100644 --- a/src/obidms.c +++ b/src/obidms.c @@ -145,12 +145,14 @@ OBIDMS_p obi_create_dms(const char* dms_name) free(directory_name); return NULL; } + + free(directory_name); + dms_file_descriptor = dirfd(dms_dir); if (dms_file_descriptor < 0) { obi_set_errno(OBIDMS_UNKNOWN_ERROR); obidebug(1, "\nProblem getting the file descriptor of a newly created OBIDMS directory"); - free(directory_name); return NULL; } @@ -159,11 +161,9 @@ OBIDMS_p obi_create_dms(const char* dms_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nProblem creating an arrays directory"); - free(directory_name); return NULL; } - free(directory_name); return obi_open_dms(dms_name); } diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index 1fe86e8..216fb9c 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -234,13 +234,14 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ } } + free(version_file_name); + // Test if the version file size is ok if (lseek(version_file_descriptor, 0, SEEK_END) < loc_size) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError testing if a version file size is ok"); close(version_file_descriptor); - free(version_file_name); return -1; } @@ -250,7 +251,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError preparing a version file for locking"); close(version_file_descriptor); - free(version_file_name); return -1; } @@ -260,7 +260,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError locking a version file"); close(version_file_descriptor); - free(version_file_name); return -1; } @@ -270,7 +269,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError reading the endianness of a version file"); close(version_file_descriptor); - free(version_file_name); return -1; } @@ -280,7 +278,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obi_set_errno(OBICOL_BAD_ENDIAN_ERROR); obidebug(1, "\nEndianness of a version file is incorrect"); close(version_file_descriptor); - free(version_file_name); return -1; } @@ -290,7 +287,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError reading a version file"); close(version_file_descriptor); - free(version_file_name); return -1; } @@ -302,7 +298,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError writing a new version number in a version file"); close(version_file_descriptor); - free(version_file_name); return -1; } @@ -311,7 +306,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError writing a new version number in a version file"); close(version_file_descriptor); - free(version_file_name); return -1; } @@ -321,7 +315,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError preparing the unlocking of a version file"); close(version_file_descriptor); - free(version_file_name); return -1; } @@ -331,12 +324,10 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError unlocking a version file"); close(version_file_descriptor); - free(version_file_name); return -1; } close(version_file_descriptor); - free(version_file_name); return new_version_number; } @@ -378,12 +369,13 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo return -1; } + free(version_file_name); + // Lock the file if (lockf(version_file_descriptor, F_LOCK, loc_size) < 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError locking a version file"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -393,7 +385,6 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError truncating a version file"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -403,7 +394,6 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError changing offset of a version file"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -415,7 +405,6 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError writing endianness of a version file"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -425,7 +414,6 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError writing version number in a version file"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -435,7 +423,6 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError preparing a version file for unlocking"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -445,13 +432,11 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError unlocking a version file"); - free(version_file_name); close(version_file_descriptor); return -1; } close(version_file_descriptor); - free(version_file_name); return version_number; } @@ -511,12 +496,13 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire return -1; } + free(version_file_name); + // Check that the version file size is ok if (lseek(version_file_descriptor, 0, SEEK_END) < loc_size) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError testing if a version file size is ok"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -526,7 +512,6 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError setting the offset of a version file to 0"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -536,7 +521,6 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError reading the endianness of a version file"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -546,7 +530,6 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire { obi_set_errno(OBICOL_BAD_ENDIAN_ERROR); obidebug(1, "\nEndianness of a version file is incorrect"); - free(version_file_name); close(version_file_descriptor); return -1; } @@ -556,12 +539,10 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError reading the latest version number in a version file"); - free(version_file_name); close(version_file_descriptor); return -1; } - free(version_file_name); close(version_file_descriptor); return latest_version_number; @@ -731,13 +712,14 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, return NULL; } + free(column_file_name); + // Truncate the column file to the right size if (ftruncate(column_file_descriptor, file_size) < 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError truncating a column file to the right size"); close(column_file_descriptor); - free(column_file_name); return NULL; } @@ -748,7 +730,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError allocating the memory for the column structure"); close(column_file_descriptor); - free(column_file_name); return NULL; } @@ -768,7 +749,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError mmapping the header of a column"); close(column_file_descriptor); - free(column_file_name); free(new_column); return NULL; } @@ -787,7 +767,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, obidebug(1, "\nError mmapping the data of a column"); munmap(new_column->header, header_size); close(column_file_descriptor); - free(column_file_name); free(new_column); return NULL; } @@ -819,7 +798,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, obidebug(1, "\nError opening or creating the array associated with a column"); munmap(new_column->header, header_size); close(column_file_descriptor); - free(column_file_name); free(new_column); return NULL; } @@ -830,7 +808,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, // Fill the data with NA values obi_ini_to_NA_values(new_column, 0, nb_lines); - free(column_file_name); close(column_file_descriptor); return new_column; @@ -900,6 +877,8 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, return NULL; } + free(column_file_name); + // Allocate the memory for the column structure column = (OBIDMS_column_p) malloc(sizeof(OBIDMS_column_t)); if (column == NULL) @@ -907,7 +886,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError allocating the memory for a column structure"); close(column_file_descriptor); - free(column_file_name); return NULL; } @@ -927,7 +905,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError mmapping the header of a column"); close(column_file_descriptor); - free(column_file_name); free(column); return NULL; } @@ -952,7 +929,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, obidebug(1, "\nError mmapping the data of a column"); munmap(column->header, header_size); close(column_file_descriptor); - free(column_file_name); free(column); return NULL; } @@ -968,14 +944,12 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, obidebug(1, "\nError opening the array associated with a column"); munmap(column->header, header_size); close(column_file_descriptor); - free(column_file_name); free(column); return NULL; } column->array = array; } - free(column_file_name); close(column_file_descriptor); return column; @@ -989,7 +963,7 @@ OBIDMS_column_p obi_clone_column(OBIDMS_p dms, { OBIDMS_column_p column_to_clone; OBIDMS_column_p new_column; - index_t nb_lines; + index_t nb_lines; index_t nb_elements_per_line; size_t data_size; OBIType_t data_type; @@ -1123,13 +1097,14 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece return -1; } + free(column_file_name); + // Unmap the data before truncating the file data_size = obi_array_sizeof((column->header)->data_type, (column->header)->line_count, (column->header)->nb_elements_per_line); if (munmap(column->data, data_size) < 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError munmapping the data of a column before truncating"); - free(column_file_name); close(column_file_descriptor); return -1; } @@ -1141,7 +1116,6 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError truncating a column file at the number of lines used"); - free(column_file_name); close(column_file_descriptor); return -1; } @@ -1159,7 +1133,6 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError re-mmapping the data of a column after truncating"); - free(column_file_name); close(column_file_descriptor); return -1; } @@ -1167,7 +1140,6 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece // Set line_count to the new line count (column->header)->line_count = new_line_count; - free(column_file_name); close(column_file_descriptor); return 0; @@ -1213,6 +1185,8 @@ int obi_enlarge_column(OBIDMS_column_p column) return -1; } + free(column_file_name); + // Calculate the new file size old_line_count = (column->header)->line_count; new_line_count = old_line_count * COLUMN_GROWTH_FACTOR; @@ -1221,7 +1195,6 @@ int obi_enlarge_column(OBIDMS_column_p column) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError enlarging a column file: new line count greater than the maximum allowed"); - free(column_file_name); close(column_file_descriptor); return -1; } @@ -1235,7 +1208,6 @@ int obi_enlarge_column(OBIDMS_column_p column) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError enlarging a column file"); - free(column_file_name); close(column_file_descriptor); return -1; } @@ -1255,7 +1227,6 @@ int obi_enlarge_column(OBIDMS_column_p column) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError re-mmapping the data of a column after enlarging the file"); - free(column_file_name); close(column_file_descriptor); return -1; } @@ -1268,7 +1239,6 @@ int obi_enlarge_column(OBIDMS_column_p column) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError munmapping the data of a column before enlarging"); - free(column_file_name); close(column_file_descriptor); return -1; } @@ -1285,7 +1255,6 @@ int obi_enlarge_column(OBIDMS_column_p column) { obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError re-mmapping the data of a column after enlarging the file"); - free(column_file_name); close(column_file_descriptor); return -1; } @@ -1297,7 +1266,6 @@ int obi_enlarge_column(OBIDMS_column_p column) // Initialize new data lines to NA obi_ini_to_NA_values(column, old_line_count, new_line_count - old_line_count); - free(column_file_name); close(column_file_descriptor); return 0; @@ -1418,6 +1386,8 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* return NULL; } + free(column_file_name); + // Fill the header structure header = mmap(NULL, header_size, @@ -1432,13 +1402,11 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* obi_set_errno(OBICOL_UNKNOWN_ERROR); obidebug(1, "\nError mmapping the header of a column"); close(column_file_descriptor); - free(column_file_name); return NULL; } // TODO Check endianness? - free(column_file_name); close(column_file_descriptor); return header; diff --git a/src/obidmscolumn.h b/src/obidmscolumn.h index 4b32bce..587c8d5 100644 --- a/src/obidmscolumn.h +++ b/src/obidmscolumn.h @@ -341,6 +341,8 @@ index_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const cha /** * @brief Formats a date in a way that is easy to read. * + * @warning The pointer returned must be freed by the caller. + * * @param date A date. * * @returns The date formatted in a way that is easy to read.