diff --git a/quad/src/computation_graph/computation_graph.c b/quad/src/computation_graph/computation_graph.c index 81ebbf44e5dbdd5e07d039619d26093c4f53ce16..92daf28a55dc2d1117bbc1ba03f28add913ec50a 100644 --- a/quad/src/computation_graph/computation_graph.c +++ b/quad/src/computation_graph/computation_graph.c @@ -102,18 +102,16 @@ struct node_src graph_get_source(struct computation_graph *graph, int node_id, i int graph_add_node(struct computation_graph *graph, const char* name, - const struct graph_node_type *type, - void *state) { + const struct graph_node_type *type) { assert(type->n_inputs <= GRAPH_MAX_INPUTS); int new_id = graph->n_nodes; - return graph_add_node_id(graph, new_id, name, type, state); + return graph_add_node_id(graph, new_id, name, type); } int graph_add_node_id(struct computation_graph *graph, int id, const char *name, - const struct graph_node_type *type, - void *state) { + const struct graph_node_type *type) { if (id >= graph->size) { size_t old_size = graph->size; size_t new_size = old_size == 0 ? 8 : id * 2; // Hold twice the given ID @@ -135,16 +133,17 @@ int graph_add_node_id(struct computation_graph *graph, struct graph_node *new_node = &graph->nodes[id]; new_node->name = strdup(name); new_node->type = type; - new_node->state = state; + new_node->state = malloc(type->state_size); new_node->n_children = 0; new_node->updated = 1; - new_node->output_values = malloc(type->n_outputs * sizeof(double)); + new_node->output_values = calloc(type->n_outputs, sizeof(double)); new_node->param_values = calloc(type->n_params, sizeof(double)); new_node->input_srcs = malloc(type->n_inputs * sizeof(struct node_src)); // Check that malloc succeeded in every case which memory was requested if ((type->n_outputs && !new_node->output_values) || (type->n_params && !new_node->param_values) || - (type->n_inputs && !new_node->input_srcs)) { + (type->n_inputs && !new_node->input_srcs) || + (type->state_size && !new_node->state)) { return -1; } int i; diff --git a/quad/src/computation_graph/computation_graph.h b/quad/src/computation_graph/computation_graph.h index adddf5119ff742f9f2a603936c8d3b59ae4c6deb..23a8fb1efbf5f62704e3fbc28beafe6868514a8f 100644 --- a/quad/src/computation_graph/computation_graph.h +++ b/quad/src/computation_graph/computation_graph.h @@ -87,8 +87,7 @@ struct node_src graph_get_source(struct computation_graph *graph, int node_id, i */ int graph_add_node(struct computation_graph *graph, const char *name, - const struct graph_node_type *type, - void *state); + const struct graph_node_type *type); /* * Similar to graph_add_node, but adds with a specific ID @@ -99,8 +98,7 @@ int graph_add_node(struct computation_graph *graph, int graph_add_node_id(struct computation_graph *graph, int id, const char *name, - const struct graph_node_type *type, - void *state); + const struct graph_node_type *type); /* * Returns the value at the output of the requested node for the requested output. diff --git a/quad/src/computation_graph/test/test_computation_graph.c b/quad/src/computation_graph/test/test_computation_graph.c index b0b6a14505ce0ca581f02ff82a2d1d010efe42b5..e0882d4d1f2a2c89ede879e1f4f2f4c656e45fca 100644 --- a/quad/src/computation_graph/test/test_computation_graph.c +++ b/quad/src/computation_graph/test/test_computation_graph.c @@ -244,13 +244,13 @@ int graph_test_get_source_null() { int graph_test_add_by_id() { struct computation_graph *graph = create_graph(); int desired_id = 87; - int add_block = graph_add_node_id(graph, desired_id, "Add", &node_add_type, NULL); + int add_block = graph_add_node_id(graph, desired_id, "Add", &node_add_type); if (add_block != desired_id) { return -1; } - int const1 = graph_add_node_id(graph, 12, "const1", &node_const_type, NULL); + int const1 = graph_add_node_id(graph, 12, "const1", &node_const_type); graph_set_param_val(graph, const1, CONST_SET, 3.5); - int const2 = graph_add_node_id(graph, 123, "const2", &node_const_type, NULL); + int const2 = graph_add_node_id(graph, 123, "const2", &node_const_type); graph_set_param_val(graph, const2, CONST_SET, 2.5); graph_set_source(graph, add_block, ADD_SUMMAND1, const1, CONST_VAL); graph_set_source(graph, add_block, ADD_SUMMAND2, const2, CONST_VAL); diff --git a/quad/src/graph_blocks/graph_blocks.c b/quad/src/graph_blocks/graph_blocks.c index fe92a80106b2ddebe30a6a826f112ce6d2d66e97..1ecda7ad6886b5d6810f863226f05cfb7dd00191 100644 --- a/quad/src/graph_blocks/graph_blocks.c +++ b/quad/src/graph_blocks/graph_blocks.c @@ -22,13 +22,7 @@ int graph_add_defined_block(struct computation_graph* graph, int type_id, const return -1; } const struct graph_node_type *block_type = blockDefs[type_id]; - void* state = NULL; - // Allocate the state struct for this node, if necessary - if (block_type->state_size) { - state = malloc(block_type->state_size); - if (!state) {return -1;} // Check for malloc failure - } // Use the computation graph implementation's add node function - return graph_add_node(graph, name, block_type, state); + return graph_add_node(graph, name, block_type); } \ No newline at end of file