@@ -198,45 +198,68 @@ int get_num_procs(void);
198
198
#else
199
199
int get_num_procs (void ) {
200
200
static int nums = 0 ;
201
- cpu_set_t * cpusetp ;
202
- size_t size ;
203
- int ret ;
204
- int i ,n ;
201
+ cpu_set_t cpuset ,* cpusetp ;
202
+ size_t size ;
203
+ int ret ;
204
+
205
+ #if defined(__GLIBC_PREREQ )
206
+ #if !__GLIBC_PREREQ (2 , 7 )
207
+ int i ;
208
+ #if !__GLIBC_PREREQ (2 , 6 )
209
+ int n ;
210
+ #endif
211
+ #endif
212
+ #endif
205
213
206
214
if (!nums ) nums = sysconf (_SC_NPROCESSORS_CONF );
207
215
#if !defined(OS_LINUX )
208
- return nums ;
216
+ return nums ;
209
217
#endif
210
218
211
219
#if !defined(__GLIBC_PREREQ )
212
- return nums ;
220
+ return nums ;
213
221
#else
214
222
#if !__GLIBC_PREREQ (2 , 3 )
215
- return nums ;
223
+ return nums ;
216
224
#endif
217
225
218
226
#if !__GLIBC_PREREQ (2 , 7 )
219
- ret = sched_getaffinity (0 ,sizeof (cpu_set_t ), cpusetp );
227
+ ret = sched_getaffinity (0 ,sizeof (cpuset ), & cpuset );
220
228
if (ret != 0 ) return nums ;
221
229
n = 0 ;
222
230
#if !__GLIBC_PREREQ (2 , 6 )
223
231
for (i = 0 ;i < nums ;i ++ )
224
- if (CPU_ISSET (i ,cpusetp )) n ++ ;
232
+ if (CPU_ISSET (i ,cpuset )) n ++ ;
225
233
nums = n ;
226
234
#else
227
- nums = CPU_COUNT (sizeof (cpu_set_t ), cpusetp );
235
+ nums = CPU_COUNT (sizeof (cpuset ), & cpuset );
228
236
#endif
229
237
return nums ;
230
238
#else
231
- cpusetp = CPU_ALLOC (nums );
232
- if (cpusetp == NULL ) return nums ;
233
- size = CPU_ALLOC_SIZE (nums );
234
- ret = sched_getaffinity (0 ,size ,cpusetp );
235
- if (ret != 0 ) return nums ;
236
- ret = CPU_COUNT_S (size ,cpusetp );
237
- if (ret > 0 && ret < nums ) nums = ret ;
238
- CPU_FREE (cpusetp );
239
- return nums ;
239
+ if (nums >= CPU_SETSIZE ) {
240
+ cpusetp = CPU_ALLOC (nums );
241
+ if (cpusetp == NULL ) {
242
+ return nums ;
243
+ }
244
+ size = CPU_ALLOC_SIZE (nums );
245
+ ret = sched_getaffinity (0 ,size ,cpusetp );
246
+ if (ret != 0 ) {
247
+ CPU_FREE (cpusetp );
248
+ return nums ;
249
+ }
250
+ ret = CPU_COUNT_S (size ,cpusetp );
251
+ if (ret > 0 && ret < nums ) nums = ret ;
252
+ CPU_FREE (cpusetp );
253
+ return nums ;
254
+ } else {
255
+ ret = sched_getaffinity (0 ,sizeof (cpuset ),& cpuset );
256
+ if (ret != 0 ) {
257
+ return nums ;
258
+ }
259
+ ret = CPU_COUNT (& cpuset );
260
+ if (ret > 0 && ret < nums ) nums = ret ;
261
+ return nums ;
262
+ }
240
263
#endif
241
264
#endif
242
265
}
@@ -1709,46 +1732,70 @@ void goto_set_num_threads(int num_threads) {};
1709
1732
int get_num_procs (void );
1710
1733
#else
1711
1734
int get_num_procs (void ) {
1735
+
1712
1736
static int nums = 0 ;
1713
- cpu_set_t * cpusetp ;
1714
- size_t size ;
1715
- int ret ;
1716
- int i ,n ;
1737
+ cpu_set_t cpuset ,* cpusetp ;
1738
+ size_t size ;
1739
+ int ret ;
1740
+
1741
+ #if defined(__GLIBC_PREREQ )
1742
+ #if !__GLIBC_PREREQ (2 , 7 )
1743
+ int i ;
1744
+ #if !__GLIBC_PREREQ (2 , 6 )
1745
+ int n ;
1746
+ #endif
1747
+ #endif
1748
+ #endif
1717
1749
1718
1750
if (!nums ) nums = sysconf (_SC_NPROCESSORS_CONF );
1719
1751
#if !defined(OS_LINUX )
1720
- return nums ;
1752
+ return nums ;
1721
1753
#endif
1722
1754
1723
1755
#if !defined(__GLIBC_PREREQ )
1724
- return nums ;
1756
+ return nums ;
1725
1757
#else
1726
1758
#if !__GLIBC_PREREQ (2 , 3 )
1727
- return nums ;
1759
+ return nums ;
1728
1760
#endif
1729
1761
1730
1762
#if !__GLIBC_PREREQ (2 , 7 )
1731
- ret = sched_getaffinity (0 ,sizeof (cpu_set_t ), cpusetp );
1763
+ ret = sched_getaffinity (0 ,sizeof (cpuset ), & cpuset );
1732
1764
if (ret != 0 ) return nums ;
1733
1765
n = 0 ;
1734
1766
#if !__GLIBC_PREREQ (2 , 6 )
1735
1767
for (i = 0 ;i < nums ;i ++ )
1736
- if (CPU_ISSET (i ,cpusetp )) n ++ ;
1768
+ if (CPU_ISSET (i ,cpuset )) n ++ ;
1737
1769
nums = n ;
1738
1770
#else
1739
- nums = CPU_COUNT (sizeof (cpu_set_t ), cpusetp );
1771
+ nums = CPU_COUNT (sizeof (cpuset ), & cpuset );
1740
1772
#endif
1741
1773
return nums ;
1742
1774
#else
1743
- cpusetp = CPU_ALLOC (nums );
1744
- if (cpusetp == NULL ) return nums ;
1745
- size = CPU_ALLOC_SIZE (nums );
1746
- ret = sched_getaffinity (0 ,size ,cpusetp );
1747
- if (ret != 0 ) return nums ;
1748
- ret = CPU_COUNT_S (size ,cpusetp );
1749
- if (ret > 0 && ret < nums ) nums = ret ;
1750
- CPU_FREE (cpusetp );
1751
- return nums ;
1775
+ if (nums >= CPU_SETSIZE ) {
1776
+ cpusetp = CPU_ALLOC (nums );
1777
+ if (cpusetp == NULL ) {
1778
+ return nums ;
1779
+ }
1780
+ size = CPU_ALLOC_SIZE (nums );
1781
+ ret = sched_getaffinity (0 ,size ,cpusetp );
1782
+ if (ret != 0 ) {
1783
+ CPU_FREE (cpusetp );
1784
+ return nums ;
1785
+ }
1786
+ ret = CPU_COUNT_S (size ,cpusetp );
1787
+ if (ret > 0 && ret < nums ) nums = ret ;
1788
+ CPU_FREE (cpusetp );
1789
+ return nums ;
1790
+ } else {
1791
+ ret = sched_getaffinity (0 ,sizeof (cpuset ),& cpuset );
1792
+ if (ret != 0 ) {
1793
+ return nums ;
1794
+ }
1795
+ ret = CPU_COUNT (& cpuset );
1796
+ if (ret > 0 && ret < nums ) nums = ret ;
1797
+ return nums ;
1798
+ }
1752
1799
#endif
1753
1800
#endif
1754
1801
}
0 commit comments