Commits

Anonymous committed 11f4b7f

on darwin use sysctl rather than sysctlbyname

  • Participants
  • Parent commits f8b5db5

Comments (0)

Files changed (1)

 // _SC_NPROCESSORS_CONF, _SC_NPROCESSORS_MAX
 
 int cnt(void) {
-#if defined(__DARWIN__) || defined(__FREEBSD__)
-	int c;
-	size_t s = sizeof(int);
-	if(sysctlbyname("hw.ncpu", &c, &s, NULL, 0) != 0)
-		return 1;
-	return c;
+#if defined(__DARWIN__) 
+	int mib[2]; uint32_t cnt; size_t len = 4;
+	mib[0] = CTL_HW; mib[1] = HW_AVAILCPU;
+	sysctl(mib, 2, &cnt, &len, NULL, 0);
+        if(cnt < 1) {
+            mib[1] = HW_NCPU;
+            sysctl(mib, 2, &cnt, &len, NULL, 0);
+            if(cnt < 1) { cnt = 1; }
+        }
+        return cnt;
+#elif  defined(__FREEBSD__)
+	int cnt; size_t len = sizeof(int);
+	if(sysctlbyname("hw.ncpu", &cnt, &len, NULL, 0) != 0) { cnt = 1; }
+	return cnt;
 #elif defined(__WINDOWS__)
 	SYSTEM_INFO sysinfo;
 	GetSystemInfo(&sysinfo);