From ea8a7b0f93aa3b391e92d7b930f667d1ff439d04 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 17 Jan 2024 17:31:45 +1100 Subject: [PATCH] allocate lua properties in spi ram --- src/lua/include/property.hpp | 4 ++-- src/lua/property.cpp | 13 +++++++++---- src/memory/include/memory_resource.hpp | 5 +++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/lua/include/property.hpp b/src/lua/include/property.hpp index 425cc15c..03229bc1 100644 --- a/src/lua/include/property.hpp +++ b/src/lua/include/property.hpp @@ -35,7 +35,7 @@ class Property { Property(const LuaValue&); Property(const LuaValue&, std::function); - auto Get() -> const LuaValue& { return value_; } + auto Get() -> const LuaValue& { return *value_; } auto IsTwoWay() -> bool { return cb_.has_value(); } @@ -46,7 +46,7 @@ class Property { auto AddLuaBinding(lua_State*, int ref) -> void; private: - LuaValue value_; + std::unique_ptr value_; std::optional> cb_; std::pmr::vector> bindings_; }; diff --git a/src/lua/property.cpp b/src/lua/property.cpp index 0c2e041b..c16434fa 100644 --- a/src/lua/property.cpp +++ b/src/lua/property.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -160,11 +161,15 @@ template inline constexpr bool always_false_v = false; Property::Property(const LuaValue& val) - : value_(val), cb_(), bindings_(&memory::kSpiRamResource) {} + : value_(memory::SpiRamAllocator().new_object(val)), + cb_(), + bindings_(&memory::kSpiRamResource) {} Property::Property(const LuaValue& val, std::function cb) - : value_(val), cb_(cb), bindings_(&memory::kSpiRamResource) {} + : value_(memory::SpiRamAllocator().new_object(val)), + cb_(cb), + bindings_(&memory::kSpiRamResource) {} static auto pushTagValue(lua_State* L, const database::TagValue& val) -> void { std::visit( @@ -273,7 +278,7 @@ auto Property::PushValue(lua_State& s) -> int { static_assert(always_false_v, "PushValue missing type"); } }, - value_); + *value_); return 1; } @@ -343,7 +348,7 @@ auto Property::PopValue(lua_State& s) -> bool { } auto Property::Update(const LuaValue& v) -> void { - value_ = v; + *value_ = v; for (int i = bindings_.size() - 1; i >= 0; i--) { auto& b = bindings_[i]; diff --git a/src/memory/include/memory_resource.hpp b/src/memory/include/memory_resource.hpp index ed4b1a1c..e41a1caf 100644 --- a/src/memory/include/memory_resource.hpp +++ b/src/memory/include/memory_resource.hpp @@ -40,4 +40,9 @@ class Resource : public std::pmr::memory_resource { extern Resource kSpiRamResource; +template +auto SpiRamAllocator() { + return std::pmr::polymorphic_allocator{&kSpiRamResource}; +} + } // namespace memory