From 4d1a1baef9a00d920d284a39b9fc60b8ec32e450 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 5 Nov 2022 09:12:07 -0700 Subject: [PATCH] prune: return list by value from 'new_generic_list' The list type is exposed in the header, so we can stack allocate these, removing some heap pressure and failure paths. --- contrib/prune/generic_list.c | 9 ++++----- contrib/prune/generic_list.h | 2 +- contrib/prune/prune.c | 34 ++++++++++++++-------------------- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/contrib/prune/generic_list.c b/contrib/prune/generic_list.c index f75d3705e..3707c3608 100644 --- a/contrib/prune/generic_list.c +++ b/contrib/prune/generic_list.c @@ -16,13 +16,13 @@ #define DFLT_SIZE 100 -generic_list_t *new_generic_list(uint64_t size) +generic_list_t new_generic_list(uint64_t size) { - generic_list_t *list = gv_alloc(sizeof(generic_list_t)); + generic_list_t list = {0}; if (size != 0) { - list->data = gv_calloc(size, sizeof(gl_data)); + list.data = gv_calloc(size, sizeof(gl_data)); } - list->size = size; + list.size = size; return list; } @@ -31,7 +31,6 @@ void free_generic_list(generic_list_t * list) if (list->size > 0) { free(list->data); } - free(list); } void add_to_generic_list(generic_list_t *list, gl_data element) { diff --git a/contrib/prune/generic_list.h b/contrib/prune/generic_list.h index 9eb5c3690..39c2ed183 100644 --- a/contrib/prune/generic_list.h +++ b/contrib/prune/generic_list.h @@ -18,7 +18,7 @@ gl_data *data; /* pointer to first element */ } generic_list_t; - extern generic_list_t *new_generic_list(uint64_t size); + extern generic_list_t new_generic_list(uint64_t size); extern void add_to_generic_list(generic_list_t * list, gl_data element); extern void free_generic_list(generic_list_t * list); diff --git a/contrib/prune/prune.c b/contrib/prune/prune.c index 32b312ee6..e22a645b5 100644 --- a/contrib/prune/prune.c +++ b/contrib/prune/prune.c @@ -67,9 +67,6 @@ int main(int argc, char **argv) char **files; - generic_list_t *attr_list; - generic_list_t *node_list; - unsigned long i, j; opterr = 0; @@ -81,16 +78,16 @@ int main(int argc, char **argv) progname++; /* character after last '/' */ } - attr_list = new_generic_list(16); - node_list = new_generic_list(16); + generic_list_t attr_list = new_generic_list(16); + generic_list_t node_list = new_generic_list(16); while ((c = getopt(argc, argv, "hvn:N:")) != -1) { switch (c) { case 'N': - addattr(attr_list, optarg); + addattr(&attr_list, optarg); break; case 'n': - addnode(node_list, optarg); + addnode(&node_list, optarg); break; case 'h': help_message(progname); @@ -138,17 +135,16 @@ int main(int argc, char **argv) aginit(graph, AGNODE, NDNAME, sizeof(ndata), 1); /* prune all nodes specified on the commandline */ - for (i = 0; i < node_list->used; i++) { + for (i = 0; i < node_list.used; i++) { if (verbose == 1) - fprintf(stderr, "Pruning node %s\n", - (char *) node_list->data[i]); + fprintf(stderr, "Pruning node %s\n", (char*)node_list.data[i]); /* check whether a node of that name exists at all */ - node = agnode(graph, (char *) node_list->data[i], 0); + node = agnode(graph, (char*)node_list.data[i], 0); if (node == NULL) { fprintf(stderr, "*** Warning: No such node: %s -- gracefully skipping this one\n", - (char *) node_list->data[i]); + (char*)node_list.data[i]); } else { MARK(node); /* Avoid cycles */ /* Iterate over all outgoing edges */ @@ -165,26 +161,24 @@ int main(int argc, char **argv) UNMARK(node); /* Unmark so that it can be removed in later passes */ /* Change attribute (e.g. border style) to show that node has been pruneed */ - for (j = 0; j < attr_list->used; j++) { + for (j = 0; j < attr_list.used; j++) { /* create attribute if it doesn't exist and set it */ attr = - agattr(graph, AGNODE, - ((strattr_t *) attr_list->data[j])->n, ""); + agattr(graph, AGNODE, ((strattr_t*)attr_list.data[j])->n, ""); if (attr == NULL) { fprintf(stderr, "Couldn't create attribute: %s\n", - ((strattr_t *) attr_list->data[j])->n); + ((strattr_t*)attr_list.data[j])->n); graphviz_exit(EXIT_FAILURE); } - agxset(node, attr, - ((strattr_t *) attr_list->data[j])->v); + agxset(node, attr, ((strattr_t*)attr_list.data[j])->v); } } } agwrite(graph, stdout); agclose(graph); } - free_generic_list(attr_list); - free_generic_list(node_list); + free_generic_list(&attr_list); + free_generic_list(&node_list); graphviz_exit(EXIT_SUCCESS); } -- 2.40.0