mirror of
https://github.com/wolfpld/tracy.git
synced 2024-11-22 06:34:36 +00:00
Fix segfault in Python bindings for TRACE_ENABLE=OFF case
This commit is contained in:
parent
adbee2f820
commit
93be1d9343
@ -9,12 +9,12 @@ namespace py = pybind11;
|
||||
using OptionalString = std::optional<std::string>;
|
||||
using OptionalInt = std::optional<int>;
|
||||
|
||||
#ifdef TRACY_ENABLE
|
||||
template <typename Type = uint64_t>
|
||||
OptionalNumber MemoryAllocate(const Type &type, std::size_t size,
|
||||
const OptionalString &name = std::nullopt,
|
||||
const OptionalNumber &id = std::nullopt,
|
||||
OptionalInt depth = std::nullopt) {
|
||||
#ifdef TRACY_ENABLE
|
||||
if (!name && !id) {
|
||||
if (!depth)
|
||||
TracyAlloc(reinterpret_cast<void *>(type), size);
|
||||
@ -38,27 +38,11 @@ OptionalNumber MemoryAllocate(const Type &type, std::size_t size,
|
||||
else
|
||||
TracyAllocNS(reinterpret_cast<void *>(type), size, *depth, entry.second);
|
||||
return entry.first;
|
||||
#else
|
||||
static_cast<void>(type); // unused
|
||||
static_cast<void>(size); // unused
|
||||
static_cast<void>(name); // unused
|
||||
static_cast<void>(id); // unused
|
||||
static_cast<void>(depth); // unused
|
||||
#endif
|
||||
}
|
||||
|
||||
template <>
|
||||
OptionalNumber MemoryAllocate(const py::object &object, std::size_t size,
|
||||
const OptionalString &name,
|
||||
const OptionalNumber &id, OptionalInt depth) {
|
||||
return MemoryAllocate<uint64_t>(reinterpret_cast<uint64_t>(object.ptr()),
|
||||
size, name, id, depth);
|
||||
}
|
||||
|
||||
template <typename Type = uint64_t>
|
||||
bool MemoryFree(const Type &type, const OptionalNumber &id = std::nullopt,
|
||||
OptionalInt depth = std::nullopt) {
|
||||
#ifdef TRACY_ENABLE
|
||||
if (!id) {
|
||||
if (!depth)
|
||||
TracyFree(reinterpret_cast<void *>(type));
|
||||
@ -75,11 +59,27 @@ bool MemoryFree(const Type &type, const OptionalNumber &id = std::nullopt,
|
||||
else
|
||||
TracyFreeNS(reinterpret_cast<void *>(type), *depth, ptr);
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
static_cast<void>(type); // unused
|
||||
static_cast<void>(id); // unused
|
||||
static_cast<void>(depth); // unused
|
||||
|
||||
template <typename Type = uint64_t>
|
||||
OptionalNumber MemoryAllocate(const Type &, std::size_t, const OptionalString &,
|
||||
const OptionalNumber &, OptionalInt) {
|
||||
return 0ul;
|
||||
}
|
||||
|
||||
template <typename Type = uint64_t>
|
||||
bool MemoryFree(const Type &, const OptionalNumber &, OptionalInt) {
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
template <>
|
||||
OptionalNumber MemoryAllocate(const py::object &object, std::size_t size,
|
||||
const OptionalString &name,
|
||||
const OptionalNumber &id, OptionalInt depth) {
|
||||
return MemoryAllocate<uint64_t>(reinterpret_cast<uint64_t>(object.ptr()),
|
||||
size, name, id, depth);
|
||||
}
|
||||
|
||||
template <>
|
||||
|
@ -703,7 +703,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
m.def(
|
||||
"program_name",
|
||||
[](const std::string &name) {
|
||||
if (!tracy::IsEnabled()) return false;
|
||||
if (!tracy::IsEnabled()) return true;
|
||||
auto entry = NameBuffer::Add(name);
|
||||
if (!entry.first) return false;
|
||||
TracySetProgramName(entry.second);
|
||||
@ -722,7 +722,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
m.def(
|
||||
"app_info",
|
||||
[](const std::string &text) {
|
||||
if (!tracy::IsEnabled()) return false;
|
||||
if (!tracy::IsEnabled()) return true;
|
||||
if (text.size() >= std::numeric_limits<uint16_t>::max()) return false;
|
||||
TracyAppInfo(text.c_str(), text.size());
|
||||
return true;
|
||||
@ -732,7 +732,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
m.def(
|
||||
"message",
|
||||
[](const std::string &message) {
|
||||
if (!tracy::IsEnabled()) return false;
|
||||
if (!tracy::IsEnabled()) return true;
|
||||
if (message.size() >= std::numeric_limits<uint16_t>::max())
|
||||
return false;
|
||||
TracyMessage(message.c_str(), message.size());
|
||||
@ -743,7 +743,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
m.def(
|
||||
"message",
|
||||
[](const std::string &message, uint32_t pColor) {
|
||||
if (!tracy::IsEnabled()) return false;
|
||||
if (!tracy::IsEnabled()) return true;
|
||||
if (message.size() >= std::numeric_limits<uint16_t>::max())
|
||||
return false;
|
||||
TracyMessageC(message.c_str(), message.size(), pColor);
|
||||
@ -756,8 +756,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
m.def(
|
||||
"frame_mark_start",
|
||||
[](const std::string &name) {
|
||||
if (!tracy::IsEnabled())
|
||||
return static_cast<OptionalNumber>(std::nullopt);
|
||||
if (!tracy::IsEnabled()) return static_cast<OptionalNumber>(0ul);
|
||||
auto entry = NameBuffer::Add(name);
|
||||
if (!entry.first) return static_cast<OptionalNumber>(std::nullopt);
|
||||
FrameMarkStart(entry.second);
|
||||
@ -768,7 +767,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
m.def(
|
||||
"frame_mark_end",
|
||||
[](std::size_t id) {
|
||||
if (!tracy::IsEnabled()) return false;
|
||||
if (!tracy::IsEnabled()) return true;
|
||||
auto ptr = NameBuffer::Get(id);
|
||||
if (!ptr) return false;
|
||||
FrameMarkEnd(ptr);
|
||||
@ -780,7 +779,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
"frame_image",
|
||||
[](const py::bytes &image, uint16_t width, uint16_t height,
|
||||
uint8_t offset = 0, bool flip = false) {
|
||||
if (!tracy::IsEnabled()) return false;
|
||||
if (!tracy::IsEnabled()) return true;
|
||||
if (width % 4 != 0 || height % 4 != 0) return false;
|
||||
TracyCFrameImage(std::string(image).data(), width, height, offset,
|
||||
flip);
|
||||
@ -823,8 +822,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
"_plot_config",
|
||||
[](const std::string &name, int type, bool step, bool fill,
|
||||
uint32_t color = 0) {
|
||||
if (!tracy::IsEnabled())
|
||||
return static_cast<OptionalNumber>(std::nullopt);
|
||||
if (!tracy::IsEnabled()) return static_cast<OptionalNumber>(0ul);
|
||||
auto entry = NameBuffer::Add(name);
|
||||
if (!entry.first) return static_cast<OptionalNumber>(std::nullopt);
|
||||
TracyCPlotConfig(entry.second, type, step, fill, color);
|
||||
@ -842,7 +840,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
m.def(
|
||||
"plot",
|
||||
[](std::size_t id, double value) {
|
||||
if (!tracy::IsEnabled()) return false;
|
||||
if (!tracy::IsEnabled()) return true;
|
||||
auto ptr = NameBuffer::Get(id);
|
||||
if (!ptr) return false;
|
||||
TracyCPlot(ptr, value);
|
||||
@ -852,7 +850,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
m.def(
|
||||
"plot",
|
||||
[](std::size_t id, float value) {
|
||||
if (!tracy::IsEnabled()) return false;
|
||||
if (!tracy::IsEnabled()) return true;
|
||||
auto ptr = NameBuffer::Get(id);
|
||||
if (!ptr) return false;
|
||||
TracyCPlotF(ptr, value);
|
||||
@ -862,7 +860,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
|
||||
m.def(
|
||||
"plot",
|
||||
[](std::size_t id, int64_t value) {
|
||||
if (!tracy::IsEnabled()) return false;
|
||||
if (!tracy::IsEnabled()) return true;
|
||||
auto ptr = NameBuffer::Get(id);
|
||||
if (!ptr) return false;
|
||||
TracyCPlotI(ptr, value);
|
||||
|
@ -23,7 +23,6 @@ bool SetText(const std::string& text, tracy::ScopedZone* zone) {
|
||||
zone->Text(text.c_str(), text.size());
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
class PyScopedZone {
|
||||
public:
|
||||
@ -45,47 +44,30 @@ class PyScopedZone {
|
||||
virtual ~PyScopedZone() { Exit(); };
|
||||
|
||||
bool IsActive() const {
|
||||
#ifdef TRACY_ENABLE
|
||||
if (!m_zone) return m_active;
|
||||
return m_zone->IsActive();
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
bool Text(const Type& text) {
|
||||
#ifdef TRACY_ENABLE
|
||||
return SetText(text, m_zone);
|
||||
#else
|
||||
static_cast<void>(text); // unused
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Name(const std::string& name) {
|
||||
#ifdef TRACY_ENABLE
|
||||
if (name.size() >= std::numeric_limits<uint16_t>::max()) return false;
|
||||
m_name = name;
|
||||
if (!m_zone) return true;
|
||||
m_zone->Name(m_name->c_str(), m_name->size());
|
||||
return true;
|
||||
#else
|
||||
static_cast<void>(name); // unused
|
||||
#endif
|
||||
}
|
||||
|
||||
void Color(uint32_t color) {
|
||||
#ifdef TRACY_ENABLE
|
||||
m_color = color;
|
||||
if (!m_zone) return;
|
||||
m_zone->Color(m_color);
|
||||
#else
|
||||
static_cast<void>(color); // unused
|
||||
#endif
|
||||
}
|
||||
|
||||
void Enter() {
|
||||
#ifdef TRACY_ENABLE
|
||||
if (m_depth)
|
||||
m_zone = new tracy::ScopedZone(
|
||||
m_line, m_source.c_str(), m_source.size(), m_function.c_str(),
|
||||
@ -96,14 +78,11 @@ class PyScopedZone {
|
||||
m_line, m_source.c_str(), m_source.size(), m_function.c_str(),
|
||||
m_function.size(), m_name ? m_name->c_str() : nullptr,
|
||||
m_name ? m_name->size() : 0ul, m_color, m_active);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Exit() {
|
||||
#ifdef TRACY_ENABLE
|
||||
if (m_zone) delete m_zone;
|
||||
m_zone = nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
@ -116,7 +95,26 @@ class PyScopedZone {
|
||||
std::string m_source;
|
||||
uint32_t m_line;
|
||||
|
||||
#ifdef TRACY_ENABLE
|
||||
tracy::ScopedZone* m_zone;
|
||||
#endif
|
||||
};
|
||||
#else
|
||||
|
||||
class PyScopedZone {
|
||||
public:
|
||||
PyScopedZone(const std::optional<std::string>&, uint32_t, std::optional<int>,
|
||||
bool, const std::string&, const std::string&, uint32_t line) {}
|
||||
virtual ~PyScopedZone(){};
|
||||
|
||||
bool IsActive() const { return false; }
|
||||
|
||||
template <typename Type>
|
||||
bool Text(const Type&) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Name(const std::string&) { return true; }
|
||||
void Color(uint32_t) {}
|
||||
void Enter() {}
|
||||
void Exit() {}
|
||||
};
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user