new memory free strategy that relies on goto
This commit is contained in:
46
src/main.c
46
src/main.c
@@ -68,7 +68,6 @@ void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo
|
|||||||
info->data_last_index++;
|
info->data_last_index++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// printf("%f, ", ((float *)col1_data)[row]);
|
|
||||||
} else {
|
} else {
|
||||||
printf("NULL");
|
printf("NULL");
|
||||||
}
|
}
|
||||||
@@ -80,44 +79,44 @@ void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
duckdb_database db;
|
||||||
|
duckdb_connection conn;
|
||||||
|
duckdb_result lap_count_res;
|
||||||
|
LapInfo *session_data = NULL;
|
||||||
|
double *lap_timestamps = NULL;
|
||||||
|
|
||||||
printf("\nHello from HardCompound!\n");
|
printf("\nHello from HardCompound!\n");
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
printf("You need to specify a duckdb file path! Terminating!\n");
|
printf("You need to specify a duckdb file path! Terminating!\n");
|
||||||
exit(ERR_ARGUMENT);
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_valid_path(argv[1])) {
|
if (!is_valid_path(argv[1])) {
|
||||||
printf("The file you gave is not valid. Exiting!");
|
printf("The file you gave is not valid. Exiting!");
|
||||||
exit(ERR_FILE);
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("First argument: %s\n", argv[1]);
|
printf("First argument: %s\n", argv[1]);
|
||||||
|
|
||||||
duckdb_database db;
|
|
||||||
if (duckdb_open(argv[1], &db) == DuckDBError) {
|
if (duckdb_open(argv[1], &db) == DuckDBError) {
|
||||||
printf("Error opening duckdb file, terminating!\n");
|
printf("Error opening duckdb file, terminating!\n");
|
||||||
exit(ERR_DATABASE);
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
duckdb_connection conn;
|
|
||||||
if (duckdb_connect(db, &conn) == DuckDBError) {
|
if (duckdb_connect(db, &conn) == DuckDBError) {
|
||||||
printf("Error connecting to the duckdb database! Terminating!\n");
|
printf("Error connecting to the duckdb database! Terminating!\n");
|
||||||
duckdb_close(&db);
|
goto cleanup;
|
||||||
exit(ERR_CONNECTION);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get how many laps is in the session and allocate that many LapInfo-s
|
// get how many laps is in the session and allocate that many LapInfo-s
|
||||||
duckdb_result lap_count_res;
|
|
||||||
int32_t lap_count = 0;
|
int32_t lap_count = 0;
|
||||||
duckdb_query(conn, "Select count(value) from Lap\n", &lap_count_res);
|
duckdb_query(conn, "Select count(value) from Lap\n", &lap_count_res);
|
||||||
|
|
||||||
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(lap_count_res);
|
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(lap_count_res);
|
||||||
if (!data_chunk) {
|
if (!data_chunk) {
|
||||||
printf("Error in retrieving laps");
|
printf("Error in retrieving laps");
|
||||||
duckdb_disconnect(&conn);
|
goto cleanup;
|
||||||
duckdb_close(&db);
|
|
||||||
exit(ERR_QUERY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// set column
|
// set column
|
||||||
@@ -136,22 +135,22 @@ int main(int argc, char **argv) {
|
|||||||
duckdb_destroy_result(&lap_count_res);
|
duckdb_destroy_result(&lap_count_res);
|
||||||
|
|
||||||
printf("Lap count: %d\n", lap_count);
|
printf("Lap count: %d\n", lap_count);
|
||||||
LapInfo *session_data = calloc(lap_count, sizeof(LapInfo));
|
session_data = calloc(lap_count, sizeof(LapInfo));
|
||||||
double *lap_timestamps = calloc(lap_count, sizeof(double));
|
lap_timestamps = calloc(lap_count, sizeof(double));
|
||||||
|
|
||||||
if (!session_data) {
|
if (!session_data) {
|
||||||
printf("Session data failed to allocate!");
|
printf("Session data failed to allocate!");
|
||||||
exit(ERR_MEMORY);
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lap_timestamps) {
|
if (!lap_timestamps) {
|
||||||
printf("Lap timestaps array failed to init!");
|
printf("Lap timestaps array failed to init!");
|
||||||
exit(ERR_MEMORY);
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
duckdb_result lap_timestamps_res;
|
duckdb_result lap_timestamps_res;
|
||||||
if (duckdb_query(conn, "Select ts as DOUBLE from Lap\n", &lap_timestamps_res) == DuckDBError) {
|
if (duckdb_query(conn, "Select ts as DOUBLE from Lap\n", &lap_timestamps_res) == DuckDBError) {
|
||||||
exit(ERR_QUERY);
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
int current_index = 0;
|
int current_index = 0;
|
||||||
@@ -185,12 +184,19 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
free(session_data);
|
// get the per laptime data
|
||||||
|
|
||||||
printf("Shutting down HardCompound!\n");
|
printf("Shutting down HardCompound!\n");
|
||||||
|
|
||||||
printf("Closing database and any connection.\n");
|
printf("Closing database and any connection.\n");
|
||||||
|
goto cleanup;
|
||||||
|
exit:
|
||||||
|
return EXIT_OK;
|
||||||
|
cleanup:
|
||||||
|
free(NULL);
|
||||||
|
free(session_data);
|
||||||
|
free(lap_timestamps);
|
||||||
duckdb_disconnect(&conn);
|
duckdb_disconnect(&conn);
|
||||||
duckdb_close(&db);
|
duckdb_close(&db);
|
||||||
|
goto exit;
|
||||||
return EXIT_OK;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user