From a5f7735d9a355fcc52007fb4ec1daaf7b3e25c2d Mon Sep 17 00:00:00 2001 From: modeco80 Date: Wed, 2 Aug 2023 05:30:18 -0400 Subject: [PATCH] lucore: add Unique and Ref types These alias the standard library unique_ptr and shared_ptr riscv: Use Unique in many places Uses lucore::Unique. This should hopefully help memory management somewhat. lcpu: Use Unique riscv::System::Create now creates a Unique so does lucore::Library::OpenExisting --- native/projects/lcpu/src/LuaCpu.cpp | 4 --- native/projects/lcpu/src/LuaCpu.hpp | 3 +- native/projects/lcpu/src/LuaDevice.hpp | 1 - native/projects/lcpu/src/SourceSink.cpp | 5 +-- .../lucore/include/lucore/Library.hpp | 6 ++-- .../projects/lucore/include/lucore/Types.hpp | 11 +++++-- native/projects/lucore/src/Library.cpp | 4 +-- native/projects/riscv/include/riscv/Bus.hpp | 1 + .../riscv/include/riscv/Devices/RamDevice.hpp | 1 + .../projects/riscv/include/riscv/System.hpp | 18 +++++----- native/projects/riscv/src/System.cpp | 33 +++++++------------ test-gmod/project.json | 3 +- 12 files changed, 42 insertions(+), 48 deletions(-) diff --git a/native/projects/lcpu/src/LuaCpu.cpp b/native/projects/lcpu/src/LuaCpu.cpp index 3c05956..d5ed356 100644 --- a/native/projects/lcpu/src/LuaCpu.cpp +++ b/native/projects/lcpu/src/LuaCpu.cpp @@ -83,8 +83,4 @@ namespace lcpu { } } - LuaCpu::~LuaCpu() { - delete system; - } - } // namespace lcpu diff --git a/native/projects/lcpu/src/LuaCpu.hpp b/native/projects/lcpu/src/LuaCpu.hpp index 2023d45..c8fcb3d 100644 --- a/native/projects/lcpu/src/LuaCpu.hpp +++ b/native/projects/lcpu/src/LuaCpu.hpp @@ -14,7 +14,6 @@ namespace lcpu { protected: friend struct lua::LuaObject; LuaCpu(u32 memorySize); - ~LuaCpu(); private: LUA_CLASS_FUNCTION_DECL(PoweredOn); // Check if the CPU is powered on @@ -25,7 +24,7 @@ namespace lcpu { LUA_CLASS_FUNCTION_DECL(AttachDevice); // attach a LuaDevice to this cpu // member variables - riscv::System* system; + lucore::Unique system; bool poweredOn; }; diff --git a/native/projects/lcpu/src/LuaDevice.hpp b/native/projects/lcpu/src/LuaDevice.hpp index 2466155..248a536 100644 --- a/native/projects/lcpu/src/LuaDevice.hpp +++ b/native/projects/lcpu/src/LuaDevice.hpp @@ -26,7 +26,6 @@ namespace lcpu { protected: friend lcpu::lua::LuaObject; LuaDevice(riscv::Address base, riscv::Address size); - ~LuaDevice() = default; void AfterLuaInit() override; diff --git a/native/projects/lcpu/src/SourceSink.cpp b/native/projects/lcpu/src/SourceSink.cpp index 50c53ed..235cce5 100644 --- a/native/projects/lcpu/src/SourceSink.cpp +++ b/native/projects/lcpu/src/SourceSink.cpp @@ -5,7 +5,7 @@ #include namespace tier0 { - lucore::Library* library = nullptr; + lucore::Unique library = nullptr; using Msg_t = void (*)(const char*, ...); Msg_t Msg {}; @@ -65,7 +65,8 @@ namespace lcpu { } SourceSink::~SourceSink() { - delete tier0::library; + // Unique will do the right thing for us anyways but /shrug + tier0::library.reset(); } void SourceSink::OutputMessage(const lucore::Logger::MessageData& data) { diff --git a/native/projects/lucore/include/lucore/Library.hpp b/native/projects/lucore/include/lucore/Library.hpp index 7378d31..d06384f 100644 --- a/native/projects/lucore/include/lucore/Library.hpp +++ b/native/projects/lucore/include/lucore/Library.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include namespace lucore { @@ -8,7 +9,7 @@ namespace lucore { using Handle = void*; /// Open an already loaded library - static Library* OpenExisting(std::string_view dllname); + static Unique OpenExisting(std::string_view dllname); /// Query if [dllname] is loaded in the process. static bool Loaded(std::string_view dllname); @@ -21,8 +22,9 @@ namespace lucore { } private: - void* SymbolImpl(const char* symbol); constexpr explicit Library(Handle handle) : handle(handle) {} + + void* SymbolImpl(const char* symbol); Handle handle {}; }; diff --git a/native/projects/lucore/include/lucore/Types.hpp b/native/projects/lucore/include/lucore/Types.hpp index ccfa178..bd1f5b3 100644 --- a/native/projects/lucore/include/lucore/Types.hpp +++ b/native/projects/lucore/include/lucore/Types.hpp @@ -1,6 +1,6 @@ #include +#include -// namespace lucore { using u8 = std::uint8_t; using i8 = std::int8_t; using u16 = std::uint16_t; @@ -12,4 +12,11 @@ using i64 = std::int64_t; using usize = std::size_t; using isize = std::intptr_t; -//} // namespace lucore + namespace lucore { + template> + using Unique = std::unique_ptr; + + template + using Ref = std::shared_ptr; + +} // namespace lucore diff --git a/native/projects/lucore/src/Library.cpp b/native/projects/lucore/src/Library.cpp index daac631..6183d3c 100644 --- a/native/projects/lucore/src/Library.cpp +++ b/native/projects/lucore/src/Library.cpp @@ -16,7 +16,7 @@ namespace lucore { } } // namespace - Library* Library::OpenExisting(std::string_view dllname) { + Unique Library::OpenExisting(std::string_view dllname) { auto name = FormatLibraryName(dllname); if(!detail::OsLibraryLoaded(name.c_str())) { #ifndef _WIN32 @@ -29,7 +29,7 @@ namespace lucore { #endif } - return new Library(detail::OsOpenLibrary(name.c_str())); + return Unique(new Library(detail::OsOpenLibrary(name.c_str()))); } bool Library::Loaded(std::string_view dllname) { diff --git a/native/projects/riscv/include/riscv/Bus.hpp b/native/projects/riscv/include/riscv/Bus.hpp index 5d1491e..7c6ade7 100644 --- a/native/projects/riscv/include/riscv/Bus.hpp +++ b/native/projects/riscv/include/riscv/Bus.hpp @@ -109,6 +109,7 @@ namespace riscv { /// /// Note that once this function is called (and the device is successfully added), /// the object pointed to by [device] should last at least as long as the bus. + /// The bus does not own objects attached to it. /// /// # Returns /// This function returns true if the device was able to be put on the bus. diff --git a/native/projects/riscv/include/riscv/Devices/RamDevice.hpp b/native/projects/riscv/include/riscv/Devices/RamDevice.hpp index 4e19800..d47f2e8 100644 --- a/native/projects/riscv/include/riscv/Devices/RamDevice.hpp +++ b/native/projects/riscv/include/riscv/Devices/RamDevice.hpp @@ -37,6 +37,7 @@ namespace riscv::devices { Address memoryBase {}; + // TODO: convert to Unique u8* memory {}; usize memorySize {}; }; diff --git a/native/projects/riscv/include/riscv/System.hpp b/native/projects/riscv/include/riscv/System.hpp index c509e7c..b4237d2 100644 --- a/native/projects/riscv/include/riscv/System.hpp +++ b/native/projects/riscv/include/riscv/System.hpp @@ -11,22 +11,23 @@ namespace riscv { struct System { /// Create a basic system with the basic periphials created. /// All other periphials should be managed by the creator of this System - static System* Create(Address ramSize); - - ~System(); + static lucore::Unique Create(Address ramSize); void Step(); std::function OnPowerOff; std::function OnReboot; - Bus* bus; + lucore::Unique bus; // Required devices. - CPU* cpu; - devices::RamDevice* ram; - devices::SysconDevice* syscon; - devices::ClntDevice* clnt; + lucore::Unique cpu; + lucore::Unique ram; + lucore::Unique syscon; + lucore::Unique clnt; + + // call this and you get the door + System() = default; private: friend struct devices::SysconDevice; @@ -34,7 +35,6 @@ namespace riscv { void SysconPowerOff(); void SysconReboot(); - System() = default; }; } // namespace riscv diff --git a/native/projects/riscv/src/System.cpp b/native/projects/riscv/src/System.cpp index e9e1d31..43490b4 100644 --- a/native/projects/riscv/src/System.cpp +++ b/native/projects/riscv/src/System.cpp @@ -2,24 +2,24 @@ namespace riscv { - System* System::Create(Address ramSize) { - auto* system = new System; + lucore::Unique System::Create(Address ramSize) { + auto system = std::make_unique(); // create all the devices we require. - system->bus = new Bus(); - system->cpu = new CPU(); - system->ram = new devices::RamDevice(0x80000000, ramSize); - system->clnt = new devices::ClntDevice(); - system->syscon = new devices::SysconDevice(system); + system->bus = std::make_unique(); + system->cpu = std::make_unique(); + system->ram = std::make_unique(0x80000000, ramSize); + system->clnt = std::make_unique(); + system->syscon = std::make_unique(system.get()); // attach everything into the bus - if(!system->bus->AttachDevice(system->cpu)) + if(!system->bus->AttachDevice(system->cpu.get())) return nullptr; - if(!system->bus->AttachDevice(system->clnt)) + if(!system->bus->AttachDevice(system->clnt.get())) return nullptr; - if(!system->bus->AttachDevice(system->syscon)) + if(!system->bus->AttachDevice(system->syscon.get())) return nullptr; - if(!system->bus->AttachDevice(system->ram)) + if(!system->bus->AttachDevice(system->ram.get())) return nullptr; // reset the bus and all devices on it @@ -28,17 +28,6 @@ namespace riscv { return system; } - System::~System() { - delete cpu; - delete bus; - - // delete our devices - // externally bound devices should clean themselves up - delete clnt; - delete syscon; - delete ram; - } - void System::Step() { // Clock the bus, it'll do everything else. bus->Clock(); diff --git a/test-gmod/project.json b/test-gmod/project.json index 9b22e17..21bd018 100644 --- a/test-gmod/project.json +++ b/test-gmod/project.json @@ -4,8 +4,7 @@ "Configurations": { "Debug": { "CCompileFlags": "-O0", - "CppCompileFlags": "-O0", - "LinkerFlags": "" + "CppCompileFlags": "-O0" }, "Release": { "CCompileFlags": "-O2",