Skip to content

Commit c989bac

Browse files
committed
Get rid of alloca in the loop
1 parent e0a1dd9 commit c989bac

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

ext/etc/etc.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -957,11 +957,13 @@ io_pathconf(VALUE io, VALUE arg)
957957
static int
958958
etc_nprocessors_affin(void)
959959
{
960-
cpu_set_t *cpuset;
960+
cpu_set_t *cpuset, cpuset_buff[1024 / sizeof(cpu_set_t)];
961961
size_t size;
962962
int ret;
963963
int n;
964964

965+
CPU_ZERO_S(sizeof(cpuset_buff), cpuset_buff);
966+
965967
/*
966968
* XXX:
967969
* man page says CPU_ALLOC takes number of cpus. But it is not accurate
@@ -980,13 +982,12 @@ etc_nprocessors_affin(void)
980982
*/
981983
for (n=64; n <= 16384; n *= 2) {
982984
size = CPU_ALLOC_SIZE(n);
983-
if (size >= 1024) {
985+
if (size >= sizeof(cpuset_buff)) {
984986
cpuset = xcalloc(1, size);
985987
if (!cpuset)
986988
return -1;
987989
} else {
988-
cpuset = alloca(size);
989-
CPU_ZERO_S(size, cpuset);
990+
cpuset = cpuset_buff;
990991
}
991992

992993
ret = sched_getaffinity(0, size, cpuset);
@@ -995,10 +996,10 @@ etc_nprocessors_affin(void)
995996
ret = CPU_COUNT_S(size, cpuset);
996997
}
997998

998-
if (size >= 1024) {
999+
if (size >= sizeof(cpuset_buff)) {
9991000
xfree(cpuset);
10001001
}
1001-
if (ret > 0) {
1002+
if (ret > 0 || errno != EINVAL) {
10021003
return ret;
10031004
}
10041005
}

0 commit comments

Comments
 (0)