Module Name:    src
Committed By:   rillig
Date:           Sun Dec 20 00:57:29 UTC 2020

Modified Files:
        src/usr.bin/make: var.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.738 -r1.739 src/usr.bin/make/var.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.738 src/usr.bin/make/var.c:1.739
--- src/usr.bin/make/var.c:1.738	Sun Dec 20 00:47:21 2020
+++ src/usr.bin/make/var.c	Sun Dec 20 00:57:29 2020
@@ -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 @@ ENUM_FLAGS_RTTI_6(VarFlags,
 
 static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE;
 
-#define FSTR_INIT { NULL, NULL }
+/* Return an FStr that is the sole owner of str. */
+static FStr
+FStr_InitOwn(char *str)
+{
+	return (FStr){ str, str };
+}
 
-static void
-FStr_Assign(FStr *fstr, const char *str, void *freeIt)
+/* Return an FStr that refers to the shared str. */
+static FStr
+FStr_InitRefer(const char *str)
 {
-	free(fstr->freeIt);
-	fstr->str = str;
-	fstr->freeIt = freeIt;
+	return (FStr){ str, NULL };
 }
 
 static void
@@ -791,7 +795,7 @@ GetVarnamesToUnexport(Boolean isEnv, con
 		      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 @@ GetVarnamesToUnexport(Boolean isEnv, con
 	} 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 @@ GetVarnamesToUnexport(Boolean isEnv, con
 		(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;

Reply via email to