Skip to content

Commit

Permalink
Bug 988831 - support memory mapped files in Windows. r=ehoogeveen
Browse files Browse the repository at this point in the history
--HG--
extra : rebase_source : faf8d37b0d4d517b8a859b3fa57f8cd7f914de8f
  • Loading branch information
Benjamin Dahse committed Jun 30, 2016
1 parent 0642c8d commit 2400e06
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
2 changes: 0 additions & 2 deletions b2g/app/b2g.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,9 +1021,7 @@ pref("identity.fxaccounts.remote.profile.uri", "https://profile.accounts.firefox
pref("identity.fxaccounts.skipDeviceRegistration", true);

// Enable mapped array buffer.
#ifndef XP_WIN
pref("dom.mapped_arraybuffer.enabled", true);
#endif

// SystemUpdate API
pref("dom.system_update.enabled", true);
Expand Down
2 changes: 0 additions & 2 deletions dom/base/test/chrome.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ subsuite = clipboard
[test_messagemanager_send_principal.html]
skip-if = buildapp == 'mulet'
[test_bug945152.html]
run-if = os == 'linux'
[test_bug1008126.html]
run-if = os == 'linux'
[test_sandboxed_blob_uri.html]
[test_websocket_frame.html]
8 changes: 1 addition & 7 deletions dom/xhr/XMLHttpRequestMainThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2728,7 +2728,7 @@ XMLHttpRequestMainThread::Send(nsIVariant* aVariant, const Nullable<RequestBody>

mIsMappedArrayBuffer = false;
if (mResponseType == XMLHttpRequestResponseType::Arraybuffer &&
Preferences::GetBool("dom.mapped_arraybuffer.enabled", false)) {
Preferences::GetBool("dom.mapped_arraybuffer.enabled", true)) {
nsCOMPtr<nsIURI> uri;
nsAutoCString scheme;

Expand Down Expand Up @@ -3757,11 +3757,6 @@ nsresult
ArrayBufferBuilder::mapToFileInPackage(const nsCString& aFile,
nsIFile* aJarFile)
{
#ifdef XP_WIN
// TODO: Bug 988813 - Support memory mapped array buffer for Windows platform.
MOZ_CRASH("Not implemented");
return NS_ERROR_NOT_IMPLEMENTED;
#else
nsresult rv;

// Open Jar file to get related attributes of target file.
Expand Down Expand Up @@ -3793,7 +3788,6 @@ ArrayBufferBuilder::mapToFileInPackage(const nsCString& aFile,
}
}
return NS_ERROR_FAILURE;
#endif
}

/* static */ bool
Expand Down
63 changes: 59 additions & 4 deletions js/src/gc/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,61 @@ GetPageFaultCount()
return pmc.PageFaultCount;
}

// On Windows the minimum size for a mapping is the allocation granularity
// (64KiB in practice), so mapping very small buffers is potentially wasteful.
void*
AllocateMappedContent(int fd, size_t offset, size_t length, size_t alignment)
{
// The allocation granularity must be a whole multiple of the alignment and
// the caller must request an aligned offset to satisfy Windows' and the
// caller's alignment requirements.
if (allocGranularity % alignment != 0 || offset % alignment != 0)
return nullptr;

// Make sure file exists and do sanity check for offset and size.
HANDLE hFile = reinterpret_cast<HANDLE>(intptr_t(fd));
MOZ_ASSERT(hFile != INVALID_HANDLE_VALUE);

uint32_t fSizeHgh;
uint32_t fSizeLow = GetFileSize(hFile, LPDWORD(&fSizeHgh));
if (fSizeLow == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
return nullptr;

uint64_t fSize = (uint64_t(fSizeHgh) << 32) + fSizeLow;
if (offset >= size_t(fSize) || length == 0 || length > fSize - offset)
return nullptr;

uint64_t mapSize = length + offset;
HANDLE hMap = CreateFileMapping(hFile, nullptr, PAGE_READONLY, mapSize >> 32, mapSize, nullptr);
if (!hMap)
return nullptr;

// MapViewOfFile requires the offset to be a whole multiple of the
// allocation granularity.
size_t alignOffset = offset - (offset % allocGranularity);
size_t alignLength = length + (offset % allocGranularity);
void* map = MapViewOfFile(hMap, FILE_MAP_COPY, 0, alignOffset, alignLength);
CloseHandle(hMap);
if (!map)
return nullptr;

return reinterpret_cast<void*>(uintptr_t(map) + (offset - alignOffset));
}

void
DeallocateMappedContent(void* p, size_t /*length*/)
{
if (!p)
return;

// Calculate the address originally returned by MapViewOfFile.
// This is required because AllocateMappedContent returns a pointer that
// might be offset into the view, necessitated by the requirement that the
// beginning of a view must be aligned with the allocation granularity.
uintptr_t map = uintptr_t(p) - (uintptr_t(p) % allocGranularity);
MOZ_ALWAYS_TRUE(UnmapViewOfFile(reinterpret_cast<void*>(map)));
}

# else // Various APIs are unavailable.

void*
Expand Down Expand Up @@ -328,22 +383,22 @@ GetPageFaultCount()
return 0;
}

# endif

void*
AllocateMappedContent(int fd, size_t offset, size_t length, size_t alignment)
{
// TODO: Bug 988813 - Support memory mapped array buffer for Windows platform.
// Not implemented.
return nullptr;
}

// Deallocate mapped memory for object.
void
DeallocateMappedContent(void* p, size_t length)
{
// TODO: Bug 988813 - Support memory mapped array buffer for Windows platform.
// Not implemented.
}

# endif

#elif defined(SOLARIS)

#ifndef MAP_NOSYNC
Expand Down
4 changes: 2 additions & 2 deletions modules/libpref/init/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5053,8 +5053,8 @@ pref("dom.voicemail.enabled", false);
// parameter omitted.
pref("dom.voicemail.defaultServiceId", 0);

// Disable mapped array buffer by default.
pref("dom.mapped_arraybuffer.enabled", false);
// Enable mapped array buffer by default.
pref("dom.mapped_arraybuffer.enabled", true);

// The tables used for Safebrowsing phishing and malware checks.
pref("urlclassifier.malwareTable", "goog-malware-shavar,goog-unwanted-shavar,test-malware-simple,test-unwanted-simple");
Expand Down

0 comments on commit 2400e06

Please sign in to comment.