git: 21d9f3d9f0d7 - stable/14 - ps(1): Consider references to keywords as immutable
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 01 May 2025 19:51:33 UTC
The branch stable/14 has been updated by olce:
URL: https://cgit.FreeBSD.org/src/commit/?id=21d9f3d9f0d77b315567eaa0acf153cba17e23ef
commit 21d9f3d9f0d77b315567eaa0acf153cba17e23ef
Author: Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-02-28 21:07:14 +0000
Commit: Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-05-01 19:37:03 +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
(cherry picked from commit 1dbb1cca9b8b7874f22c06f5340686361c76da47)
---
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 823018f12ba5..40986550b92b 100644
--- a/bin/ps/keyword.c
+++ b/bin/ps/keyword.c
@@ -305,10 +305,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 d7d3fd1339dc..a3f8f244648d 100644
--- a/bin/ps/print.c
+++ b/bin/ps/print.c
@@ -73,7 +73,7 @@ static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
void
printheader(void)
{
- VAR *v;
+ const VAR *v;
struct varent *vent;
STAILQ_FOREACH(vent, &varlist, next_ve)
@@ -745,7 +745,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;
@@ -796,7 +796,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));
@@ -805,7 +805,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 44e6ecd32498..fe100170b7e2 100644
--- a/bin/ps/ps.c
+++ b/bin/ps/ps.c
@@ -1217,7 +1217,7 @@ static void
scanvars(void)
{
struct varent *vent;
- VAR *v;
+ const VAR *v;
STAILQ_FOREACH(vent, &varlist, next_ve) {
v = vent->var;
@@ -1232,7 +1232,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 838aea33f3aa..800e732262e9 100644
--- a/bin/ps/ps.h
+++ b/bin/ps/ps.h
@@ -59,7 +59,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);