blob: 389bb48668a142c72b5a276c402c5d9263ff6c1d [file] [log] [blame]
//===-- sanitizer_stack_store.h ---------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef SANITIZER_STACK_STORE_H
#define SANITIZER_STACK_STORE_H
#include "sanitizer_atomic.h"
#include "sanitizer_common.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_mutex.h"
#include "sanitizer_stacktrace.h"
namespace __sanitizer {
class StackStore {
static constexpr uptr kBlockSizeFrames = 0x100000;
static constexpr uptr kBlockCount = 0x1000;
static constexpr uptr kBlockSizeBytes = kBlockSizeFrames * sizeof(uptr);
public:
constexpr StackStore() = default;
using Id = uptr;
Id Store(const StackTrace &trace);
StackTrace Load(Id id) const;
uptr Allocated() const;
void TestOnlyUnmap();
private:
static constexpr uptr GetBlockIdx(uptr frame_idx) {
return frame_idx / kBlockSizeFrames;
}
static constexpr uptr GetInBlockIdx(uptr frame_idx) {
return frame_idx % kBlockSizeFrames;
}
uptr *Alloc(uptr count);
// Total number of allocated frames.
atomic_uintptr_t total_frames_ = {};
// Each block will hold pointer to exactly kBlockSizeFrames.
class BlockInfo {
atomic_uintptr_t data_;
StaticSpinMutex mtx_; // Protects alloc of new blocks.
uptr *Create();
uptr *Get() const;
public:
uptr *GetOrCreate();
void TestOnlyUnmap();
};
BlockInfo blocks_[kBlockCount] = {};
};
} // namespace __sanitizer
#endif // SANITIZER_STACK_STORE_H