summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2016-07-28 21:45:00 +0000
committertedu <tedu@openbsd.org>2016-07-28 21:45:00 +0000
commite8182c463d4d94fa8c8e6ec339cb1767c9e6d54d (patch)
tree5402026b9fe9b96168b306ac72f5984731ca27c9
parentstrncmp is a more reasonable way to check the beginning of a string. (diff)
downloadwireguard-openbsd-e8182c463d4d94fa8c8e6ec339cb1767c9e6d54d.tar.xz
wireguard-openbsd-e8182c463d4d94fa8c8e6ec339cb1767c9e6d54d.zip
rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.
-rw-r--r--usr.bin/top/machine.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c
index 72056253d51..b65b8555a6a 100644
--- a/usr.bin/top/machine.c
+++ b/usr.bin/top/machine.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: machine.c,v 1.86 2016/05/11 08:11:27 edd Exp $ */
+/* $OpenBSD: machine.c,v 1.87 2016/07/28 21:45:00 tedu Exp $ */
/*-
* Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com>
@@ -366,20 +366,25 @@ static char **
get_proc_args(struct kinfo_proc *kp)
{
static char **s;
- size_t siz = 100;
+ static size_t siz = 1023;
int mib[4];
- for (;; siz *= 2) {
- if ((s = realloc(s, siz)) == NULL)
- err(1, NULL);
- mib[0] = CTL_KERN;
- mib[1] = KERN_PROC_ARGS;
- mib[2] = kp->p_pid;
- mib[3] = KERN_PROC_ARGV;
- if (sysctl(mib, 4, s, &siz, NULL, 0) == 0)
+ if (!s && !(s = malloc(siz)))
+ err(1, NULL);
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROC_ARGS;
+ mib[2] = kp->p_pid;
+ mib[3] = KERN_PROC_ARGV;
+ for (;;) {
+ size_t space = siz;
+ if (sysctl(mib, 4, s, &space, NULL, 0) == 0)
break;
if (errno != ENOMEM)
return NULL;
+ siz *= 2;
+ if ((s = realloc(s, siz)) == NULL)
+ err(1, NULL);
}
return s;
}