git: 1dbb1cca9b8b - main - ps(1): Consider references to keywords as immutable

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Mon, 28 Apr 2025 12:23:25 UTC
The branch main has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=1dbb1cca9b8b7874f22c06f5340686361c76da47

commit 1dbb1cca9b8b7874f22c06f5340686361c76da47
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-02-28 21:07:14 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-04-28 11:56:45 +0000

    ps(1): Consider references to keywords as immutable
    
    Building on the previous commit that moved the 'width' field from VAR
    (structure representing the meaning of a keyword) to VARENT (structure
    representing an actual column in a display), it is now possible to
    declare the field 'var' of VARENT as a constant reference.
    
    At this point, it could be possible to constify the var[] array
    containing all the recognized keywords.  However, we do not do it as
    a subsequent commit will change the way alias keywords are resolved,
    causing their fields to be modified to reflect the final values to use
    after alias resolution.
    
    In parsefmt(), forget about allocating a new VAR for the selected
    keyword to be pointed by the corresponding column (VARENT), and instead
    just keep a pointer to that structure in var[].
    
    MFC after:      3 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D49611
---
 bin/ps/keyword.c | 5 +----
 bin/ps/print.c   | 8 ++++----
 bin/ps/ps.c      | 4 ++--
 bin/ps/ps.h      | 2 +-
 4 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/bin/ps/keyword.c b/bin/ps/keyword.c
index f3ff567f2d57..37a4a1711445 100644
--- a/bin/ps/keyword.c
+++ b/bin/ps/keyword.c
@@ -299,10 +299,7 @@ parsefmt(const char *p, struct velisthead *const var_list,
 				vent->header = hp;
 		}
 		vent->width = strlen(vent->header);
-		vent->var = malloc(sizeof(*vent->var));
-		if (vent->var == NULL)
-			xo_errx(1, "malloc failed");
-		memcpy(vent->var, v, sizeof(*vent->var));
+		vent->var = v;
 		STAILQ_INSERT_TAIL(var_list, vent, next_ve);
 	}
 	free(tempstr1);
diff --git a/bin/ps/print.c b/bin/ps/print.c
index 005268b30d8c..b496858c071b 100644
--- a/bin/ps/print.c
+++ b/bin/ps/print.c
@@ -66,7 +66,7 @@
 void
 printheader(void)
 {
-	VAR *v;
+	const VAR *v;
 	struct varent *vent;
 
 	STAILQ_FOREACH(vent, &varlist, next_ve)
@@ -734,7 +734,7 @@ priorityr(KINFO *k, VARENT *ve __unused)
  * structures.
  */
 static char *
-printval(void *bp, VAR *v)
+printval(void *bp, const VAR *v)
 {
 	static char ofmt[32] = "%";
 	const char *fcp;
@@ -785,7 +785,7 @@ printval(void *bp, VAR *v)
 char *
 kvar(KINFO *k, VARENT *ve)
 {
-	VAR *v;
+	const VAR *v;
 
 	v = ve->var;
 	return (printval((char *)((char *)k->ki_p + v->off), v));
@@ -794,7 +794,7 @@ kvar(KINFO *k, VARENT *ve)
 char *
 rvar(KINFO *k, VARENT *ve)
 {
-	VAR *v;
+	const VAR *v;
 
 	v = ve->var;
 	if (!k->ki_valid)
diff --git a/bin/ps/ps.c b/bin/ps/ps.c
index db3826c958c6..10b3322dc269 100644
--- a/bin/ps/ps.c
+++ b/bin/ps/ps.c
@@ -1192,7 +1192,7 @@ static void
 scanvars(void)
 {
 	struct varent *vent;
-	VAR *v;
+	const VAR *v;
 
 	STAILQ_FOREACH(vent, &varlist, next_ve) {
 		v = vent->var;
@@ -1207,7 +1207,7 @@ static void
 format_output(KINFO *ki)
 {
 	struct varent *vent;
-	VAR *v;
+	const VAR *v;
 	KINFO_STR *ks;
 	char *str;
 	u_int len;
diff --git a/bin/ps/ps.h b/bin/ps/ps.h
index a1ec49cd676b..1787af33021e 100644
--- a/bin/ps/ps.h
+++ b/bin/ps/ps.h
@@ -57,7 +57,7 @@ typedef struct kinfo {
 typedef struct varent {
 	STAILQ_ENTRY(varent)	 next_ve;
 	const char		*header;
-	struct var		*var;
+	const struct var	*var;
 	u_int			 width;
 } VARENT;
 STAILQ_HEAD(velisthead, varent);