#ifndef PARTITION_ALLOC_POINTERS_INSTANCE_TRACER_H_
#define PARTITION_ALLOC_POINTERS_INSTANCE_TRACER_H_
#include <array>
#include <atomic>
#include <cstdint>
#include <utility>
#include <vector>
#include "partition_alloc/buildflags.h"
#include "partition_alloc/partition_alloc_base/compiler_specific.h"
#include "partition_alloc/partition_alloc_base/component_export.h"
#include "partition_alloc/partition_alloc_base/cxx20_is_constant_evaluated.h"
namespace base::internal {
#if !PA_BUILDFLAG(ENABLE_BACKUP_REF_PTR_INSTANCE_TRACER)
class InstanceTracer { … };
#else
class PA_TRIVIAL_ABI InstanceTracer {
public:
constexpr InstanceTracer() : owner_id_(CreateOwnerId()) {}
constexpr InstanceTracer(const InstanceTracer&) : InstanceTracer() {}
constexpr InstanceTracer& operator=(const InstanceTracer&) { return *this; }
constexpr InstanceTracer(InstanceTracer&&) : InstanceTracer() {}
constexpr InstanceTracer& operator=(InstanceTracer&&) { return *this; }
constexpr uint64_t owner_id() const { return owner_id_; }
constexpr static void Trace(uint64_t owner_id,
bool may_dangle,
uintptr_t address) {
if (partition_alloc::internal::base::is_constant_evaluated() ||
owner_id == 0) {
return;
}
TraceImpl(owner_id, may_dangle, address);
}
constexpr static void Untrace(uint64_t owner_id) {
if (partition_alloc::internal::base::is_constant_evaluated() ||
owner_id == 0) {
return;
}
UntraceImpl(owner_id);
}
PA_COMPONENT_EXPORT(RAW_PTR)
static std::vector<std::array<const void*, 32>> GetStackTracesForDanglingRefs(
uintptr_t allocation);
PA_COMPONENT_EXPORT(RAW_PTR)
static std::vector<std::array<const void*, 32>>
GetStackTracesForAddressForTest(const void* address);
private:
PA_COMPONENT_EXPORT(RAW_PTR)
static void TraceImpl(uint64_t owner_id, bool may_dangle, uintptr_t address);
PA_COMPONENT_EXPORT(RAW_PTR) static void UntraceImpl(uint64_t owner_id);
constexpr uint64_t CreateOwnerId() {
if (partition_alloc::internal::base::is_constant_evaluated()) {
return 0;
}
return ++counter_;
}
PA_COMPONENT_EXPORT(RAW_PTR) static std::atomic<uint64_t> counter_;
uint64_t owner_id_ = 0;
};
#endif
}
#endif