| 1 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 2 |
// Christopher Chang. |
| 3 |
// |
| 4 |
// This library is free software: you can redistribute it and/or modify it |
| 5 |
// under the terms of the GNU Lesser General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3 of the License, or (at your |
| 7 |
// option) any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 12 |
// for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Lesser General Public License |
| 15 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
#include "plink2_memory.h" |
| 18 |
#include "plink2_string.h" |
| 19 |
|
| 20 |
#include <unistd.h> // sysconf() |
| 21 |
#ifdef __APPLE__ |
| 22 |
# include <sys/sysctl.h> // sysctl() |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifdef __cplusplus |
| 26 |
namespace plink2 { |
| 27 |
#endif |
| 28 |
|
| 29 |
uint64_t DetectMib() { |
| 30 |
int64_t llxx; |
| 31 |
// return zero if detection failed |
| 32 |
// see e.g. http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system . |
| 33 |
#ifdef __APPLE__ |
| 34 |
int32_t mib[2]; |
| 35 |
mib[0] = CTL_HW; |
| 36 |
mib[1] = HW_MEMSIZE; |
| 37 |
llxx = 0; |
| 38 |
size_t sztmp = sizeof(int64_t); |
| 39 |
sysctl(&mib[0], 2, &llxx, &sztmp, nullptr, 0); |
| 40 |
llxx /= 1048576; |
| 41 |
#else |
| 42 |
# ifdef _WIN32 |
| 43 |
MEMORYSTATUSEX memstatus; |
| 44 |
memstatus.dwLength = sizeof(memstatus); |
| 45 |
GlobalMemoryStatusEx(&memstatus); |
| 46 |
llxx = memstatus.ullTotalPhys / 1048576; |
| 47 |
# else |
| 48 |
// TODO: check whether getrlimit(RLIMIT_AS, ...) returns a per-user limit on |
| 49 |
// shared systems. |
| 50 |
llxx = S_CAST(uint64_t, sysconf(_SC_PHYS_PAGES)) * S_CAST(size_t, sysconf(_SC_PAGESIZE)) / 1048576; |
| 51 |
# endif |
| 52 |
#endif |
| 53 |
return llxx; |
| 54 |
} |
| 55 |
|
| 56 |
uint64_t GetMemAvailableKib(__attribute__((unused)) uint32_t textbuf_blen, __attribute__((unused)) char* textbuf) { |
| 57 |
// On more recent Linux builds, the MemAvailable value reported by |
| 58 |
// "cat /proc/meminfo" is a pretty accurate estimate of how much we can |
| 59 |
// afford to allocate before introducing substantial risk of OOM-killer |
| 60 |
// action. |
| 61 |
// Returns (~0LLU) if this estimate is unavailable. |
| 62 |
#if defined(__APPLE__) || defined(_WIN32) |
| 63 |
return (~0LLU); |
| 64 |
#else |
| 65 |
FILE* meminfo = fopen("/proc/meminfo", FOPEN_RB); |
| 66 |
if (!meminfo) { |
| 67 |
return (~0LLU); |
| 68 |
} |
| 69 |
{ |
| 70 |
textbuf[textbuf_blen - 1] = ' '; |
| 71 |
do { |
| 72 |
if ((!fgets(textbuf, textbuf_blen, meminfo)) || (!textbuf[textbuf_blen - 1])) { |
| 73 |
goto GetMemAvailableKib_ret_FAIL; |
| 74 |
} |
| 75 |
} while (!StrStartsWithUnsafe(textbuf, "MemAvailable:")); |
| 76 |
const char* textbuf_iter = &(textbuf[strlen("MemAvailable:")]); |
| 77 |
textbuf_iter = FirstNonTspace(textbuf_iter); |
| 78 |
uint64_t kib_free; |
| 79 |
if (ScanmovU64Capped(1LLU << 54, &textbuf_iter, &kib_free)) { |
| 80 |
goto GetMemAvailableKib_ret_FAIL; |
| 81 |
} |
| 82 |
textbuf_iter = FirstNonTspace(textbuf_iter); |
| 83 |
if (!memequal_sk(textbuf_iter, "kB\n")) { |
| 84 |
goto GetMemAvailableKib_ret_FAIL; |
| 85 |
} |
| 86 |
return kib_free; |
| 87 |
} |
| 88 |
GetMemAvailableKib_ret_FAIL: |
| 89 |
fclose(meminfo); |
| 90 |
return (~0LLU); |
| 91 |
#endif |
| 92 |
} |
| 93 |
|
| 94 |
BoolErr arena_calloc_w(unsigned char* arena_top, uintptr_t ct, unsigned char** arena_bottom_ptr, uintptr_t** w_arr_ptr) { |
| 95 |
*w_arr_ptr = S_CAST(uintptr_t*, arena_alloc(arena_top, ct * sizeof(intptr_t), arena_bottom_ptr)); |
| 96 |
if (unlikely(!(*w_arr_ptr))) { |
| 97 |
return 1; |
| 98 |
} |
| 99 |
ZeroWArr(ct, *w_arr_ptr); |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
#ifdef __cplusplus |
| 104 |
} // namespace plink2 |
| 105 |
#endif |