Skip to content

Commit

Permalink
Get rid of alloca in the loop
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Oct 14, 2021
1 parent e0a1dd9 commit c989bac
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions ext/etc/etc.c
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,13 @@ io_pathconf(VALUE io, VALUE arg)
static int
etc_nprocessors_affin(void)
{
cpu_set_t *cpuset;
cpu_set_t *cpuset, cpuset_buff[1024 / sizeof(cpu_set_t)];
size_t size;
int ret;
int n;

CPU_ZERO_S(sizeof(cpuset_buff), cpuset_buff);

/*
* XXX:
* man page says CPU_ALLOC takes number of cpus. But it is not accurate
Expand All @@ -980,13 +982,12 @@ etc_nprocessors_affin(void)
*/
for (n=64; n <= 16384; n *= 2) {
size = CPU_ALLOC_SIZE(n);
if (size >= 1024) {
if (size >= sizeof(cpuset_buff)) {
cpuset = xcalloc(1, size);
if (!cpuset)
return -1;
} else {
cpuset = alloca(size);
CPU_ZERO_S(size, cpuset);
cpuset = cpuset_buff;
}

ret = sched_getaffinity(0, size, cpuset);
Expand All @@ -995,10 +996,10 @@ etc_nprocessors_affin(void)
ret = CPU_COUNT_S(size, cpuset);
}

if (size >= 1024) {
if (size >= sizeof(cpuset_buff)) {
xfree(cpuset);
}
if (ret > 0) {
if (ret > 0 || errno != EINVAL) {
return ret;
}
}
Expand Down

0 comments on commit c989bac

Please sign in to comment.