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