From owner-svn-src-all@freebsd.org Sat Jun 2 21:40:46 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B861BFEA739; Sat, 2 Jun 2018 21:40:46 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 65EBB79867; Sat, 2 Jun 2018 21:40:46 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 47D25156EF; Sat, 2 Jun 2018 21:40:46 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w52LekqB005620; Sat, 2 Jun 2018 21:40:46 GMT (envelope-from eadler@FreeBSD.org) Received: (from eadler@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w52LejjF005616; Sat, 2 Jun 2018 21:40:45 GMT (envelope-from eadler@FreeBSD.org) Message-Id: <201806022140.w52LejjF005616@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: eadler set sender to eadler@FreeBSD.org using -f From: Eitan Adler Date: Sat, 2 Jun 2018 21:40:45 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r334540 - head/usr.bin/top X-SVN-Group: head X-SVN-Commit-Author: eadler X-SVN-Commit-Paths: head/usr.bin/top X-SVN-Commit-Revision: 334540 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 Jun 2018 21:40:47 -0000 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 #include -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;