Skip to content

Commit

Permalink
unix: pass sysctl size arg using ARRAY_SIZE macro
Browse files Browse the repository at this point in the history
PR-URL: #2510
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
  • Loading branch information
devnexen authored and bnoordhuis committed Dec 16, 2019
1 parent 93ca478 commit 2ab3dc1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/unix/darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ uint64_t uv_get_total_memory(void) {
int which[] = {CTL_HW, HW_MEMSIZE};
size_t size = sizeof(info);

if (sysctl(which, 2, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
return UV__ERR(errno);

return (uint64_t) info;
Expand All @@ -127,7 +127,7 @@ void uv_loadavg(double avg[3]) {
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_LOADAVG};

if (sysctl(which, 2, &info, &size, NULL, 0) < 0) return;
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;

avg[0] = (double) info.ldavg[0] / info.fscale;
avg[1] = (double) info.ldavg[1] / info.fscale;
Expand Down Expand Up @@ -162,7 +162,7 @@ int uv_uptime(double* uptime) {
size_t size = sizeof(info);
static int which[] = {CTL_KERN, KERN_BOOTTIME};

if (sysctl(which, 2, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
return UV__ERR(errno);

now = time(NULL);
Expand Down
8 changes: 4 additions & 4 deletions src/unix/freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int uv_exepath(char* buffer, size_t* size) {
mib[3] = -1;

abspath_size = sizeof abspath;
if (sysctl(mib, 4, abspath, &abspath_size, NULL, 0))
if (sysctl(mib, ARRAY_SIZE(mib), abspath, &abspath_size, NULL, 0))
return UV__ERR(errno);

assert(abspath_size > 0);
Expand Down Expand Up @@ -130,7 +130,7 @@ uint64_t uv_get_total_memory(void) {

size_t size = sizeof(info);

if (sysctl(which, 2, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
return UV__ERR(errno);

return (uint64_t) info;
Expand All @@ -147,7 +147,7 @@ void uv_loadavg(double avg[3]) {
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_LOADAVG};

if (sysctl(which, 2, &info, &size, NULL, 0) < 0) return;
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;

avg[0] = (double) info.ldavg[0] / info.fscale;
avg[1] = (double) info.ldavg[1] / info.fscale;
Expand All @@ -168,7 +168,7 @@ int uv_resident_set_memory(size_t* rss) {

kinfo_size = sizeof(kinfo);

if (sysctl(mib, 4, &kinfo, &kinfo_size, NULL, 0))
if (sysctl(mib, ARRAY_SIZE(mib), &kinfo, &kinfo_size, NULL, 0))
return UV__ERR(errno);

page_size = getpagesize();
Expand Down
8 changes: 4 additions & 4 deletions src/unix/netbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void uv_loadavg(double avg[3]) {
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_LOADAVG};

if (sysctl(which, 2, &info, &size, NULL, 0) == -1) return;
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) == -1) return;

avg[0] = (double) info.ldavg[0] / info.fscale;
avg[1] = (double) info.ldavg[1] / info.fscale;
Expand Down Expand Up @@ -102,7 +102,7 @@ uint64_t uv_get_free_memory(void) {
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_UVMEXP};

if (sysctl(which, 2, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
return UV__ERR(errno);

return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
Expand All @@ -119,7 +119,7 @@ uint64_t uv_get_total_memory(void) {
#endif
size_t size = sizeof(info);

if (sysctl(which, 2, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
return UV__ERR(errno);

return (uint64_t) info;
Expand Down Expand Up @@ -167,7 +167,7 @@ int uv_uptime(double* uptime) {
size_t size = sizeof(info);
static int which[] = {CTL_KERN, KERN_BOOTTIME};

if (sysctl(which, 2, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
return UV__ERR(errno);

now = time(NULL);
Expand Down
30 changes: 15 additions & 15 deletions src/unix/openbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void uv_loadavg(double avg[3]) {
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_LOADAVG};

if (sysctl(which, 2, &info, &size, NULL, 0) < 0) return;
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;

avg[0] = (double) info.ldavg[0] / info.fscale;
avg[1] = (double) info.ldavg[1] / info.fscale;
Expand Down Expand Up @@ -81,7 +81,7 @@ int uv_exepath(char* buffer, size_t* size) {
mib[1] = KERN_PROC_ARGS;
mib[2] = mypid;
mib[3] = KERN_PROC_ARGV;
if (sysctl(mib, 4, argsbuf, &argsbuf_size, NULL, 0) == 0) {
if (sysctl(mib, ARRAY_SIZE(mib), argsbuf, &argsbuf_size, NULL, 0) == 0) {
break;
}
if (errno != ENOMEM) {
Expand Down Expand Up @@ -117,7 +117,7 @@ uint64_t uv_get_free_memory(void) {
size_t size = sizeof(info);
int which[] = {CTL_VM, VM_UVMEXP};

if (sysctl(which, 2, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
return UV__ERR(errno);

return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
Expand All @@ -129,7 +129,7 @@ uint64_t uv_get_total_memory(void) {
int which[] = {CTL_HW, HW_PHYSMEM64};
size_t size = sizeof(info);

if (sysctl(which, 2, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
return UV__ERR(errno);

return (uint64_t) info;
Expand All @@ -154,7 +154,7 @@ int uv_resident_set_memory(size_t* rss) {
mib[4] = sizeof(struct kinfo_proc);
mib[5] = 1;

if (sysctl(mib, 6, &kinfo, &size, NULL, 0) < 0)
if (sysctl(mib, ARRAY_SIZE(mib), &kinfo, &size, NULL, 0) < 0)
return UV__ERR(errno);

*rss = kinfo.p_vm_rssize * page_size;
Expand All @@ -168,7 +168,7 @@ int uv_uptime(double* uptime) {
size_t size = sizeof(info);
static int which[] = {CTL_KERN, KERN_BOOTTIME};

if (sysctl(which, 2, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
return UV__ERR(errno);

now = time(NULL);
Expand All @@ -184,18 +184,19 @@ int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
uint64_t info[CPUSTATES];
char model[512];
int numcpus = 1;
int which[] = {CTL_HW,HW_MODEL,0};
int which[] = {CTL_HW,HW_MODEL};
int percpu[] = {CTL_HW,HW_CPUSPEED,0};
size_t size;
int i, j;
uv_cpu_info_t* cpu_info;

size = sizeof(model);
if (sysctl(which, 2, &model, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &model, &size, NULL, 0))
return UV__ERR(errno);

which[1] = HW_NCPUONLINE;
size = sizeof(numcpus);
if (sysctl(which, 2, &numcpus, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(which), &numcpus, &size, NULL, 0))
return UV__ERR(errno);

*cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
Expand All @@ -205,18 +206,17 @@ int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
i = 0;
*count = numcpus;

which[1] = HW_CPUSPEED;
size = sizeof(cpuspeed);
if (sysctl(which, 2, &cpuspeed, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(percpu), &cpuspeed, &size, NULL, 0))
goto error;

size = sizeof(info);
which[0] = CTL_KERN;
which[1] = KERN_CPTIME2;
percpu[0] = CTL_KERN;
percpu[1] = KERN_CPTIME2;
for (i = 0; i < numcpus; i++) {
which[2] = i;
percpu[2] = i;
size = sizeof(info);
if (sysctl(which, 3, &info, &size, NULL, 0))
if (sysctl(which, ARRAY_SIZE(percpu), &info, &size, NULL, 0))
goto error;

cpu_info = &(*cpu_infos)[i];
Expand Down

0 comments on commit 2ab3dc1

Please sign in to comment.