1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/MemUtils.h"
11 # include "mozilla/Maybe.h"
13 # include <sys/mman.h>
17 typedef BOOL(WINAPI
* PrefetchVirtualMemoryFn
)(HANDLE
, ULONG_PTR
, PVOID
, ULONG
);
19 static mozilla::Maybe
<PrefetchVirtualMemoryFn
> sPrefetchVirtualMemory
;
21 void MaybeInitPrefetchVirtualMemory() {
22 if (sPrefetchVirtualMemory
.isNothing()) {
23 sPrefetchVirtualMemory
.emplace(
24 reinterpret_cast<PrefetchVirtualMemoryFn
>(GetProcAddress(
25 GetModuleHandleW(L
"kernel32.dll"), "PrefetchVirtualMemory")));
30 bool mozilla::CanPrefetchMemory() {
31 #if defined(XP_SOLARIS) || defined(XP_UNIX)
34 MaybeInitPrefetchVirtualMemory();
35 return *sPrefetchVirtualMemory
;
41 void mozilla::PrefetchMemory(uint8_t* aStart
, size_t aNumBytes
) {
46 #if defined(XP_SOLARIS)
47 posix_madvise(aStart
, aNumBytes
, POSIX_MADV_WILLNEED
);
48 #elif defined(XP_UNIX)
49 madvise(aStart
, aNumBytes
, MADV_WILLNEED
);
51 MaybeInitPrefetchVirtualMemory();
52 if (*sPrefetchVirtualMemory
) {
53 // Normally, we'd use WIN32_MEMORY_RANGE_ENTRY, but that requires
54 // a different _WIN32_WINNT value before including windows.h, but
55 // that causes complications with unified sources. It's a simple
56 // enough struct anyways.
61 entry
.VirtualAddress
= aStart
;
62 entry
.NumberOfBytes
= aNumBytes
;
63 (*sPrefetchVirtualMemory
)(GetCurrentProcess(), 1, &entry
, 0);