DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (fddffdeab170)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/* 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/. */

#ifdef MOZ_JEMALLOC4

#define MOZ_JEMALLOC_IMPL

/* mozmemory_wrap.h needs to be included before MFBT headers */
#include "mozmemory_wrap.h"
#include <mozilla/Assertions.h>
#include "mozilla/Types.h"

#if defined(MOZ_NATIVE_JEMALLOC)
#include MALLOC_H
#else
#define DLLEXPORT
#include "jemalloc/jemalloc.h"
#endif

#ifdef XP_WIN
#include <windows.h>
#endif
#ifdef XP_DARWIN
#include <sys/mman.h>
#endif

/* Override some jemalloc defaults */
#ifdef DEBUG
#define MOZ_MALLOC_BUILD_OPTIONS ",junk:true"
#else
#define MOZ_MALLOC_BUILD_OPTIONS ",junk:free"
#endif

#define MOZ_MALLOC_OPTIONS "narenas:1,tcache:false"
MFBT_DATA const char* je_(malloc_conf) =
  MOZ_MALLOC_OPTIONS MOZ_MALLOC_BUILD_OPTIONS;

#ifdef ANDROID
#include <android/log.h>

static void
_je_malloc_message(void* cbopaque, const char* s)
{
  __android_log_print(ANDROID_LOG_INFO, "GeckoJemalloc", "%s", s);
}

void (*je_(malloc_message))(void*, const char* s) = _je_malloc_message;
#endif

/* Jemalloc supports hooks that are called on chunk
 * allocate/deallocate/commit/decommit/purge/etc.
 *
 * We currently only hook commit, decommit and purge. We do this to tweak
 * the way chunks are handled so that RSS stays lower than it normally
 * would with the default jemalloc uses.
 * This somewhat matches the behavior of mozjemalloc, except it doesn't
 * rely on a double purge on mac, instead purging directly. (Yes, this
 * means we can get rid of jemalloc_purge_freed_pages at some point)
 *
 * The default for jemalloc is to do the following:
 * - commit, decommit: nothing
 * - purge: MEM_RESET on Windows, MADV_FREE on Mac/BSD, MADV_DONTNEED on Linux
 *
 * The hooks we setup do the following:
 * on Windows:
 * - commit: MEM_COMMIT
 * - decommit: MEM_DECOMMIT
 * on Mac:
 * - purge: mmap new anonymous memory on top of the chunk
 *
 * We only set the above hooks, others are left with the default.
 */
#if defined(XP_WIN) || defined(XP_DARWIN)
class JemallocInit {
public:
  JemallocInit()
  {
    chunk_hooks_t hooks;
    size_t hooks_len;
    unsigned narenas;
    size_t mib[3];
    size_t size;

    size = sizeof(narenas);
    je_(mallctl)("arenas.narenas", &narenas, &size, nullptr, 0);

    size = sizeof(mib) / sizeof(mib[0]);
    je_(mallctlnametomib)("arena.0.chunk_hooks", mib, &size);

    /* Set the hooks on all the existing arenas. */
    for (unsigned arena = 0; arena < narenas; arena++) {
      mib[1] = arena;
      hooks_len = sizeof(hooks);
      je_(mallctlbymib)(mib, size, &hooks, &hooks_len, nullptr, 0);

#ifdef XP_WIN
      hooks.commit = CommitHook;
      hooks.decommit = DecommitHook;
#endif
#ifdef XP_DARWIN
      hooks.purge = PurgeHook;
#endif

      je_(mallctlbymib)(mib, size, nullptr, nullptr, &hooks, hooks_len);
    }
  }

private:
#ifdef XP_WIN
  static bool
  CommitHook(void* chunk, size_t size, size_t offset, size_t length,
             unsigned arena_ind)
  {
    void* addr = reinterpret_cast<void*>(
      reinterpret_cast<uintptr_t>(chunk) + static_cast<uintptr_t>(offset));

    if (!VirtualAlloc(addr, length, MEM_COMMIT, PAGE_READWRITE))
      return true;

    return false;
  }

  static bool
  DecommitHook(void* chunk, size_t size, size_t offset, size_t length,
               unsigned arena_ind)
  {
    void* addr = reinterpret_cast<void*>(
      reinterpret_cast<uintptr_t>(chunk) + static_cast<uintptr_t>(offset));

    if (!VirtualFree(addr, length, MEM_DECOMMIT))
      MOZ_CRASH();

    return false;
  }
#endif

#ifdef XP_DARWIN
  static bool
  PurgeHook(void* chunk, size_t size, size_t offset, size_t length,
            unsigned arena_ind)
  {
    void* addr = reinterpret_cast<void*>(
      reinterpret_cast<uintptr_t>(chunk) + static_cast<uintptr_t>(offset));

    void* new_addr = mmap(addr, length, PROT_READ | PROT_WRITE,
                          MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
    return (new_addr != addr);
  }
#endif
};

/* For the static constructor from the class above */
JemallocInit gJemallocInit;
#endif

#else
#include <mozilla/Assertions.h>
#endif /* MOZ_JEMALLOC4 */

/* Provide an abort function for use in jemalloc code */
extern "C" void moz_abort() {
  MOZ_CRASH();
}