summaryrefslogtreecommitdiff
path: root/lib/command_graph.c
diff options
context:
space:
mode:
authorQuentin Young <qlyoung@cumulusnetworks.com>2016-07-29 15:54:03 +0000
committerQuentin Young <qlyoung@cumulusnetworks.com>2016-07-29 15:54:03 +0000
commitde9d7e4f3ccb1b199602c1a1ce884df37e54f834 (patch)
tree7ecb990ffe55f28e5ce77a059c791f558e503130 /lib/command_graph.c
parent76699ae7e0358c57a1186b88962d7233a7057d76 (diff)
lib: Cleanup some memory issues in CLI
Various memory leaks have been fixed and the quagga memory macros are in use. Also consolidated the argv and matching code into one graph traversal. Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib/command_graph.c')
-rw-r--r--lib/command_graph.c57
1 files changed, 53 insertions, 4 deletions
diff --git a/lib/command_graph.c b/lib/command_graph.c
index a5880f34a6..7c09a5cd6c 100644
--- a/lib/command_graph.c
+++ b/lib/command_graph.c
@@ -22,6 +22,7 @@ add_node(struct graph_node *parent, struct graph_node *child)
return p_child;
}
vector_set(parent->children, child);
+ child->refs++;
return child;
}
@@ -58,6 +59,7 @@ cmp_node(struct graph_node *first, struct graph_node *second)
*/
case START_GN:
case END_GN:
+ case NUL_GN:
default:
break;
}
@@ -73,29 +75,61 @@ new_node(enum graph_node_type type)
node->type = type;
node->children = vector_init(VECTOR_MIN_SIZE);
- node->is_start = 0;
node->end = NULL;
node->text = NULL;
+ node->element = NULL;
+ node->arg = NULL;
+ node->is_start = 0;
node->value = 0;
node->min = 0;
node->max = 0;
- node->element = NULL;
+ node->refs = 0;
return node;
}
+struct graph_node *
+copy_node (struct graph_node *node)
+{
+ struct graph_node *new = new_node(node->type);
+ new->children = vector_copy (node->children);
+ new->is_start = node->is_start;
+ new->end = node->end;
+ new->text = node->text ? XSTRDUP(MTYPE_CMD_TOKENS, node->text) : NULL;
+ new->value = node->value;
+ new->min = node->min;
+ new->max = node->max;
+ new->element = node->element ? copy_cmd_element(node->element) : NULL;
+ new->arg = node->arg ? XSTRDUP(MTYPE_CMD_TOKENS, node->arg) : NULL;
+ new->refs = 0;
+ return new;
+}
+
void
free_node (struct graph_node *node)
{
if (!node) return;
- free_node (node->end);
vector_free (node->children);
+ free_cmd_element (node->element);
free (node->text);
free (node->arg);
- free (node->element);
free (node);
}
+void
+free_graph (struct graph_node *start)
+{
+ if (start && start->children && vector_active(start->children) > 0) {
+ for (unsigned int i = 0; i < vector_active(start->children); i++) {
+ free_graph (vector_slot(start->children, i));
+ vector_unset(start->children, i);
+ }
+ }
+
+ if (--(start->refs) == 0)
+ free_node (start);
+}
+
char *
describe_node(struct graph_node *node, char* buffer, unsigned int bufsize)
{
@@ -166,3 +200,18 @@ walk_graph(struct graph_node *start, int level)
fprintf(stderr, "\n");
}
+void
+dump_node (struct graph_node *node)
+{
+ char buf[50];
+ describe_node(node, buf, 50);
+ fprintf(stderr, "%s[%d]\n", buf, node->type);
+ fprintf(stderr, "\t->text: %s\n", node->text);
+ fprintf(stderr, "\t->value: %ld\n", node->value);
+ fprintf(stderr, "\t->is_start: %d\n", node->is_start);
+ fprintf(stderr, "\t->element: %p\n", node->element);
+ fprintf(stderr, "\t->min: %ld\n->max: %ld\n", node->min, node->max);
+ fprintf(stderr, "\t->arg: %s\n", node->arg);
+ fprintf(stderr, "\t->refs: %d\n", node->refs);
+ fprintf(stderr, "\tnum children: %d\n", vector_active(node->children));
+}