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
This commit is contained in:
Lily Tsuru 2023-08-02 05:30:18 -04:00
parent 92a62322f9
commit a5f7735d9a
12 changed files with 42 additions and 48 deletions

View File

@ -83,8 +83,4 @@ namespace lcpu {
} }
} }
LuaCpu::~LuaCpu() {
delete system;
}
} // namespace lcpu } // namespace lcpu

View File

@ -14,7 +14,6 @@ namespace lcpu {
protected: protected:
friend struct lua::LuaObject<LuaCpu>; friend struct lua::LuaObject<LuaCpu>;
LuaCpu(u32 memorySize); LuaCpu(u32 memorySize);
~LuaCpu();
private: private:
LUA_CLASS_FUNCTION_DECL(PoweredOn); // Check if the CPU is powered on 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 LUA_CLASS_FUNCTION_DECL(AttachDevice); // attach a LuaDevice to this cpu
// member variables // member variables
riscv::System* system; lucore::Unique<riscv::System> system;
bool poweredOn; bool poweredOn;
}; };

View File

@ -26,7 +26,6 @@ namespace lcpu {
protected: protected:
friend lcpu::lua::LuaObject<LuaDevice>; friend lcpu::lua::LuaObject<LuaDevice>;
LuaDevice(riscv::Address base, riscv::Address size); LuaDevice(riscv::Address base, riscv::Address size);
~LuaDevice() = default;
void AfterLuaInit() override; void AfterLuaInit() override;

View File

@ -5,7 +5,7 @@
#include <lucore/Types.hpp> #include <lucore/Types.hpp>
namespace tier0 { namespace tier0 {
lucore::Library* library = nullptr; lucore::Unique<lucore::Library> library = nullptr;
using Msg_t = void (*)(const char*, ...); using Msg_t = void (*)(const char*, ...);
Msg_t Msg {}; Msg_t Msg {};
@ -65,7 +65,8 @@ namespace lcpu {
} }
SourceSink::~SourceSink() { SourceSink::~SourceSink() {
delete tier0::library; // Unique<T> will do the right thing for us anyways but /shrug
tier0::library.reset();
} }
void SourceSink::OutputMessage(const lucore::Logger::MessageData& data) { void SourceSink::OutputMessage(const lucore::Logger::MessageData& data) {

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <lucore/Types.hpp>
#include <string_view> #include <string_view>
namespace lucore { namespace lucore {
@ -8,7 +9,7 @@ namespace lucore {
using Handle = void*; using Handle = void*;
/// Open an already loaded library /// Open an already loaded library
static Library* OpenExisting(std::string_view dllname); static Unique<Library> OpenExisting(std::string_view dllname);
/// Query if [dllname] is loaded in the process. /// Query if [dllname] is loaded in the process.
static bool Loaded(std::string_view dllname); static bool Loaded(std::string_view dllname);
@ -21,8 +22,9 @@ namespace lucore {
} }
private: private:
void* SymbolImpl(const char* symbol);
constexpr explicit Library(Handle handle) : handle(handle) {} constexpr explicit Library(Handle handle) : handle(handle) {}
void* SymbolImpl(const char* symbol);
Handle handle {}; Handle handle {};
}; };

View File

@ -1,6 +1,6 @@
#include <cstdint> #include <cstdint>
#include <memory>
// namespace lucore {
using u8 = std::uint8_t; using u8 = std::uint8_t;
using i8 = std::int8_t; using i8 = std::int8_t;
using u16 = std::uint16_t; using u16 = std::uint16_t;
@ -12,4 +12,11 @@ using i64 = std::int64_t;
using usize = std::size_t; using usize = std::size_t;
using isize = std::intptr_t; using isize = std::intptr_t;
//} // namespace lucore namespace lucore {
template<class T, class Deleter = std::default_delete<T>>
using Unique = std::unique_ptr<T, Deleter>;
template<class T>
using Ref = std::shared_ptr<T>;
} // namespace lucore

View File

@ -16,7 +16,7 @@ namespace lucore {
} }
} // namespace } // namespace
Library* Library::OpenExisting(std::string_view dllname) { Unique<Library> Library::OpenExisting(std::string_view dllname) {
auto name = FormatLibraryName(dllname); auto name = FormatLibraryName(dllname);
if(!detail::OsLibraryLoaded(name.c_str())) { if(!detail::OsLibraryLoaded(name.c_str())) {
#ifndef _WIN32 #ifndef _WIN32
@ -29,7 +29,7 @@ namespace lucore {
#endif #endif
} }
return new Library(detail::OsOpenLibrary(name.c_str())); return Unique<Library>(new Library(detail::OsOpenLibrary(name.c_str())));
} }
bool Library::Loaded(std::string_view dllname) { bool Library::Loaded(std::string_view dllname) {

View File

@ -109,6 +109,7 @@ namespace riscv {
/// ///
/// Note that once this function is called (and the device is successfully added), /// 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 object pointed to by [device] should last at least as long as the bus.
/// The bus does not own objects attached to it.
/// ///
/// # Returns /// # Returns
/// This function returns true if the device was able to be put on the bus. /// This function returns true if the device was able to be put on the bus.

View File

@ -37,6 +37,7 @@ namespace riscv::devices {
Address memoryBase {}; Address memoryBase {};
// TODO: convert to Unique<u8[]>
u8* memory {}; u8* memory {};
usize memorySize {}; usize memorySize {};
}; };

View File

@ -11,22 +11,23 @@ namespace riscv {
struct System { struct System {
/// Create a basic system with the basic periphials created. /// Create a basic system with the basic periphials created.
/// All other periphials should be managed by the creator of this System /// All other periphials should be managed by the creator of this System
static System* Create(Address ramSize); static lucore::Unique<System> Create(Address ramSize);
~System();
void Step(); void Step();
std::function<void()> OnPowerOff; std::function<void()> OnPowerOff;
std::function<void()> OnReboot; std::function<void()> OnReboot;
Bus* bus; lucore::Unique<Bus> bus;
// Required devices. // Required devices.
CPU* cpu; lucore::Unique<CPU> cpu;
devices::RamDevice* ram; lucore::Unique<devices::RamDevice> ram;
devices::SysconDevice* syscon; lucore::Unique<devices::SysconDevice> syscon;
devices::ClntDevice* clnt; lucore::Unique<devices::ClntDevice> clnt;
// call this and you get the door
System() = default;
private: private:
friend struct devices::SysconDevice; friend struct devices::SysconDevice;
@ -34,7 +35,6 @@ namespace riscv {
void SysconPowerOff(); void SysconPowerOff();
void SysconReboot(); void SysconReboot();
System() = default;
}; };
} // namespace riscv } // namespace riscv

View File

@ -2,24 +2,24 @@
namespace riscv { namespace riscv {
System* System::Create(Address ramSize) { lucore::Unique<System> System::Create(Address ramSize) {
auto* system = new System; auto system = std::make_unique<System>();
// create all the devices we require. // create all the devices we require.
system->bus = new Bus(); system->bus = std::make_unique<Bus>();
system->cpu = new CPU(); system->cpu = std::make_unique<CPU>();
system->ram = new devices::RamDevice(0x80000000, ramSize); system->ram = std::make_unique<devices::RamDevice>(0x80000000, ramSize);
system->clnt = new devices::ClntDevice(); system->clnt = std::make_unique<devices::ClntDevice>();
system->syscon = new devices::SysconDevice(system); system->syscon = std::make_unique<devices::SysconDevice>(system.get());
// attach everything into the bus // attach everything into the bus
if(!system->bus->AttachDevice(system->cpu)) if(!system->bus->AttachDevice(system->cpu.get()))
return nullptr; return nullptr;
if(!system->bus->AttachDevice(system->clnt)) if(!system->bus->AttachDevice(system->clnt.get()))
return nullptr; return nullptr;
if(!system->bus->AttachDevice(system->syscon)) if(!system->bus->AttachDevice(system->syscon.get()))
return nullptr; return nullptr;
if(!system->bus->AttachDevice(system->ram)) if(!system->bus->AttachDevice(system->ram.get()))
return nullptr; return nullptr;
// reset the bus and all devices on it // reset the bus and all devices on it
@ -28,17 +28,6 @@ namespace riscv {
return system; 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() { void System::Step() {
// Clock the bus, it'll do everything else. // Clock the bus, it'll do everything else.
bus->Clock(); bus->Clock();

View File

@ -4,8 +4,7 @@
"Configurations": { "Configurations": {
"Debug": { "Debug": {
"CCompileFlags": "-O0", "CCompileFlags": "-O0",
"CppCompileFlags": "-O0", "CppCompileFlags": "-O0"
"LinkerFlags": ""
}, },
"Release": { "Release": {
"CCompileFlags": "-O2", "CCompileFlags": "-O2",