summaryrefslogtreecommitdiff
path: root/ext/etc
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-10-14 13:17:13 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-10-14 18:44:27 +0900
commitd210950196bb24a2ff5f8afdd02b1ee95abbaf5f (patch)
tree11c09ca3843544354e7b15baf3bbebba5253fa03 /ext/etc
parent1d6a490c2c67d4796c52335bdb28038960c29d9d (diff)
[ruby/etc] Get rid of alloca in the loop
https://github.com/ruby/etc/commit/c989bacc4c
Diffstat (limited to 'ext/etc')
-rw-r--r--ext/etc/etc.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/ext/etc/etc.c b/ext/etc/etc.c
index 50f566d10f..e3ff9df26e 100644
--- a/ext/etc/etc.c
+++ b/ext/etc/etc.c
@@ -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
@@ -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);
@@ -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;
}
}