diff --git a/quad/src/computation_graph/computation_graph.c b/quad/src/computation_graph/computation_graph.c index 634823ed0934b51eca500558d55ba509748fc62d..18c2200682822263bc1cc9e670630c77f48611ee 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 671beb5f411dc42b48767e05551959444288ca1e..97c369bce91188449ecd10e69e46a35e69cc99d0 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