tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ProfilingStack.cpp (1613B)


      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/. */
      6 
      7 #include "js/ProfilingStack.h"
      8 
      9 #include "mozilla/IntegerRange.h"
     10 #include "mozilla/MathAlgorithms.h"
     11 
     12 #include <algorithm>
     13 
     14 using namespace js;
     15 
     16 ProfilingStack::~ProfilingStack() {
     17  // The label macros keep a reference to the ProfilingStack to avoid a TLS
     18  // access. If these are somehow not all cleared we will get a
     19  // use-after-free so better to crash now.
     20  MOZ_RELEASE_ASSERT(stackPointer == 0);
     21 
     22  delete[] frames;
     23 }
     24 
     25 void ProfilingStack::ensureCapacitySlow() {
     26  MOZ_ASSERT(stackPointer >= capacity);
     27  const uint32_t kInitialCapacity = 4096 / sizeof(ProfilingStackFrame);
     28 
     29  uint32_t sp = stackPointer;
     30 
     31  uint32_t newCapacity;
     32  if (!capacity) {
     33    newCapacity = kInitialCapacity;
     34  } else {
     35    size_t memoryGoal =
     36        mozilla::RoundUpPow2(capacity * 2 * sizeof(ProfilingStackFrame));
     37    newCapacity = memoryGoal / sizeof(ProfilingStackFrame);
     38  }
     39  newCapacity = std::max(sp + 1, newCapacity);
     40 
     41  auto* newFrames = new js::ProfilingStackFrame[newCapacity];
     42 
     43  // It's important that `frames` / `capacity` / `stackPointer` remain
     44  // consistent here at all times.
     45  for (auto i : mozilla::IntegerRange(capacity)) {
     46    newFrames[i] = frames[i];
     47  }
     48 
     49  js::ProfilingStackFrame* oldFrames = frames;
     50  frames = newFrames;
     51  capacity = newCapacity;
     52  delete[] oldFrames;
     53 }