Date: Sat, 2 Jun 2018 21:40:45 +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: r334540 - head/usr.bin/top Message-ID: <201806022140.w52LejjF005616@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: eadler Date: Sat Jun 2 21:40:45 2018 New Revision: 334540 URL: https://svnweb.freebsd.org/changeset/base/334540 Log: top(1): cleanup memory allocation and warnings - Prefer calloc over malloc. This is more predicable and we're not in a performance sensitive context. [1] - Remove bogus comment (obsolete from prior commit). [2] - Remove void casts and type casts of NULL - Remove redundant declaration of 'quit' - Add additional const Reported by: kib [1], vangyzen [2] Modified: head/usr.bin/top/display.c head/usr.bin/top/machine.c head/usr.bin/top/screen.c head/usr.bin/top/screen.h head/usr.bin/top/utils.c Modified: head/usr.bin/top/display.c ============================================================================== --- head/usr.bin/top/display.c Sat Jun 2 21:16:20 2018 (r334539) +++ head/usr.bin/top/display.c Sat Jun 2 21:40:45 2018 (r334540) @@ -148,8 +148,8 @@ display_resize(void) } /* now, allocate space for the screen buffer */ - screenbuf = malloc(lines * display_width); - if (screenbuf == (char *)NULL) + screenbuf = calloc(lines, display_width); + if (screenbuf == NULL) { /* oops! */ return(-1); @@ -205,23 +205,23 @@ int display_init(struct statics * statics) procstate_names = statics->procstate_names; num_procstates = string_count(procstate_names); assert(num_procstates > 0); - lprocstates = malloc(num_procstates * sizeof(int)); + lprocstates = calloc(num_procstates, sizeof(int)); cpustate_names = statics->cpustate_names; swap_names = statics->swap_names; num_swap = string_count(swap_names); assert(num_swap > 0); - lswap = malloc(num_swap * sizeof(int)); + lswap = calloc(num_swap, sizeof(int)); num_cpustates = string_count(cpustate_names); assert(num_cpustates > 0); - lcpustates = malloc(num_cpustates * sizeof(int) * statics->ncpus); - cpustate_columns = malloc(num_cpustates * sizeof(int)); + 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); assert(num_memory > 0); - lmemory = malloc(num_memory * sizeof(int)); + lmemory = calloc(num_memory, sizeof(int)); arc_names = statics->arc_names; carc_names = statics->carc_names; @@ -745,7 +745,7 @@ trim_header(char *text) width = display_width; header_length = strlen(text); if (header_length >= width) { - s = malloc((width + 1) * sizeof(char)); + s = calloc((width + 1), sizeof(char)); if (s == NULL) return (NULL); strncpy(s, text, width); Modified: head/usr.bin/top/machine.c ============================================================================== --- head/usr.bin/top/machine.c Sat Jun 2 21:16:20 2018 (r334539) +++ head/usr.bin/top/machine.c Sat Jun 2 21:40:45 2018 (r334540) @@ -382,9 +382,9 @@ machine_init(struct statics *statics) ncpus = 0; GETSYSCTL("kern.smp.maxcpus", maxcpu); size = sizeof(long) * maxcpu * CPUSTATES; - times = malloc(size); + times = calloc(size, 1); if (times == NULL) - err(1, "malloc %zu bytes", size); + err(1, "calloc %zu bytes", size); if (sysctlbyname("kern.cp_times", times, &size, NULL, 0) == -1) err(1, "sysctlbyname kern.cp_times"); pcpu_cp_time = calloc(1, size); @@ -779,11 +779,11 @@ get_process_info(struct system_info *si, struct proces */ if (previous_proc_count_max < nproc) { free(previous_procs); - previous_procs = malloc(nproc * sizeof(*previous_procs)); + previous_procs = calloc(nproc, sizeof(*previous_procs)); free(previous_pref); - previous_pref = malloc(nproc * sizeof(*previous_pref)); + previous_pref = calloc(nproc, sizeof(*previous_pref)); if (previous_procs == NULL || previous_pref == NULL) { - (void) fprintf(stderr, "top: Out of memory.\n"); + fprintf(stderr, "top: Out of memory.\n"); quit(TOP_EX_SYS_ERROR); } previous_proc_count_max = nproc; @@ -996,9 +996,9 @@ format_next_process(caddr_t xhandle, char *(*get_useri break; } - cmdbuf = malloc(cmdlen + 1); + cmdbuf = calloc(cmdlen + 1, 1); if (cmdbuf == NULL) { - warn("malloc(%d)", cmdlen + 1); + warn("calloc(%d)", cmdlen + 1); return NULL; } @@ -1031,9 +1031,9 @@ format_next_process(caddr_t xhandle, char *(*get_useri size_t len; argbuflen = cmdlen * 4; - argbuf = malloc(argbuflen + 1); + argbuf = calloc(argbuflen + 1, 1); if (argbuf == NULL) { - warn("malloc(%zu)", argbuflen + 1); + warn("calloc(%zu)", argbuflen + 1); free(cmdbuf); return NULL; } Modified: head/usr.bin/top/screen.c ============================================================================== --- head/usr.bin/top/screen.c Sat Jun 2 21:16:20 2018 (r334539) +++ head/usr.bin/top/screen.c Sat Jun 2 21:40:45 2018 (r334540) @@ -3,7 +3,7 @@ * Version 3 * * This program may be freely redistributed, - * but this entire comment MUST remain intact. + * but this entire ceomment MUST remain intact. * * Copyright (c) 1984, 1989, William LeFebvre, Rice University * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University @@ -270,7 +270,7 @@ get_screensize(void) } void -top_standout(char *msg) +top_standout(const char *msg) { if (smart_terminal) { Modified: head/usr.bin/top/screen.h ============================================================================== --- head/usr.bin/top/screen.h Sat Jun 2 21:16:20 2018 (r334539) +++ head/usr.bin/top/screen.h Sat Jun 2 21:40:45 2018 (r334540) @@ -25,9 +25,8 @@ extern char *clear_to_end; extern int screen_length; extern int screen_width; -/* a function that puts a single character on stdout */ int clear_eol(int len); -void top_standout(char *msg); +void top_standout(const char *msg); void top_clear(void); void go_home(void); void reinit_screen(void); Modified: head/usr.bin/top/utils.c ============================================================================== --- head/usr.bin/top/utils.c Sat Jun 2 21:16:20 2018 (r334539) +++ head/usr.bin/top/utils.c Sat Jun 2 21:40:45 2018 (r334540) @@ -26,8 +26,6 @@ #include <paths.h> #include <kvm.h> -void quit(int); - int atoiwi(const char *str) { @@ -201,10 +199,10 @@ argparse(char *line, int *cntp) cnt += 3; /* allocate a char * array to hold the pointers */ - argarray = malloc(cnt * sizeof(char *)); + argarray = calloc(cnt, sizeof(char *)); /* allocate another array to hold the strings themselves */ - args = malloc(length+2); + args = calloc(length+2, 1); /* initialization for main loop */ from = line;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201806022140.w52LejjF005616>