Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 3 Jun 2018 23:40:54 +0000 (UTC)
From:      Eitan Adler <eadler@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r334593 - head/usr.bin/top
Message-ID:  <201806032340.w53Nesm4099887@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: eadler
Date: Sun Jun  3 23:40:54 2018
New Revision: 334593
URL: https://svnweb.freebsd.org/changeset/base/334593

Log:
  top(1): another pass of cleanup
  
  - avoid the need to call a function to get size of known array. I'll
  likely re-arrange some of the indirect in a later to avoid the magic
  constants.
  - use correct type
  - add const
  - replace caddr_t with void*. This corrects an alignment warning.
  - remove duplicated include from immediately prior commit
  
  Under base clang we're now down to:
  - 3 warning in top.c, 1 warning in mahcine.c,  4 warning in display.c,
  - 1 warning in utils.c
  
  Tested with base clang, gcc7, gcc9, base gcc (mips)

Modified:
  head/usr.bin/top/display.c
  head/usr.bin/top/machine.c
  head/usr.bin/top/machine.h
  head/usr.bin/top/top.c
  head/usr.bin/top/top.h
  head/usr.bin/top/utils.c
  head/usr.bin/top/utils.h

Modified: head/usr.bin/top/display.c
==============================================================================
--- head/usr.bin/top/display.c	Sun Jun  3 23:36:29 2018	(r334592)
+++ head/usr.bin/top/display.c	Sun Jun  3 23:40:54 2018	(r334593)
@@ -28,6 +28,7 @@
  *        *_process, u_endscreen.
  */
 
+#include <sys/resource.h>
 #include <sys/time.h>
 
 #include <assert.h>
@@ -90,8 +91,7 @@ static int cpustates_column;
 
 static enum { OFF, ON, ERASE } header_status = ON;
 
-static int string_count(char **);
-static void summary_format(char *, int *, char **);
+static void summary_format(char *, int *, const char * const *);
 static void line_update(char *, char *, int, int);
 
 int  x_lastpid =	10;
@@ -203,23 +203,23 @@ int display_init(struct statics * statics)
     {
 	/* save pointers and allocate space for names */
 	procstate_names = statics->procstate_names;
-	num_procstates = string_count(procstate_names);
+	num_procstates = 8;
 	assert(num_procstates > 0);
 	lprocstates = calloc(num_procstates, sizeof(int));
 
 	cpustate_names = statics->cpustate_names;
 
 	swap_names = statics->swap_names;
-	num_swap = string_count(swap_names);
+	num_swap = 7;
 	assert(num_swap > 0);
 	lswap = calloc(num_swap, sizeof(int));
-	num_cpustates = string_count(cpustate_names);
+	num_cpustates = CPUSTATES;
 	assert(num_cpustates > 0);
 	lcpustates = calloc(num_cpustates * sizeof(int), statics->ncpus);
 	cpustate_columns = calloc(num_cpustates, sizeof(int));
 
 	memory_names = statics->memory_names;
-	num_memory = string_count(memory_names);
+	num_memory = 7;
 	assert(num_memory > 0);
 	lmemory = calloc(num_memory, sizeof(int));
 
@@ -422,8 +422,8 @@ i_cpustates(int *states)
 {
     int i = 0;
     int value;
-    char **names;
-    char *thisname;
+    const char * const *names;
+    const char *thisname;
     int cpu;
 
 for (cpu = 0; cpu < num_cpus; cpu++) {
@@ -761,7 +761,7 @@ trim_header(const char *text)
 void
 i_header(const char *text)
 {
-    const char *s;
+    char *s;
 
     s = trim_header(text);
     if (s != NULL)
@@ -1075,23 +1075,11 @@ readline(char *buffer, int size, int numeric)
 
 /* internal support routines */
 
-static int string_count(char **pp)
+static void summary_format(char *str, int *numbers, const char * const *names)
 {
-    int cnt;
-
-    cnt = 0;
-    while (*pp++ != NULL)
-    {
-	cnt++;
-    }
-    return(cnt);
-}
-
-static void summary_format(char *str, int *numbers, char **names)
-{
     char *p;
     int num;
-    char *thisname;
+    const char *thisname;
     char rbuf[6];
 
     /* format each number followed by its string */

Modified: head/usr.bin/top/machine.c
==============================================================================
--- head/usr.bin/top/machine.c	Sun Jun  3 23:36:29 2018	(r334592)
+++ head/usr.bin/top/machine.c	Sun Jun  3 23:40:54 2018	(r334593)
@@ -15,7 +15,6 @@
  * $FreeBSD$
  */
 
-#include <sys/param.h>
 #include <sys/errno.h>
 #include <sys/file.h>
 #include <sys/param.h>
@@ -147,12 +146,12 @@ static long cp_diff[CPUSTATES];
 
 /* these are for detailing the process states */
 
-static int process_states[8];
 static const char *procstatenames[] = {
 	"", " starting, ", " running, ", " sleeping, ", " stopped, ",
 	" zombie, ", " waiting, ", " lock, ",
 	NULL
 };
+static int process_states[nitems(procstatenames)];
 
 /* these are for detailing the cpu states */
 
@@ -163,29 +162,29 @@ static const char *cpustatenames[] = {
 
 /* these are for detailing the memory statistics */
 
-static int memory_stats[7];
 static const char *memorynames[] = {
 	"K Active, ", "K Inact, ", "K Laundry, ", "K Wired, ", "K Buf, ",
 	"K Free", NULL
 };
+static int memory_stats[nitems(memorynames)];
 
-static int arc_stats[7];
 static const char *arcnames[] = {
 	"K Total, ", "K MFU, ", "K MRU, ", "K Anon, ", "K Header, ", "K Other",
 	NULL
 };
+static int arc_stats[nitems(arcnames)];
 
-static int carc_stats[4];
 static const char *carcnames[] = {
 	"K Compressed, ", "K Uncompressed, ", ":1 Ratio, ",
 	NULL
 };
+static int carc_stats[nitems(carcnames)];
 
-static int swap_stats[7];
 static const char *swapnames[] = {
 	"K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
 	NULL
 };
+static int swap_stats[nitems(swapnames)];
 
 
 /* these are for keeping track of the proc array */
@@ -914,13 +913,13 @@ get_process_info(struct system_info *si, struct proces
 	/* pass back a handle */
 	handle.next_proc = pref;
 	handle.remaining = active_procs;
-	return ((caddr_t)&handle);
+	return ((void*)&handle);
 }
 
 static char fmt[512];	/* static area where result is built */
 
 char *
-format_next_process(caddr_t xhandle, char *(*get_userid)(int), int flags)
+format_next_process(void* xhandle, char *(*get_userid)(int), int flags)
 {
 	struct kinfo_proc *pp;
 	const struct kinfo_proc *oldp;
@@ -932,11 +931,11 @@ format_next_process(caddr_t xhandle, char *(*get_useri
 	size_t state;
 	struct rusage ru, *rup;
 	long p_tot, s_tot;
-	char *proc_fmt;
+	const char *proc_fmt;
 	char thr_buf[6];
 	char jid_buf[TOP_JID_LEN + 1], swap_buf[TOP_SWAP_LEN + 1];
 	char *cmdbuf = NULL;
-	const char * const *args;
+	char **args;
 	const int cmdlen = 128;
 
 	/* find and remember the next proc structure */
@@ -1026,8 +1025,9 @@ format_next_process(caddr_t xhandle, char *(*get_useri
 				    "[%s]", pp->ki_comm);
 			}
 		} else {
-			char *src, *dst, *argbuf;
-			char *cmd;
+			const char *src;
+			char *dst, *argbuf;
+			const char *cmd;
 			size_t argbuflen;
 			size_t len;
 

Modified: head/usr.bin/top/machine.h
==============================================================================
--- head/usr.bin/top/machine.h	Sun Jun  3 23:36:29 2018	(r334592)
+++ head/usr.bin/top/machine.h	Sun Jun  3 23:40:54 2018	(r334593)
@@ -10,8 +10,11 @@
 #ifndef MACHINE_H
 #define MACHINE_H
 
-#include "top.h"
+#define NUM_AVERAGES    3
 
+/* Log base 2 of 1024 is 10 (2^10 == 1024) */
+#define LOG1024		10
+
 /*
  * the statics struct is filled in by machine_init
  */
@@ -76,7 +79,7 @@ struct process_select
 /* routines defined by the machine dependent module */
 
 const char	*format_header(const char *uname_field);
-char	*format_next_process(caddr_t handle, char *(*get_userid)(int),
+char	*format_next_process(void* handle, char *(*get_userid)(int),
 	    int flags);
 void	 toggle_pcpustats(void);
 void	 get_system_info(struct system_info *si);

Modified: head/usr.bin/top/top.c
==============================================================================
--- head/usr.bin/top/top.c	Sun Jun  3 23:36:29 2018	(r334592)
+++ head/usr.bin/top/top.c	Sun Jun  3 23:40:54 2018	(r334593)
@@ -210,7 +210,7 @@ main(int argc, char *argv[])
     const char *uname_field = "USERNAME";
     const char *header_text;
     char *env_top;
-    char **preset_argv;
+    const char **preset_argv;
     int  preset_argc = 0;
     char **av;
     int  ac;

Modified: head/usr.bin/top/top.h
==============================================================================
--- head/usr.bin/top/top.h	Sun Jun  3 23:36:29 2018	(r334592)
+++ head/usr.bin/top/top.h	Sun Jun  3 23:40:54 2018	(r334593)
@@ -18,9 +18,6 @@ extern int Header_lines;	/* 7 */
 /* Maximum number of columns allowed for display */
 #define MAX_COLS	512
 
-/* Log base 2 of 1024 is 10 (2^10 == 1024) */
-#define LOG1024		10
-
 /* Special atoi routine returns either a non-negative number or one of: */
 #define Infinity	-1
 #define Invalid		-2
@@ -31,8 +28,6 @@ extern int Header_lines;	/* 7 */
 /*
  * The entire display is based on these next numbers being defined as is.
  */
-
-#define NUM_AVERAGES    3
 
 /* Exit code for system errors */
 #define TOP_EX_SYS_ERROR	23

Modified: head/usr.bin/top/utils.c
==============================================================================
--- head/usr.bin/top/utils.c	Sun Jun  3 23:36:29 2018	(r334592)
+++ head/usr.bin/top/utils.c	Sun Jun  3 23:40:54 2018	(r334593)
@@ -142,7 +142,7 @@ int digits(int val)
  */
 
 int
-string_index(const char *string, char *array[])
+string_index(const char *string, const char * const *array)
 {
     size_t i = 0;
 
@@ -165,8 +165,8 @@ string_index(const char *string, char *array[])
  *	squat about quotes.
  */
 
-char **
-argparse(char *line, int *cntp)
+const char * const *
+argparse(const char *line, int *cntp)
 {
     const char *from;
     char *to;
@@ -175,7 +175,7 @@ argparse(char *line, int *cntp)
     int length;
     int lastch;
     char **argv;
-    char **argarray;
+    const char * const *argarray;
     char *args;
 
     /* unfortunately, the only real way to do this is to go thru the

Modified: head/usr.bin/top/utils.h
==============================================================================
--- head/usr.bin/top/utils.h	Sun Jun  3 23:36:29 2018	(r334592)
+++ head/usr.bin/top/utils.h	Sun Jun  3 23:40:54 2018	(r334593)
@@ -16,11 +16,11 @@ int atoiwi(const char *);
 char *itoa(unsigned int);
 char *itoa7(int);
 int digits(int);
-char **argparse(char *, int *);
+const char * const *argparse(const char *, int *);
 long percentages(int, int *, long *, long *, long *);
 char *format_time(long);
 char *format_k(int);
 char *format_k2(unsigned long long);
-int string_index(const char *string, char *array[]);
+int string_index(const char *string, const char * const *array);
 int find_pid(pid_t pid);
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201806032340.w53Nesm4099887>