Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): replace FStr_Assign with separate init...



details:   https://anonhg.NetBSD.org/src/rev/1fcf7ccbd572
branches:  trunk
changeset: 947911:1fcf7ccbd572
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Dec 20 00:57:29 2020 +0000

description:
make(1): replace FStr_Assign with separate initialization functions

In GetVarnamesToUnexport, there is no need to free the local FStr since
the only place where it is assigned an allocated string is at the very
end.

Having separate functions for the two main use cases of a possibly
allocated string makes the calling code simpler.  This is a preparatory
commit for making the memory allocation in ApplyModifiers easier to
understand.

diffstat:

 usr.bin/make/var.c |  28 ++++++++++++++++------------
 1 files changed, 16 insertions(+), 12 deletions(-)

diffs (71 lines):

diff -r b5c04dec6169 -r 1fcf7ccbd572 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Dec 20 00:47:21 2020 +0000
+++ b/usr.bin/make/var.c        Sun Dec 20 00:57:29 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.738 2020/12/20 00:47:21 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.739 2020/12/20 00:57:29 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.738 2020/12/20 00:47:21 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.739 2020/12/20 00:57:29 rillig Exp $");
 
 /* A string that may need to be freed after use. */
 typedef struct FStr {
@@ -305,14 +305,18 @@
 
 static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE;
 
-#define FSTR_INIT { NULL, NULL }
-
-static void
-FStr_Assign(FStr *fstr, const char *str, void *freeIt)
+/* Return an FStr that is the sole owner of str. */
+static FStr
+FStr_InitOwn(char *str)
 {
-       free(fstr->freeIt);
-       fstr->str = str;
-       fstr->freeIt = freeIt;
+       return (FStr){ str, str };
+}
+
+/* Return an FStr that refers to the shared str. */
+static FStr
+FStr_InitRefer(const char *str)
+{
+       return (FStr){ str, NULL };
 }
 
 static void
@@ -791,7 +795,7 @@
                      FStr *out_varnames, UnexportWhat *out_what)
 {
        UnexportWhat what;
-       FStr varnames = FSTR_INIT;
+       FStr varnames = FStr_InitRefer("");
 
        if (isEnv) {
                if (arg[0] != '\0') {
@@ -804,7 +808,7 @@
        } else {
                what = arg[0] != '\0' ? UNEXPORT_NAMED : UNEXPORT_ALL;
                if (what == UNEXPORT_NAMED)
-                       FStr_Assign(&varnames, arg, NULL);
+                       varnames = FStr_InitRefer(arg);
        }
 
        if (what != UNEXPORT_NAMED) {
@@ -813,7 +817,7 @@
                (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL,
                    VARE_WANTRES, &expanded);
                /* TODO: handle errors */
-               FStr_Assign(&varnames, expanded, expanded);
+               varnames = FStr_InitOwn(expanded);
        }
 
        *out_varnames = varnames;



Home | Main Index | Thread Index | Old Index