1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_PROFILER_STACK_BUFFER_H_
6 #define BASE_PROFILER_STACK_BUFFER_H_
12 #include "base/base_export.h"
13 #include "base/memory/aligned_memory.h"
14 #include "build/build_config.h"
18 // This class contains a buffer for stack copies that can be shared across
19 // multiple instances of StackSampler.
20 class BASE_EXPORT StackBuffer {
22 // The expected alignment of the stack on the current platform. Windows and
23 // System V AMD64 ABIs on x86, x64, and ARM require the stack to be aligned
24 // to twice the pointer size. Excepted from this requirement is code setting
25 // up the stack during function calls (between pushing the return address
26 // and the end of the function prologue). The profiler will sometimes
27 // encounter this exceptional case for leaf frames.
28 static constexpr size_t kPlatformStackAlignment = 2 * sizeof(uintptr_t);
30 explicit StackBuffer(size_t buffer_size);
32 StackBuffer(const StackBuffer&) = delete;
33 StackBuffer& operator=(const StackBuffer&) = delete;
37 // Returns a kPlatformStackAlignment-aligned pointer to the stack buffer.
38 uintptr_t* buffer() const {
39 // Aligned during allocation.
44 size_t size() const { return size_; }
46 #if BUILDFLAG(IS_CHROMEOS)
47 // Tell the kernel that we no longer need the data currently in the upper
48 // parts of the buffer and that the kernel may discard it to free up space.
49 // Specifically, the bytes from |(uint8_t*)buffer + retained_bytes| to the
50 // end of the buffer may be discarded, while the bytes from |buffer| to
51 // |(uint8_t*)buffer + retained_bytes - 1| will not be affected.
52 // The program can still write to that part of the buffer, but should not read
53 // from that part of buffer until after the next write. (The contents of that
54 // part of the buffer are undefined.)
55 // After calling this function, there may be a page fault on the next write to
56 // that area, so it should only be called when parts of the buffer were
57 // written to and will probably not be written to again soon.
58 void MarkUpperBufferContentsAsUnneeded(size_t retained_bytes);
62 // The size in bytes of the requested buffer allocation.
65 // The buffer to store the stack. Already aligned.
66 const std::unique_ptr<uintptr_t, AlignedFreeDeleter> buffer_;
71 #endif // BASE_PROFILER_STACK_BUFFER_H_