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