Commit ebd6ab40 authored by Doug Thayer's avatar Doug Thayer
Browse files

Bug 1538279 - Extract nsZipArchive's madvise and similar to a helper r=glandium,froydnj

This is just to make it simpler to use PrefetchVirtualMemory in subsequent patches.

Differential Revision: https://phabricator.services.mozilla.com/D26016

--HG--
extra : moz-landing-system : lando
parent 28d488a2
Loading
Loading
Loading
Loading
+3 −26
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include "plstr.h"
#include "mozilla/Attributes.h"
#include "mozilla/Logging.h"
#include "mozilla/MemUtils.h"
#include "mozilla/UniquePtrExtensions.h"
#include "stdlib.h"
#include "nsDirectoryService.h"
@@ -650,32 +651,8 @@ nsresult nsZipArchive::BuildFileList(PRFileDesc *aFd) {
      xtolong(startp + centralOffset) == CENTRALSIG) {
    // Success means optimized jar layout from bug 559961 is in effect
    uint32_t readaheadLength = xtolong(startp);
    if (readaheadLength) {
#if defined(XP_SOLARIS)
      posix_madvise(const_cast<uint8_t *>(startp), readaheadLength,
                    POSIX_MADV_WILLNEED);
#elif defined(XP_UNIX)
      madvise(const_cast<uint8_t *>(startp), readaheadLength, MADV_WILLNEED);
#elif defined(XP_WIN)
      static auto prefetchVirtualMemory =
          reinterpret_cast<BOOL(WINAPI *)(HANDLE, ULONG_PTR, PVOID, ULONG)>(
              GetProcAddress(GetModuleHandle(L"kernel32.dll"),
                             "PrefetchVirtualMemory"));
      if (prefetchVirtualMemory) {
        // Normally, we'd use WIN32_MEMORY_RANGE_ENTRY, but that requires
        // a different _WIN32_WINNT value before including windows.h, but
        // that causes complications with unified sources. It's a simple
        // enough struct anyways.
        struct {
          PVOID VirtualAddress;
          SIZE_T NumberOfBytes;
        } entry;
        entry.VirtualAddress = const_cast<uint8_t *>(startp);
        entry.NumberOfBytes = readaheadLength;
        prefetchVirtualMemory(GetCurrentProcess(), 1, &entry, 0);
      }
#endif
    }
    mozilla::MaybePrefetchMemory(const_cast<uint8_t *>(startp),
                                 readaheadLength);
  } else {
    for (buf = endp - ZIPEND_SIZE; buf > startp; buf--) {
      if (xtolong(buf) == ENDSIG) {
+50 −0
Original line number Diff line number Diff line
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/MemUtils.h"

#if defined(XP_WIN)
#  include <windows.h>
#else
#  include <sys/mman.h>
#endif

bool mozilla::MaybePrefetchMemory(uint8_t* aStart, size_t aNumBytes) {
  if (aNumBytes == 0) {
    return true;
  }

#if defined(XP_SOLARIS)
  posix_madvise(aStart, aNumBytes, POSIX_MADV_WILLNEED);
  return true;
#elif defined(XP_UNIX)
  madvise(aStart, aNumBytes, MADV_WILLNEED);
  return true;
#elif defined(XP_WIN)
  static auto prefetchVirtualMemory =
      reinterpret_cast<BOOL(WINAPI*)(HANDLE, ULONG_PTR, PVOID, ULONG)>(
          GetProcAddress(GetModuleHandleW(L"kernel32.dll"),
                         "PrefetchVirtualMemory"));

  if (prefetchVirtualMemory) {
    // Normally, we'd use WIN32_MEMORY_RANGE_ENTRY, but that requires
    // a different _WIN32_WINNT value before including windows.h, but
    // that causes complications with unified sources. It's a simple
    // enough struct anyways.
    struct {
      PVOID VirtualAddress;
      SIZE_T NumberOfBytes;
    } entry;
    entry.VirtualAddress = aStart;
    entry.NumberOfBytes = aNumBytes;
    prefetchVirtualMemory(GetCurrentProcess(), 1, &entry, 0);
    return true;
  }
  return false;
#else
  return false;
#endif
}

xpcom/glue/MemUtils.h

0 → 100644
+19 −0
Original line number Diff line number Diff line
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_MemUtils_h
#define mozilla_MemUtils_h

#include <stddef.h>
#include <stdint.h>

namespace mozilla {

bool MaybePrefetchMemory(uint8_t* aStart, size_t aNumBytes);

}  // namespace mozilla

#endif
+1 −0
Original line number Diff line number Diff line
@@ -8,4 +8,5 @@ DIRS += ['standalone']

EXPORTS.mozilla += [
    'FileUtils.h',
    'MemUtils.h',
]
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@

xpcom_glue_src_lcppsrcs = [
    'FileUtils.cpp',
    'MemUtils.cpp',
    'XREAppData.cpp',
]