[PATCH] Cochran: Fixed code review issues

John Van Ostrand john at vanostrand.com
Fri Jun 2 09:30:21 PDT 2017


Fixed several memory leaks
Removed unused variable
Changed use of int to unsigned int where it made sense and fixed initializations
Used ringbuffer functions
Adjusted enum names to comply
---
 src/cochran_commander.c | 57 ++++++++++++++++++++++---------------------------
 1 file changed, 25 insertions(+), 32 deletions(-)

diff --git a/src/cochran_commander.c b/src/cochran_commander.c
index 4c15541..5ff46a5 100644
--- a/src/cochran_commander.c
+++ b/src/cochran_commander.c
@@ -42,8 +42,8 @@ typedef enum cochran_endian_t {
 } cochran_endian_t;
 
 typedef enum cochran_profile_size_t {
-	PROFILE_FULL_SIZE,
-	PROFILE_READ_SIZE,
+	PROFILE_SIZE_FULL,
+	PROFILE_SIZE_READ,
 } cochran_profile_size_t;
 
 typedef struct cochran_commander_model_t {
@@ -54,7 +54,6 @@ typedef struct cochran_commander_model_t {
 typedef struct cochran_data_t {
 	unsigned char config[1024];
 	unsigned char *logbook;
-	unsigned char *sample;
 
 	unsigned short int dive_count;
 	int fp_dive_num;
@@ -455,18 +454,18 @@ cochran_commander_guess_sample_end_address(cochran_commander_device_t *device, c
 }
 
 
-static int
+static unsigned int
 cochran_commander_profile_size(cochran_commander_device_t *device, cochran_data_t *data, int dive_num, cochran_profile_size_t type)
 {
 
 	const unsigned char *log_entry = data->logbook + dive_num * device->layout->rb_logbook_entry_size;
-	unsigned int sample_start_address = -1;
+	unsigned int sample_start_address = 0xFFFFFFFF;
 	unsigned int sample_end_address = array_uint32_le (log_entry + device->layout->pt_profile_end);
 
-	if (type == PROFILE_FULL_SIZE) {
+	if (type == PROFILE_SIZE_FULL) {
 		// actual size, includes pre-dive events
 		sample_start_address = array_uint32_le (log_entry + device->layout->pt_profile_pre);
-	} else if (type == PROFILE_READ_SIZE) {
+	} else if (type == PROFILE_SIZE_READ) {
 		// read size, only include dive profile
 		sample_start_address = array_uint32_le (log_entry + device->layout->pt_profile_begin);
 	}
@@ -484,14 +483,7 @@ cochran_commander_profile_size(cochran_commander_device_t *device, cochran_data_
 		// Corrupt dive, guess the end address
 		sample_end_address = cochran_commander_guess_sample_end_address(device, data, dive_num);
 
-	// Calculate the size of the profile only
-	int sample_size = sample_end_address - sample_start_address;
-
-	if (sample_size < 0)
-		// Adjust for ring buffer wrap-around
-		sample_size += device->layout->rb_profile_end - device->layout->rb_profile_begin;
-
-	return sample_size;
+	return ringbuffer_distance(sample_start_address, sample_end_address, 0, device->layout->rb_profile_end - device->layout->rb_profile_begin);
 }
 
 
@@ -501,7 +493,7 @@ cochran_commander_profile_size(cochran_commander_device_t *device, cochran_data_
  * Determine the most recent dive without profile data.
  */
 
-static int
+static unsigned int
 cochran_commander_find_fingerprint(cochran_commander_device_t *device, cochran_data_t *data)
 {
 	// We track profile ringbuffer usage to determine which dives have profile data
@@ -536,8 +528,8 @@ cochran_commander_find_fingerprint(cochran_commander_device_t *device, cochran_d
 			break;
 		}
 
-		int sample_size = cochran_commander_profile_size(device, data, i, PROFILE_FULL_SIZE);
-		int read_size = cochran_commander_profile_size(device, data, i, PROFILE_READ_SIZE);
+		unsigned int sample_size = cochran_commander_profile_size(device, data, i, PROFILE_FULL_SIZE);
+		unsigned int read_size = cochran_commander_profile_size(device, data, i, PROFILE_READ_SIZE);
 
 		// Determine if sample exists
 		if (profile_capacity_remaining > 0) {
@@ -550,12 +542,6 @@ cochran_commander_find_fingerprint(cochran_commander_device_t *device, cochran_d
 			// Accumulate read size for progress bar
 			sample_read_size += read_size;
 		}
-
-		if (profile_capacity_remaining < 0) {
-			// There is no profile for this dive
-			sample_size = 0;
-		}
-
 	}
 
 	return sample_read_size;
@@ -790,7 +776,6 @@ cochran_commander_device_foreach (dc_device_t *abstract, dc_dive_callback_t call
 
 	cochran_data_t data;
 	data.logbook = NULL;
-	data.sample = NULL;
 
 	// Calculate max data sizes
 	unsigned int max_config = sizeof(data.config);
@@ -839,11 +824,13 @@ cochran_commander_device_foreach (dc_device_t *abstract, dc_dive_callback_t call
 
 	// Request log book
 	rc = cochran_commander_read(device, &progress, 0, data.logbook, data.logbook_size);
-	if (rc != DC_STATUS_SUCCESS)
-		return rc;
+	if (rc != DC_STATUS_SUCCESS) {
+		status = rc;
+		goto error;
+	}
 
 	// Locate fingerprint, recent dive with invalid profile and calc read size
-	int profile_read_size = cochran_commander_find_fingerprint(device, &data);
+	unsigned int profile_read_size = cochran_commander_find_fingerprint(device, &data);
 	// Update progress indicator with new maximum
 	progress.maximum -= (max_sample - profile_read_size);
 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
@@ -898,13 +885,18 @@ cochran_commander_device_foreach (dc_device_t *abstract, dc_dive_callback_t call
 		int tries = 0;
 		// Read profile data
 		if (sample_size) {
+			if (sample_end_address == 0xFFFFFFFF)
+				// Corrupt dive, guess the end address
+				sample_end_address = cochran_commander_guess_sample_end_address(device, data, dive_num);
+
 			if (sample_start_address <= sample_end_address) {
 				do {
 					rc = cochran_commander_read (device, &progress, sample_start_address, dive + device->layout->rb_logbook_entry_size, sample_size);
 				} while (rc != DC_STATUS_SUCCESS && tries++ < 3);
 				if (rc != DC_STATUS_SUCCESS) {
 					ERROR (abstract->context, "Failed to read the sample data.");
-					return rc;
+					status = rc
+					goto error;
 				}
 			} else {
 				// It wrapped the buffer, copy two sections
@@ -916,7 +908,8 @@ cochran_commander_device_foreach (dc_device_t *abstract, dc_dive_callback_t call
 				} while (rc != DC_STATUS_SUCCESS && tries++ < 3);
 				if (rc != DC_STATUS_SUCCESS) {
 					ERROR (abstract->context, "Failed to read the sample data.");
-					return rc;
+					status = rc
+					goto error;
 				}
 				tries = 0;
 				do {
@@ -924,7 +917,8 @@ cochran_commander_device_foreach (dc_device_t *abstract, dc_dive_callback_t call
 				} while (rc != DC_STATUS_SUCCESS && tries++ < 3);
 				if (rc != DC_STATUS_SUCCESS) {
 					ERROR (abstract->context, "Failed to read the sample data.");
-					return rc;
+					status = rc
+					goto error;
 				}
 			}
 		}
@@ -939,6 +933,5 @@ cochran_commander_device_foreach (dc_device_t *abstract, dc_dive_callback_t call
 
 error:
 	free(data.logbook);
-	free(data.sample);
 	return status;
 }
-- 
2.4.11



More information about the devel mailing list