| 1 |
/* |
| 2 |
* Copyright (c) 2019 |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* MP support header */ |
| 18 |
#include "MacportsLegacySupport.h" |
| 19 |
|
| 20 |
/* sysconf wrap */ |
| 21 |
#if __MP_LEGACY_SUPPORT_SYSCONF_WRAP__ |
| 22 |
|
| 23 |
#include <sys/types.h> |
| 24 |
#include <sys/sysctl.h> |
| 25 |
|
| 26 |
#include <unistd.h> |
| 27 |
#include <dlfcn.h> |
| 28 |
#include <stdlib.h> |
| 29 |
|
| 30 |
/* emulate several commonly used but missing selectors from sysconf() on various OS versions */ |
| 31 |
|
| 32 |
long sysconf(int name) { |
| 33 |
long (*real_sysconf)(int); |
| 34 |
|
| 35 |
#if __MP_LEGACY_SUPPORT_SYSCONF_WRAP_NEED_SC_NPROCESSORS_ONLN__ |
| 36 |
if ( name == _SC_NPROCESSORS_ONLN ) { |
| 37 |
|
| 38 |
int nm[2]; |
| 39 |
int ret; |
| 40 |
size_t len = 4; |
| 41 |
uint32_t count; |
| 42 |
|
| 43 |
nm[0] = CTL_HW; nm[1] = HW_AVAILCPU; |
| 44 |
ret = sysctl(nm, 2, &count, &len, NULL, 0); |
| 45 |
|
| 46 |
if (ret < 0 || count < 1) { |
| 47 |
/* try again with _SC_NPROCESSORS_CONF */ |
| 48 |
return sysconf(_SC_NPROCESSORS_CONF); |
| 49 |
} else { |
| 50 |
return (long)count; |
| 51 |
} |
| 52 |
} |
| 53 |
#endif |
| 54 |
|
| 55 |
#if __MP_LEGACY_SUPPORT_SYSCONF_WRAP_NEED_SC_NPROCESSORS_CONF__ |
| 56 |
if ( name == _SC_NPROCESSORS_CONF ) { |
| 57 |
|
| 58 |
int nm[2]; |
| 59 |
int ret; |
| 60 |
size_t len = 4; |
| 61 |
uint32_t count; |
| 62 |
|
| 63 |
nm[0] = CTL_HW; nm[1] = HW_NCPU; |
| 64 |
ret = sysctl(nm, 2, &count, &len, NULL, 0); |
| 65 |
|
| 66 |
/* there has to be at least 1 processor */ |
| 67 |
if (ret < 0 || count < 1) { count = 1; } |
| 68 |
return (long)count; |
| 69 |
} |
| 70 |
#endif |
| 71 |
|
| 72 |
#if __MP_LEGACY_SUPPORT_SYSCONF_WRAP_NEED_SC_PHYS_PAGES__ |
| 73 |
if ( name == _SC_PHYS_PAGES ) { |
| 74 |
|
| 75 |
/* the number of pages is the total memory / pagesize */ |
| 76 |
uint64_t mem_size; |
| 77 |
size_t len = sizeof(mem_size); |
| 78 |
int pagesize = getpagesize(); |
| 79 |
|
| 80 |
sysctlbyname("hw.memsize", &mem_size, &len, NULL, 0); |
| 81 |
|
| 82 |
return (long)(mem_size/pagesize); |
| 83 |
|
| 84 |
} |
| 85 |
#endif |
| 86 |
|
| 87 |
/* for any other values of "name", call the real sysconf() */ |
| 88 |
real_sysconf = dlsym(RTLD_NEXT, "sysconf"); |
| 89 |
if (real_sysconf == NULL) { |
| 90 |
exit(EXIT_FAILURE); |
| 91 |
} |
| 92 |
return real_sysconf(name); |
| 93 |
} |
| 94 |
|
| 95 |
/* compatibility function so code does not have to be recompiled */ |
| 96 |
long macports_legacy_sysconf(int name) { return sysconf(name); } |
| 97 |
|
| 98 |
#endif /*__MP_LEGACY_SUPPORT_SYSCONF_WRAP__*/ |