[前][次][番号順一覧][スレッド一覧]

ruby-changes:68454

From: Nobuyoshi <ko1@a...>
Date: Thu, 14 Oct 2021 18:44:54 +0900 (JST)
Subject: [ruby-changes:68454] d210950196 (master): [ruby/etc] Get rid of alloca in the loop

https://git.ruby-lang.org/ruby.git/commit/?id=d210950196

From d210950196bb24a2ff5f8afdd02b1ee95abbaf5f Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 14 Oct 2021 13:17:13 +0900
Subject: [ruby/etc] Get rid of alloca in the loop

https://github.com/ruby/etc/commit/c989bacc4c
---
 ext/etc/etc.c | 13 +++++++------
 1 file 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) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L957
 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) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L982
      */
     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) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L996
 	    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;
 	}
     }
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]