From 156567dc18bcea1d3ecece24d1627e22fa6634ce Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 2 Jul 2022 08:28:09 -0700 Subject: [PATCH] gvpr: stack-allocate options struct There is no need to heap-allocate this struct that is small and whose usage is well scoped. --- lib/gvpr/gvpr.c | 85 ++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/lib/gvpr/gvpr.c b/lib/gvpr/gvpr.c index 31c98f34a..82ba2e129 100644 --- a/lib/gvpr/gvpr.c +++ b/lib/gvpr/gvpr.c @@ -15,6 +15,7 @@ * Written by Emden Gansner */ +#include #include #include "builddate.h" #include @@ -362,7 +363,7 @@ static void freeOpts (options* opts) { int i; - if (!opts) return; + assert(opts != NULL); if (opts->outFile != sfstdout) sfclose (opts->outFile); free (opts->inFiles); @@ -373,23 +374,22 @@ freeOpts (options* opts) free (opts->argv[i]); free (opts->argv); } - free (opts); } /* scanArgs: * Parse command line options. */ -static options* scanArgs(int argc, char **argv) { +static options scanArgs(int argc, char **argv) { int i, nfiles; char** input_filenames; char* arg; - options* opts = newof(0,options,1,0); + options opts = {0}; - opts->cmdName = argv[0]; - opts->state = 1; - opts->readAhead = 1; - setErrorId (opts->cmdName); - opts->verbose = 0; + opts.cmdName = argv[0]; + opts.state = 1; + opts.readAhead = 1; + setErrorId(opts.cmdName); + opts.verbose = 0; /* estimate number of file names */ nfiles = 0; @@ -403,9 +403,9 @@ static options* scanArgs(int argc, char **argv) { for (i = 1; i < argc; ) { arg = argv[i++]; if (*arg == '-') { - i = doFlags (arg+1, i, argc, argv, opts); + i = doFlags(arg+1, i, argc, argv, &opts); if (i <= 0) { - opts->state = i; + opts.state = i; goto opts_done; } } else if (arg) @@ -413,32 +413,32 @@ static options* scanArgs(int argc, char **argv) { } /* Handle additional semantics */ - if (opts->useFile == 0) { + if (opts.useFile == 0) { if (nfiles == 0) { error(ERROR_ERROR, "No program supplied via argument or -f option"); - opts->state = -1; + opts.state = -1; } else { - opts->program = input_filenames[0]; + opts.program = input_filenames[0]; for (i = 1; i <= nfiles; i++) input_filenames[i-1] = input_filenames[i]; nfiles--; } } if (nfiles == 0) { - opts->inFiles = 0; + opts.inFiles = 0; free (input_filenames); input_filenames = 0; } else - opts->inFiles = input_filenames; + opts.inFiles = input_filenames; - if (!(opts->outFile)) - opts->outFile = sfstdout; + if (!opts.outFile) + opts.outFile = sfstdout; opts_done: - if (opts->state <= 0) { - if (opts->state < 0) + if (opts.state <= 0) { + if (opts.state < 0) error(ERROR_USAGE|ERROR_ERROR, "%s", usage); free (input_filenames); } @@ -930,7 +930,6 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) Gpr_t *state = 0; gpr_info info; int rv = 0; - options* opts = 0; int cleanup, i, incoreGraphs; Agraph_t* nextg = NULL; @@ -941,22 +940,22 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) if (uopts->err) setDisc (sfstderr, &errdisc, uopts->err); } - opts = scanArgs(argc, argv); - if (opts->state <= 0) { - rv = opts->state; + options opts = scanArgs(argc, argv); + if (opts.state <= 0) { + rv = opts.state; goto finish; } - if (opts->verbose) + if (opts.verbose) gvstart_timer (); - prog = parseProg(opts->program, opts->useFile); + prog = parseProg(opts.program, opts.useFile); if (!prog) { rv = 1; goto finish; } - info.outFile = opts->outFile; - info.argc = opts->argc; - info.argv = opts->argv; + info.outFile = opts.outFile; + info.argc = opts.argc; + info.argv = opts.argv; info.errf = gverrorf; if (uopts) info.flags = uopts->flags; @@ -973,7 +972,7 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) } if (uopts->bindings) addBindings (state, uopts->bindings); - xprog = compileProg(prog, state, opts->compflags); + xprog = compileProg(prog, state, opts.compflags); if (!xprog) { rv = 1; goto finish; @@ -998,7 +997,7 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) else incoreGraphs = 0; - if (opts->verbose) + if (opts.verbose) fprintf(stderr, "Parse/compile/init: %.2f secs.\n", gvelapsed_sec()); /* do begin */ if (xprog->begin_stmt) @@ -1009,13 +1008,13 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) if (uopts && uopts->ingraphs) ing = newIngGraphs(0, uopts->ingraphs, &ingDisc); else - ing = newIng(0, opts->inFiles, &ingDisc); + ing = newIng(0, opts.inFiles, &ingDisc); - if (opts->verbose) gvstart_timer (); + if (opts.verbose) gvstart_timer (); for (state->curgraph = nextGraph(ing); state->curgraph; state->curgraph = nextg) { - if (opts->verbose) fprintf(stderr, "Read graph: %.2f secs.\n", gvelapsed_sec()); + if (opts.verbose) fprintf(stderr, "Read graph: %.2f secs.\n", gvelapsed_sec()); state->infname = fileName(ing); - if (opts->readAhead) + if (opts.readAhead) nextg = state->nextgraph = nextGraph(ing); cleanup = 0; @@ -1023,7 +1022,7 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) comp_block* bp = xprog->blocks + i; /* begin graph */ - if (incoreGraphs && (opts->compflags & CLONE)) + if (incoreGraphs && (opts.compflags & CLONE)) state->curgraph = (Agraph_t*)clone (0, (Agobj_t*)(state->curgraph)); state->curobj = (Agobj_t *) state->curgraph; state->tvroot = 0; @@ -1040,7 +1039,7 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) state->curobj = (Agobj_t *) state->curgraph; if (xprog->endg_stmt) exeval(xprog->prog, xprog->endg_stmt, state); - if (opts->verbose) fprintf(stderr, "Finish graph: %.2f secs.\n", gvelapsed_sec()); + if (opts.verbose) fprintf(stderr, "Finish graph: %.2f secs.\n", gvelapsed_sec()); /* if $O == $G and $T is empty, delete $T */ if ((state->outgraph == state->curgraph) && @@ -1052,11 +1051,11 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) * be non-empty or the -c option was used. */ if (state->outgraph && (agnnodes(state->outgraph) - || (opts->compflags & SRCOUT))) { + || (opts.compflags & SRCOUT))) { if (uopts && (uopts->flags & GV_USE_OUTGRAPH)) addOutputGraph (state, uopts); else - sfioWrite (state->outgraph, opts->outFile, state->dfltIO); + sfioWrite (state->outgraph, opts.outFile, state->dfltIO); } if (!incoreGraphs) @@ -1064,10 +1063,10 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) state->target = 0; state->outgraph = 0; - if (opts->verbose) gvstart_timer (); - if (!opts->readAhead) + if (opts.verbose) gvstart_timer (); + if (!opts.readAhead) nextg = nextGraph(ing); - if (opts->verbose && nextg) fprintf(stderr, "Read graph: %.2f secs.\n", gvelapsed_sec()); + if (opts.verbose && nextg) fprintf(stderr, "Read graph: %.2f secs.\n", gvelapsed_sec()); } } @@ -1083,7 +1082,7 @@ int gvpr (int argc, char *argv[], gvpropts * uopts) freeCompileProg (xprog); closeGPRState(state); if (ing) closeIngraph (ing); - freeOpts (opts); + freeOpts(&opts); if (uopts) { if (uopts->out) sfdisc (sfstdout, 0); -- 2.40.0