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