mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 06:48:31 +00:00
Fix madvise() on Windows
This commit is contained in:
parent
f51fd97644
commit
ce0143e2a1
5 changed files with 121 additions and 35 deletions
|
@ -17,46 +17,43 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/enum/offerpriority.h"
|
||||
#include "libc/nt/memory.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/memoryrangeentry.h"
|
||||
#include "libc/sysv/consts/madv.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
forceinline typeof(PrefetchVirtualMemory) *GetPrefetchVirtualMemory(void) {
|
||||
static bool once;
|
||||
static typeof(PrefetchVirtualMemory) *PrefetchVirtualMemory_;
|
||||
if (!once) {
|
||||
typedef bool32 (*__msabi PrefetchVirtualMemoryPtr)(
|
||||
int64_t hProcess, uintptr_t NumberOfEntries,
|
||||
struct NtMemoryRangeEntry *VirtualAddresses, uint32_t reserved_Flags);
|
||||
|
||||
textwindows static PrefetchVirtualMemoryPtr GetPrefetchVirtualMemory(void) {
|
||||
static PrefetchVirtualMemoryPtr PrefetchVirtualMemory_;
|
||||
if (!PrefetchVirtualMemory_) {
|
||||
PrefetchVirtualMemory_ = /* win8.1+ */
|
||||
GetProcAddressModule("Kernel32.dll", "PrefetchVirtualMemory");
|
||||
once = true;
|
||||
}
|
||||
return PrefetchVirtualMemory_;
|
||||
}
|
||||
|
||||
forceinline typeof(OfferVirtualMemory) *GetOfferVirtualMemory(void) {
|
||||
static bool once;
|
||||
static typeof(OfferVirtualMemory) *OfferVirtualMemory_;
|
||||
if (!once) {
|
||||
typedef bool32 (*__msabi OfferVirtualMemoryPtr)(void *inout_VirtualAddress,
|
||||
size_t Size, int Priority);
|
||||
|
||||
textwindows static OfferVirtualMemoryPtr GetOfferVirtualMemory(void) {
|
||||
static OfferVirtualMemoryPtr OfferVirtualMemory_;
|
||||
if (!OfferVirtualMemory_) {
|
||||
OfferVirtualMemory_ = /* win8.1+ */
|
||||
GetProcAddressModule("Kernel32.dll", "OfferVirtualMemory");
|
||||
once = true;
|
||||
}
|
||||
return OfferVirtualMemory_;
|
||||
}
|
||||
|
||||
textwindows int sys_madvise_nt(void *addr, size_t length, int advice) {
|
||||
uint32_t rangecount;
|
||||
struct NtMemoryRangeEntry ranges[1];
|
||||
if (advice == MADV_WILLNEED || advice == MADV_SEQUENTIAL) {
|
||||
typeof(PrefetchVirtualMemory) *fn = GetPrefetchVirtualMemory();
|
||||
PrefetchVirtualMemoryPtr fn = GetPrefetchVirtualMemory();
|
||||
if (fn) {
|
||||
ranges[0].VirtualAddress = addr;
|
||||
ranges[0].NumberOfBytes = length;
|
||||
rangecount = ARRAYLEN(ranges);
|
||||
if (fn(GetCurrentProcess(), &rangecount, ranges, 0)) {
|
||||
if (fn(GetCurrentProcess(), 1, &(struct NtMemoryRangeEntry){addr, length},
|
||||
0)) {
|
||||
return 0;
|
||||
} else {
|
||||
return __winerr();
|
||||
|
@ -65,7 +62,7 @@ textwindows int sys_madvise_nt(void *addr, size_t length, int advice) {
|
|||
return enosys();
|
||||
}
|
||||
} else if (advice == MADV_FREE) {
|
||||
typeof(OfferVirtualMemory) *fn = GetOfferVirtualMemory();
|
||||
OfferVirtualMemoryPtr fn = GetOfferVirtualMemory();
|
||||
if (fn) {
|
||||
if (fn(addr, length, kNtVmOfferPriorityNormal)) {
|
||||
return 0;
|
||||
|
|
|
@ -29,21 +29,21 @@
|
|||
*
|
||||
* @param advice can be MADV_WILLNEED, MADV_SEQUENTIAL, MADV_FREE, etc.
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @raise EINVAL if `advice` isn't valid or supported by system
|
||||
* @raise EINVAL on Linux if addr/length isn't page size aligned with
|
||||
* respect to `getauxval(AT_PAGESZ)`
|
||||
* @raise ENOMEM on Linux if addr/length overlaps unmapped regions
|
||||
* @see libc/sysv/consts.sh
|
||||
* @see fadvise()
|
||||
*/
|
||||
int madvise(void *addr, size_t length, int advice) {
|
||||
int rc;
|
||||
if (advice != 127 /* see consts.sh */) {
|
||||
if (IsAsan() && !__asan_is_valid(addr, length)) {
|
||||
rc = efault();
|
||||
} else if (!IsWindows()) {
|
||||
rc = sys_madvise(addr, length, advice);
|
||||
} else {
|
||||
rc = sys_madvise_nt(addr, length, advice);
|
||||
}
|
||||
if (IsAsan() && !__asan_is_valid(addr, length)) {
|
||||
rc = efault();
|
||||
} else if (!IsWindows()) {
|
||||
rc = sys_madvise(addr, length, advice);
|
||||
} else {
|
||||
rc = einval();
|
||||
rc = sys_madvise_nt(addr, length, advice);
|
||||
}
|
||||
STRACE("madvise(%p, %'zu, %d) → %d% m", addr, length, advice, rc);
|
||||
return rc;
|
||||
|
|
|
@ -23,6 +23,10 @@
|
|||
* Advises kernel about memory intentions, the POSIX way.
|
||||
*
|
||||
* @return 0 on success, or errno on error
|
||||
* @raise EINVAL if `advice` isn't valid or supported by system
|
||||
* @raise EINVAL on Linux if addr/length isn't page size aligned with
|
||||
* respect to `getauxval(AT_PAGESZ)`
|
||||
* @raise ENOMEM on Linux if addr/length overlaps unmapped regions
|
||||
* @returnserrno
|
||||
*/
|
||||
errno_t posix_madvise(void *addr, uint64_t len, int advice) {
|
||||
|
|
|
@ -73,12 +73,6 @@ uint64_t VirtualQuery(const void *lpAddress,
|
|||
void *VirtualAllocEx(int64_t hProcess, void *lpAddress, uint64_t dwSize,
|
||||
uint32_t flAllocationType, uint32_t flProtect);
|
||||
|
||||
bool32 PrefetchVirtualMemory(int64_t hProcess, const uint32_t *NumberOfEntries,
|
||||
struct NtMemoryRangeEntry *VirtualAddresses,
|
||||
uint32_t reserved_Flags);
|
||||
bool32 OfferVirtualMemory(void *inout_VirtualAddress, size_t Size,
|
||||
int Priority);
|
||||
|
||||
int64_t GetProcessHeap(void);
|
||||
void *HeapAlloc(int64_t hHeap, uint32_t dwFlags, size_t dwBytes) __wur;
|
||||
bool32 HeapFree(int64_t hHeap, uint32_t dwFlags, void *opt_lpMem);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue