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 (b6d82b1a6b02)

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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
/* -*- 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/. */

/*
 * Hierarchy of SpiderMonkey system memory allocators:
 *
 *   - System {m,c,re}alloc/new/free: Overridden by jemalloc in most
 *     environments. Do not use these functions directly.
 *
 *   - js_{m,c,re}alloc/new/free: Wraps the system allocators and adds a
 *     failure injection framework for use by the fuzzers as well as templated,
 *     typesafe variants. See js/public/Utility.h.
 *
 *   - AllocPolicy: An interface for the js allocators, for use with templates.
 *     These allocators are for system memory whose lifetime is not associated
 *     with a GC thing. See js/public/AllocPolicy.h.
 *
 *       - SystemAllocPolicy: No extra functionality over bare allocators.
 *
 *       - TempAllocPolicy: Adds automatic error reporting to the provided
 *         JSContext when allocations fail.
 *
 *       - ZoneAllocPolicy: Forwards to the Zone MallocProvider.
 *
 *   - MallocProvider. A mixin base class that handles automatically updating
 *     the GC's state in response to allocations that are tied to a GC lifetime
 *     or are for a particular GC purpose. These allocators must only be used
 *     for memory that will be freed when a GC thing is swept.
 *
 *       - gc::Zone:  Automatically triggers zone GC.
 *       - JSRuntime: Automatically triggers full GC.
 *       - JSContext: Dispatches directly to the runtime.
 */

#ifndef vm_MallocProvider_h
#define vm_MallocProvider_h

#include "mozilla/Attributes.h"
#include "mozilla/Likely.h"

#include "js/UniquePtr.h"
#include "js/Utility.h"

namespace js {

template <class Client>
struct MallocProvider {
  template <class T>
  T* maybe_pod_malloc(size_t numElems, arena_id_t arena = js::MallocArena) {
    T* p = js_pod_arena_malloc<T>(arena, numElems);
    if (MOZ_LIKELY(p)) {
      client()->updateMallocCounter(numElems * sizeof(T));
    }
    return p;
  }

  template <class T>
  T* maybe_pod_calloc(size_t numElems, arena_id_t arena = js::MallocArena) {
    T* p = js_pod_arena_calloc<T>(arena, numElems);
    if (MOZ_LIKELY(p)) {
      client()->updateMallocCounter(numElems * sizeof(T));
    }
    return p;
  }

  template <class T>
  T* maybe_pod_realloc(T* prior, size_t oldSize, size_t newSize,
                       arena_id_t arena = js::MallocArena) {
    T* p = js_pod_arena_realloc<T>(arena, prior, oldSize, newSize);
    if (MOZ_LIKELY(p)) {
      // For compatibility we do not account for realloc that decreases
      // previously allocated memory.
      if (newSize > oldSize) {
        client()->updateMallocCounter((newSize - oldSize) * sizeof(T));
      }
    }
    return p;
  }

  template <class T>
  T* pod_malloc() {
    return pod_malloc<T>(1);
  }

  template <class T>
  T* pod_malloc(size_t numElems, arena_id_t arena = js::MallocArena) {
    T* p = maybe_pod_malloc<T>(numElems, arena);
    if (MOZ_LIKELY(p)) {
      return p;
    }
    size_t bytes;
    if (MOZ_UNLIKELY(!CalculateAllocSize<T>(numElems, &bytes))) {
      client()->reportAllocationOverflow();
      return nullptr;
    }
    p = (T*)client()->onOutOfMemory(AllocFunction::Malloc, arena, bytes);
    if (p) {
      client()->updateMallocCounter(bytes);
    }
    return p;
  }

  template <class T, class U>
  T* pod_malloc_with_extra(size_t numExtra,
                           arena_id_t arena = js::MallocArena) {
    size_t bytes;
    if (MOZ_UNLIKELY((!CalculateAllocSizeWithExtra<T, U>(numExtra, &bytes)))) {
      client()->reportAllocationOverflow();
      return nullptr;
    }
    T* p = static_cast<T*>(js_arena_malloc(arena, bytes));
    if (MOZ_LIKELY(p)) {
      client()->updateMallocCounter(bytes);
      return p;
    }
    p = (T*)client()->onOutOfMemory(AllocFunction::Malloc, arena, bytes);
    if (p) {
      client()->updateMallocCounter(bytes);
    }
    return p;
  }

  template <class T>
  UniquePtr<T[], JS::FreePolicy> make_pod_array(
      size_t numElems, arena_id_t arena = js::MallocArena) {
    return UniquePtr<T[], JS::FreePolicy>(pod_malloc<T>(numElems, arena));
  }

  template <class T>
  T* pod_calloc(size_t numElems = 1, arena_id_t arena = js::MallocArena) {
    T* p = maybe_pod_calloc<T>(numElems, arena);
    if (MOZ_LIKELY(p)) {
      return p;
    }
    size_t bytes;
    if (MOZ_UNLIKELY(!CalculateAllocSize<T>(numElems, &bytes))) {
      client()->reportAllocationOverflow();
      return nullptr;
    }
    p = (T*)client()->onOutOfMemory(AllocFunction::Calloc, arena, bytes);
    if (p) {
      client()->updateMallocCounter(bytes);
    }
    return p;
  }

  template <class T, class U>
  T* pod_calloc_with_extra(size_t numExtra,
                           arena_id_t arena = js::MallocArena) {
    size_t bytes;
    if (MOZ_UNLIKELY((!CalculateAllocSizeWithExtra<T, U>(numExtra, &bytes)))) {
      client()->reportAllocationOverflow();
      return nullptr;
    }
    T* p = static_cast<T*>(js_arena_calloc(arena, bytes));
    if (p) {
      client()->updateMallocCounter(bytes);
      return p;
    }
    p = (T*)client()->onOutOfMemory(AllocFunction::Calloc, arena, bytes);
    if (p) {
      client()->updateMallocCounter(bytes);
    }
    return p;
  }

  template <class T>
  UniquePtr<T[], JS::FreePolicy> make_zeroed_pod_array(
      size_t numElems, arena_id_t arena = js::MallocArena) {
    return UniquePtr<T[], JS::FreePolicy>(pod_calloc<T>(numElems, arena));
  }

  template <class T>
  T* pod_realloc(T* prior, size_t oldSize, size_t newSize,
                 arena_id_t arena = js::MallocArena) {
    T* p = maybe_pod_realloc(prior, oldSize, newSize, arena);
    if (MOZ_LIKELY(p)) {
      return p;
    }
    size_t bytes;
    if (MOZ_UNLIKELY(!CalculateAllocSize<T>(newSize, &bytes))) {
      client()->reportAllocationOverflow();
      return nullptr;
    }
    p = (T*)client()->onOutOfMemory(AllocFunction::Realloc, arena, bytes,
                                    prior);
    if (p && newSize > oldSize) {
      client()->updateMallocCounter((newSize - oldSize) * sizeof(T));
    }
    return p;
  }

  JS_DECLARE_NEW_METHODS(new_, pod_malloc<uint8_t>, MOZ_ALWAYS_INLINE)
  JS_DECLARE_NEW_ARENA_METHODS(
      arena_new_,
      [this](arena_id_t arena, size_t size) {
        return pod_malloc<uint8_t>(size, arena);
      },
      MOZ_ALWAYS_INLINE)

  JS_DECLARE_MAKE_METHODS(make_unique, new_, MOZ_ALWAYS_INLINE)
  JS_DECLARE_MAKE_METHODS(arena_make_unique, arena_new_, MOZ_ALWAYS_INLINE)

 private:
  Client* client() { return static_cast<Client*>(this); }

  // The Default implementation is a no-op which can be overridden by the
  // client.
  void updateMallocCounter(size_t nbytes) {}
};

} /* namespace js */

#endif /* vm_MallocProvider_h */