llama-mmap.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /**
  2. * llama.cpp - commit 46e3556e01b824e52395fb050b29804b6cff2a7c - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #include "llama-mmap.h"
  27. #include "llama-impl.h"
  28. #include "ggml.h"
  29. #include <cstring>
  30. #include <climits>
  31. #include <stdexcept>
  32. #ifdef __has_include
  33. #if __has_include(<unistd.h>)
  34. #include <unistd.h>
  35. #if defined(_POSIX_MAPPED_FILES)
  36. #include <sys/mman.h>
  37. #include <fcntl.h>
  38. #endif
  39. #if defined(_POSIX_MEMLOCK_RANGE)
  40. #include <sys/resource.h>
  41. #endif
  42. #endif
  43. #endif
  44. #if defined(_WIN32)
  45. #define WIN32_LEAN_AND_MEAN
  46. #ifndef NOMINMAX
  47. #define NOMINMAX
  48. #endif
  49. #include <windows.h>
  50. #ifndef PATH_MAX
  51. #define PATH_MAX MAX_PATH
  52. #endif
  53. #include <io.h>
  54. #endif
  55. // TODO: consider moving to llama-impl.h if needed in more places
  56. #if defined(_WIN32)
  57. std::string llama_format_win_err(DWORD err) {
  58. LPSTR buf;
  59. size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  60. NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL);
  61. if (!size) {
  62. return "FormatMessageA failed";
  63. }
  64. std::string ret(buf, size);
  65. LocalFree(buf);
  66. return ret;
  67. }
  68. #endif
  69. // llama_file
  70. struct llama_file::impl {
  71. #if defined(_WIN32)
  72. HANDLE fp_win32;
  73. std::string GetErrorMessageWin32(DWORD error_code) const {
  74. std::string ret;
  75. LPSTR lpMsgBuf = NULL;
  76. DWORD bufLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  77. NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf, 0, NULL);
  78. if (!bufLen) {
  79. ret = format("Win32 error code: %lx", error_code);
  80. } else {
  81. ret = lpMsgBuf;
  82. LocalFree(lpMsgBuf);
  83. }
  84. return ret;
  85. }
  86. impl(const char * fname, const char * mode) {
  87. fp = ggml_fopen(fname, mode);
  88. if (fp == NULL) {
  89. throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
  90. }
  91. fp_win32 = (HANDLE) _get_osfhandle(_fileno(fp));
  92. seek(0, SEEK_END);
  93. size = tell();
  94. seek(0, SEEK_SET);
  95. }
  96. size_t tell() const {
  97. LARGE_INTEGER li;
  98. li.QuadPart = 0;
  99. BOOL ret = SetFilePointerEx(fp_win32, li, &li, FILE_CURRENT);
  100. if (!ret) {
  101. throw std::runtime_error(format("read error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
  102. }
  103. return li.QuadPart;
  104. }
  105. void seek(size_t offset, int whence) const {
  106. static_assert(SEEK_SET == FILE_BEGIN, "SEEK_SET != FILE_BEGIN");
  107. static_assert(SEEK_CUR == FILE_CURRENT, "SEEK_CUR != FILE_CURRENT");
  108. static_assert(SEEK_END == FILE_END, "SEEK_END != FILE_END");
  109. LARGE_INTEGER li;
  110. li.QuadPart = offset;
  111. BOOL ret = SetFilePointerEx(fp_win32, li, NULL, whence);
  112. if (!ret) {
  113. throw std::runtime_error(format("read error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
  114. }
  115. }
  116. void read_raw(void * ptr, size_t len) const {
  117. size_t bytes_read = 0;
  118. while (bytes_read < len) {
  119. size_t chunk_size = std::min<size_t>(len - bytes_read, 64*1024*1024);
  120. DWORD chunk_read = 0;
  121. BOOL result = ReadFile(fp_win32, reinterpret_cast<char*>(ptr) + bytes_read, chunk_size, &chunk_read, NULL);
  122. if (!result) {
  123. throw std::runtime_error(format("read error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
  124. }
  125. if (chunk_read < chunk_size || chunk_read == 0) {
  126. throw std::runtime_error("unexpectedly reached end of file");
  127. }
  128. bytes_read += chunk_read;
  129. }
  130. }
  131. uint32_t read_u32() const {
  132. uint32_t val;
  133. read_raw(&val, sizeof(val));
  134. return val;
  135. }
  136. void write_raw(const void * ptr, size_t len) const {
  137. size_t bytes_written = 0;
  138. while (bytes_written < len) {
  139. size_t chunk_size = std::min<size_t>(len - bytes_written, 64*1024*1024);
  140. DWORD chunk_written = 0;
  141. BOOL result = WriteFile(fp_win32, reinterpret_cast<char const*>(ptr) + bytes_written, chunk_size, &chunk_written, NULL);
  142. if (!result) {
  143. throw std::runtime_error(format("write error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
  144. }
  145. if (chunk_written < chunk_size || chunk_written == 0) {
  146. throw std::runtime_error("unexpectedly failed to write bytes");
  147. }
  148. bytes_written += chunk_written;
  149. }
  150. }
  151. void write_u32(uint32_t val) const {
  152. write_raw(&val, sizeof(val));
  153. }
  154. ~impl() {
  155. if (fp) {
  156. std::fclose(fp);
  157. }
  158. }
  159. #else
  160. impl(const char * fname, const char * mode) {
  161. fp = ggml_fopen(fname, mode);
  162. if (fp == NULL) {
  163. throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
  164. }
  165. seek(0, SEEK_END);
  166. size = tell();
  167. seek(0, SEEK_SET);
  168. }
  169. size_t tell() const {
  170. // TODO: this ifdef is never true?
  171. #ifdef _WIN32
  172. __int64 ret = _ftelli64(fp);
  173. #else
  174. long ret = std::ftell(fp);
  175. #endif
  176. if (ret == -1) {
  177. throw std::runtime_error(format("ftell error: %s", strerror(errno)));
  178. }
  179. return (size_t) ret;
  180. }
  181. void seek(size_t offset, int whence) const {
  182. // TODO: this ifdef is never true?
  183. #ifdef _WIN32
  184. int ret = _fseeki64(fp, (__int64) offset, whence);
  185. #else
  186. int ret = std::fseek(fp, (long) offset, whence);
  187. #endif
  188. if (ret != 0) {
  189. throw std::runtime_error(format("seek error: %s", strerror(errno)));
  190. }
  191. }
  192. void read_raw(void * ptr, size_t len) const {
  193. if (len == 0) {
  194. return;
  195. }
  196. errno = 0;
  197. std::size_t ret = std::fread(ptr, len, 1, fp);
  198. if (ferror(fp)) {
  199. throw std::runtime_error(format("read error: %s", strerror(errno)));
  200. }
  201. if (ret != 1) {
  202. throw std::runtime_error("unexpectedly reached end of file");
  203. }
  204. }
  205. uint32_t read_u32() const {
  206. uint32_t ret;
  207. read_raw(&ret, sizeof(ret));
  208. return ret;
  209. }
  210. void write_raw(const void * ptr, size_t len) const {
  211. if (len == 0) {
  212. return;
  213. }
  214. errno = 0;
  215. size_t ret = std::fwrite(ptr, len, 1, fp);
  216. if (ret != 1) {
  217. throw std::runtime_error(format("write error: %s", strerror(errno)));
  218. }
  219. }
  220. void write_u32(uint32_t val) const {
  221. write_raw(&val, sizeof(val));
  222. }
  223. ~impl() {
  224. if (fp) {
  225. std::fclose(fp);
  226. }
  227. }
  228. #endif
  229. FILE * fp;
  230. size_t size;
  231. };
  232. llama_file::llama_file(const char * fname, const char * mode) : pimpl(std::make_unique<impl>(fname, mode)) {}
  233. llama_file::~llama_file() = default;
  234. size_t llama_file::tell() const { return pimpl->tell(); }
  235. size_t llama_file::size() const { return pimpl->size; }
  236. int llama_file::fileno() const {
  237. #ifdef _WIN32
  238. return _fileno(pimpl->fp);
  239. #else
  240. return ::fileno(pimpl->fp);
  241. #endif
  242. }
  243. void llama_file::seek(size_t offset, int whence) const { pimpl->seek(offset, whence); }
  244. void llama_file::read_raw(void * ptr, size_t len) const { pimpl->read_raw(ptr, len); }
  245. uint32_t llama_file::read_u32() const { return pimpl->read_u32(); }
  246. void llama_file::write_raw(const void * ptr, size_t len) const { pimpl->write_raw(ptr, len); }
  247. void llama_file::write_u32(uint32_t val) const { pimpl->write_u32(val); }
  248. // llama_mmap
  249. struct llama_mmap::impl {
  250. #ifdef _POSIX_MAPPED_FILES
  251. std::vector<std::pair<size_t, size_t>> mapped_fragments;
  252. impl(struct llama_file * file, size_t prefetch, bool numa) {
  253. size = file->size();
  254. int fd = file->fileno();
  255. int flags = MAP_SHARED;
  256. if (numa) { prefetch = 0; }
  257. #ifdef __linux__
  258. if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL)) {
  259. LLAMA_LOG_WARN("warning: posix_fadvise(.., POSIX_FADV_SEQUENTIAL) failed: %s\n",
  260. strerror(errno));
  261. }
  262. if (prefetch) { flags |= MAP_POPULATE; }
  263. #endif
  264. addr = mmap(NULL, file->size(), PROT_READ, flags, fd, 0);
  265. if (addr == MAP_FAILED) {
  266. throw std::runtime_error(format("mmap failed: %s", strerror(errno)));
  267. }
  268. if (prefetch > 0) {
  269. if (posix_madvise(addr, std::min(file->size(), prefetch), POSIX_MADV_WILLNEED)) {
  270. LLAMA_LOG_WARN("warning: posix_madvise(.., POSIX_MADV_WILLNEED) failed: %s\n",
  271. strerror(errno));
  272. }
  273. }
  274. if (numa) {
  275. if (posix_madvise(addr, file->size(), POSIX_MADV_RANDOM)) {
  276. LLAMA_LOG_WARN("warning: posix_madvise(.., POSIX_MADV_RANDOM) failed: %s\n",
  277. strerror(errno));
  278. }
  279. }
  280. mapped_fragments.emplace_back(0, file->size());
  281. }
  282. static void align_range(size_t * first, size_t * last, size_t page_size) {
  283. size_t offset_in_page = *first & (page_size - 1);
  284. size_t offset_to_page = offset_in_page == 0 ? 0 : page_size - offset_in_page;
  285. *first += offset_to_page;
  286. *last = *last & ~(page_size - 1);
  287. if (*last <= *first) {
  288. *last = *first;
  289. }
  290. }
  291. void unmap_fragment(size_t first, size_t last) {
  292. int page_size = sysconf(_SC_PAGESIZE);
  293. align_range(&first, &last, page_size);
  294. size_t len = last - first;
  295. if (len == 0) {
  296. return;
  297. }
  298. GGML_ASSERT(first % page_size == 0);
  299. GGML_ASSERT(last % page_size == 0);
  300. GGML_ASSERT(last > first);
  301. void * next_page_start = (uint8_t *) addr + first;
  302. if (munmap(next_page_start, len)) {
  303. LLAMA_LOG_WARN("warning: munmap failed: %s\n", strerror(errno));
  304. }
  305. std::vector<std::pair<size_t, size_t>> new_mapped_fragments;
  306. for (const auto & frag : mapped_fragments) {
  307. if (frag.first < first && frag.second > last) {
  308. new_mapped_fragments.emplace_back(frag.first, first);
  309. new_mapped_fragments.emplace_back(last, frag.second);
  310. } else if (frag.first < first && frag.second > first) {
  311. new_mapped_fragments.emplace_back(frag.first, first);
  312. } else if (frag.first < last && frag.second > last) {
  313. new_mapped_fragments.emplace_back(last, frag.second);
  314. } else if (frag.first >= first && frag.second <= last) {
  315. } else {
  316. new_mapped_fragments.push_back(frag);
  317. }
  318. }
  319. mapped_fragments = std::move(new_mapped_fragments);
  320. }
  321. ~impl() {
  322. for (const auto & frag : mapped_fragments) {
  323. if (munmap((char *) addr + frag.first, frag.second - frag.first)) {
  324. LLAMA_LOG_WARN("warning: munmap failed: %s\n", strerror(errno));
  325. }
  326. }
  327. }
  328. #elif defined(_WIN32)
  329. impl(struct llama_file * file, size_t prefetch, bool numa) {
  330. GGML_UNUSED(numa);
  331. size = file->size();
  332. HANDLE hFile = (HANDLE) _get_osfhandle(file->fileno());
  333. HANDLE hMapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  334. if (hMapping == NULL) {
  335. DWORD error = GetLastError();
  336. throw std::runtime_error(format("CreateFileMappingA failed: %s", llama_format_win_err(error).c_str()));
  337. }
  338. addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
  339. DWORD error = GetLastError();
  340. CloseHandle(hMapping);
  341. if (addr == NULL) {
  342. throw std::runtime_error(format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str()));
  343. }
  344. if (prefetch > 0) {
  345. #if _WIN32_WINNT >= 0x602
  346. BOOL (WINAPI *pPrefetchVirtualMemory) (HANDLE, ULONG_PTR, PWIN32_MEMORY_RANGE_ENTRY, ULONG);
  347. HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
  348. pPrefetchVirtualMemory = (decltype(pPrefetchVirtualMemory))(void *) GetProcAddress(hKernel32, "PrefetchVirtualMemory");
  349. if (pPrefetchVirtualMemory) {
  350. WIN32_MEMORY_RANGE_ENTRY range;
  351. range.VirtualAddress = addr;
  352. range.NumberOfBytes = (SIZE_T) std::min(size, prefetch);
  353. if (!pPrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) {
  354. LLAMA_LOG_WARN("warning: PrefetchVirtualMemory failed: %s\n",
  355. llama_format_win_err(GetLastError()).c_str());
  356. }
  357. }
  358. #else
  359. throw std::runtime_error("PrefetchVirtualMemory unavailable");
  360. #endif
  361. }
  362. }
  363. void unmap_fragment(size_t first, size_t last) {
  364. GGML_UNUSED(first);
  365. GGML_UNUSED(last);
  366. }
  367. ~impl() {
  368. if (!UnmapViewOfFile(addr)) {
  369. LLAMA_LOG_WARN("warning: UnmapViewOfFile failed: %s\n",
  370. llama_format_win_err(GetLastError()).c_str());
  371. }
  372. }
  373. #else
  374. impl(struct llama_file * file, size_t prefetch, bool numa) {
  375. GGML_UNUSED(file);
  376. GGML_UNUSED(prefetch);
  377. GGML_UNUSED(numa);
  378. throw std::runtime_error("mmap not supported");
  379. }
  380. void unmap_fragment(size_t first, size_t last) {
  381. GGML_UNUSED(first);
  382. GGML_UNUSED(last);
  383. throw std::runtime_error("mmap not supported");
  384. }
  385. #endif
  386. void * addr;
  387. size_t size;
  388. };
  389. llama_mmap::llama_mmap(struct llama_file * file, size_t prefetch, bool numa) : pimpl(std::make_unique<impl>(file, prefetch, numa)) {}
  390. llama_mmap::~llama_mmap() = default;
  391. size_t llama_mmap::size() const { return pimpl->size; }
  392. void * llama_mmap::addr() const { return pimpl->addr; }
  393. void llama_mmap::unmap_fragment(size_t first, size_t last) { pimpl->unmap_fragment(first, last); }
  394. #if defined(_POSIX_MEMLOCK_RANGE) || defined(_WIN32)
  395. const bool llama_mmap::SUPPORTED = true;
  396. #else
  397. const bool llama_mmap::SUPPORTED = false;
  398. #endif
  399. // llama_mlock
  400. struct llama_mlock::impl {
  401. #ifdef _POSIX_MEMLOCK_RANGE
  402. static size_t lock_granularity() {
  403. return (size_t) sysconf(_SC_PAGESIZE);
  404. }
  405. bool raw_lock(const void * addr, size_t size) const {
  406. if (!mlock(addr, size)) {
  407. return true;
  408. }
  409. #ifdef __APPLE__
  410. #define MLOCK_SUGGESTION \
  411. "Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or " \
  412. "decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MEMLOCK (ulimit -l).\n"
  413. #else
  414. #define MLOCK_SUGGESTION \
  415. "Try increasing RLIMIT_MEMLOCK ('ulimit -l' as root).\n"
  416. #endif
  417. char* errmsg = std::strerror(errno);
  418. bool suggest = (errno == ENOMEM);
  419. struct rlimit lock_limit;
  420. if (suggest && getrlimit(RLIMIT_MEMLOCK, &lock_limit)) {
  421. suggest = false;
  422. }
  423. if (suggest && (lock_limit.rlim_max > lock_limit.rlim_cur + size)) {
  424. suggest = false;
  425. }
  426. LLAMA_LOG_WARN("warning: failed to mlock %zu-byte buffer (after previously locking %zu bytes): %s\n%s",
  427. size, this->size, errmsg, suggest ? MLOCK_SUGGESTION : "");
  428. return false;
  429. }
  430. static void raw_unlock(void * addr, size_t size) {
  431. if (munlock(addr, size)) {
  432. LLAMA_LOG_WARN("warning: failed to munlock buffer: %s\n", std::strerror(errno));
  433. }
  434. }
  435. #elif defined(_WIN32)
  436. static size_t lock_granularity() {
  437. SYSTEM_INFO si;
  438. GetSystemInfo(&si);
  439. return (size_t) si.dwPageSize;
  440. }
  441. bool raw_lock(void * ptr, size_t len) const {
  442. for (int tries = 1; ; tries++) {
  443. if (VirtualLock(ptr, len)) {
  444. return true;
  445. }
  446. if (tries == 2) {
  447. LLAMA_LOG_WARN("warning: failed to VirtualLock %zu-byte buffer (after previously locking %zu bytes): %s\n",
  448. len, size, llama_format_win_err(GetLastError()).c_str());
  449. return false;
  450. }
  451. SIZE_T min_ws_size, max_ws_size;
  452. if (!GetProcessWorkingSetSize(GetCurrentProcess(), &min_ws_size, &max_ws_size)) {
  453. LLAMA_LOG_WARN("warning: GetProcessWorkingSetSize failed: %s\n",
  454. llama_format_win_err(GetLastError()).c_str());
  455. return false;
  456. }
  457. size_t increment = len + 1048576;
  458. min_ws_size += increment;
  459. max_ws_size += increment;
  460. if (!SetProcessWorkingSetSize(GetCurrentProcess(), min_ws_size, max_ws_size)) {
  461. LLAMA_LOG_WARN("warning: SetProcessWorkingSetSize failed: %s\n",
  462. llama_format_win_err(GetLastError()).c_str());
  463. return false;
  464. }
  465. }
  466. }
  467. static void raw_unlock(void * ptr, size_t len) {
  468. if (!VirtualUnlock(ptr, len)) {
  469. LLAMA_LOG_WARN("warning: failed to VirtualUnlock buffer: %s\n",
  470. llama_format_win_err(GetLastError()).c_str());
  471. }
  472. }
  473. #else
  474. static size_t lock_granularity() {
  475. return (size_t) 65536;
  476. }
  477. bool raw_lock(const void * addr, size_t len) const {
  478. LLAMA_LOG_WARN("warning: mlock not supported on this system\n");
  479. return false;
  480. }
  481. static void raw_unlock(const void * addr, size_t len) {}
  482. #endif
  483. impl() : addr(NULL), size(0), failed_already(false) {}
  484. void init(void * ptr) {
  485. GGML_ASSERT(addr == NULL && size == 0);
  486. addr = ptr;
  487. }
  488. void grow_to(size_t target_size) {
  489. GGML_ASSERT(addr);
  490. if (failed_already) {
  491. return;
  492. }
  493. size_t granularity = lock_granularity();
  494. target_size = (target_size + granularity - 1) & ~(granularity - 1);
  495. if (target_size > size) {
  496. if (raw_lock((uint8_t *) addr + size, target_size - size)) {
  497. size = target_size;
  498. } else {
  499. failed_already = true;
  500. }
  501. }
  502. }
  503. void * addr;
  504. size_t size;
  505. bool failed_already;
  506. };
  507. llama_mlock::llama_mlock() : pimpl(std::make_unique<impl>()) {}
  508. llama_mlock::~llama_mlock() = default;
  509. void llama_mlock::init(void * ptr) { pimpl->init(ptr); }
  510. void llama_mlock::grow_to(size_t target_size) { pimpl->grow_to(target_size); }
  511. #if defined(_POSIX_MEMLOCK_RANGE) || defined(_WIN32)
  512. const bool llama_mlock::SUPPORTED = true;
  513. #else
  514. const bool llama_mlock::SUPPORTED = false;
  515. #endif
  516. size_t llama_path_max() {
  517. return PATH_MAX;
  518. }