From 03f9a86918559525c816eb4c97df73bdef38bd38 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sun, 22 Nov 2020 00:59:00 +0100 Subject: [PATCH] Reduce scope of local variables --- linux/LinuxProcessList.c | 3 +-- openbsd/OpenBSDProcessList.c | 19 +++++++------------ zfs/ZfsArcMeter.c | 2 +- zfs/ZfsCompressedArcMeter.c | 2 +- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 4f40acad..7be54160 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -804,7 +804,6 @@ static int handleNetlinkMsg(struct nl_msg* nlmsg, void* linuxProcess) { struct nlattr* nlattr; struct taskstats stats; int rem; - unsigned long long int timeDelta; LinuxProcess* lp = (LinuxProcess*) linuxProcess; nlhdr = nlmsg_hdr(nlmsg); @@ -817,7 +816,7 @@ static int handleNetlinkMsg(struct nl_msg* nlmsg, void* linuxProcess) { memcpy(&stats, nla_data(nla_next(nla_data(nlattr), &rem)), sizeof(stats)); assert(lp->super.pid == (pid_t)stats.ac_pid); - timeDelta = stats.ac_etime * 1000 - lp->delay_read_time; + unsigned long long int timeDelta = stats.ac_etime * 1000 - lp->delay_read_time; #define BOUNDS(x) (isnan(x) ? 0.0 : ((x) > 100) ? 100.0 : (x)) #define DELTAPERC(x,y) BOUNDS((float) ((x) - (y)) / timeDelta * 100) lp->cpu_delay_percent = DELTAPERC(stats.cpu_delay_total, lp->cpu_delay_total); diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index 6b0b3951..cc3de5bb 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -136,7 +136,7 @@ static inline void OpenBSDProcessList_scanMemoryInfo(ProcessList* pl) { char* OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) { char *s, **arg; - size_t len = 0, n; + size_t len = 0; int i; /* @@ -160,7 +160,7 @@ char* OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in *s = '\0'; for (i = 0; arg[i] != NULL; i++) { - n = strlcat(s, arg[i], len); + size_t n = strlcat(s, arg[i], len); if (i == 0) { /* TODO: rename all basenameEnd to basenameLen, make size_t */ *basenameEnd = MINIMUM(n, len - 1); @@ -188,23 +188,18 @@ static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { const Settings* settings = this->super.settings; bool hideKernelThreads = settings->hideKernelThreads; bool hideUserlandThreads = settings->hideUserlandThreads; - struct kinfo_proc* kproc; - bool preExisting; - Process* proc; - OpenBSDProcess* fp; int count = 0; - int i; // use KERN_PROC_KTHREAD to also include kernel threads struct kinfo_proc* kprocs = kvm_getprocs(this->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count); //struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count); - for (i = 0; i < count; i++) { - kproc = &kprocs[i]; + for (int i = 0; i < count; i++) { + struct kinfo_proc* kproc = &kprocs[i]; - preExisting = false; - proc = ProcessList_getProcess(&this->super, kproc->p_pid, &preExisting, OpenBSDProcess_new); - fp = (OpenBSDProcess*) proc; + bool preExisting = false; + Process* proc = ProcessList_getProcess(&this->super, kproc->p_pid, &preExisting, OpenBSDProcess_new); + OpenBSDProcess* fp = (OpenBSDProcess*) proc; proc->show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc))); diff --git a/zfs/ZfsArcMeter.c b/zfs/ZfsArcMeter.c index 72af3bc7..9bfc44b7 100644 --- a/zfs/ZfsArcMeter.c +++ b/zfs/ZfsArcMeter.c @@ -47,10 +47,10 @@ static void ZfsArcMeter_updateValues(Meter* this, char* buffer, int size) { } static void ZfsArcMeter_display(const Object* cast, RichString* out) { - char buffer[50]; const Meter* this = (const Meter*)cast; if (this->values[5] > 0) { + char buffer[50]; Meter_humanUnit(buffer, this->total, 50); RichString_append(out, CRT_colors[METER_VALUE], buffer); Meter_humanUnit(buffer, this->values[5], 50); diff --git a/zfs/ZfsCompressedArcMeter.c b/zfs/ZfsCompressedArcMeter.c index d0278a9d..31773664 100644 --- a/zfs/ZfsCompressedArcMeter.c +++ b/zfs/ZfsCompressedArcMeter.c @@ -43,10 +43,10 @@ static void ZfsCompressedArcMeter_updateValues(Meter* this, char* buffer, int si } static void ZfsCompressedArcMeter_display(const Object* cast, RichString* out) { - char buffer[50]; const Meter* this = (const Meter*)cast; if (this->values[0] > 0) { + char buffer[50]; Meter_humanUnit(buffer, this->total, 50); RichString_append(out, CRT_colors[METER_VALUE], buffer); RichString_append(out, CRT_colors[METER_TEXT], " Uncompressed, ");