From 6c39ab8b3fe9d3eaec8c375b91329ae2b6629ecc Mon Sep 17 00:00:00 2001
From: TJFriedl <tjfriedl17@gmail.com>
Date: Tue, 7 Nov 2023 01:13:05 -0600
Subject: [PATCH] Finished free_graph method, needs testing implementation

---
 .../src/computation_graph/computation_graph.c | 25 +++++++++++++++++++
 .../src/computation_graph/computation_graph.h |  5 ++++
 2 files changed, 30 insertions(+)

diff --git a/quad/src/computation_graph/computation_graph.c b/quad/src/computation_graph/computation_graph.c
index 634823ed0..18c220068 100644
--- a/quad/src/computation_graph/computation_graph.c
+++ b/quad/src/computation_graph/computation_graph.c
@@ -27,6 +27,31 @@ struct computation_graph *create_graph() {
     return the_graph;
 }
 
+struct computation_graph free_graph(struct computation_graph *graph) {
+    if (graph == NULL) return; // Do nothing if memory has already been cleared
+
+    for (int i = 0; i < graph->size; i++) {
+        if (graph_node_exists(graph, i)) {
+            struct graph_node *node = &graph->nodes[i];
+
+            // THESE ARE SEPERATED TO TEST FOR NOW -> There's gotta be a more concise way to do this.
+            if (node->name != NULL) free(node->name);
+            if (node->state != NULL) free(node->state);
+            if (node->output_values != NULL) free(node->output_values);
+            if (node->param_values != NULL) free(node->param_values);
+            if (node->input_srcs != NULL) free(node->input_srcs);
+
+
+        }
+    }
+    // These need to be tested as well... should just be able to be grouped together... right?
+    if (graph->nodes != NULL) free(graph->nodes);
+    if (graph->node_existence != NULL) free(graph->node_existence);
+
+    // Finally...
+    free(graph);
+}
+
 static void reset_node_rec(struct computation_graph* graph, int node_id, int depth) {
     if (depth > GRAPH_MAX_DEPTH) {
         return;
diff --git a/quad/src/computation_graph/computation_graph.h b/quad/src/computation_graph/computation_graph.h
index 671beb5f4..97c369bce 100644
--- a/quad/src/computation_graph/computation_graph.h
+++ b/quad/src/computation_graph/computation_graph.h
@@ -67,6 +67,11 @@ struct graph_node {
  */
 struct computation_graph *create_graph();
 
+/**
+ * Frees memory allocated for computation graph
+*/
+struct computation_graph free_graph(struct computation_graph *graph);
+
 /*
  * Defines which node's output gets its value passed into the input of a different node.
  * Will call reset for each node which was previously orphaned, but is now connected to the graph
-- 
GitLab