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:
parent
92a62322f9
commit
a5f7735d9a
|
@ -83,8 +83,4 @@ namespace lcpu {
|
|||
}
|
||||
}
|
||||
|
||||
LuaCpu::~LuaCpu() {
|
||||
delete system;
|
||||
}
|
||||
|
||||
} // namespace lcpu
|
||||
|
|
|
@ -14,7 +14,6 @@ namespace lcpu {
|
|||
protected:
|
||||
friend struct lua::LuaObject<LuaCpu>;
|
||||
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<riscv::System> system;
|
||||
bool poweredOn;
|
||||
};
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ namespace lcpu {
|
|||
protected:
|
||||
friend lcpu::lua::LuaObject<LuaDevice>;
|
||||
LuaDevice(riscv::Address base, riscv::Address size);
|
||||
~LuaDevice() = default;
|
||||
|
||||
void AfterLuaInit() override;
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <lucore/Types.hpp>
|
||||
|
||||
namespace tier0 {
|
||||
lucore::Library* library = nullptr;
|
||||
lucore::Unique<lucore::Library> library = nullptr;
|
||||
|
||||
using Msg_t = void (*)(const char*, ...);
|
||||
Msg_t Msg {};
|
||||
|
@ -65,7 +65,8 @@ namespace lcpu {
|
|||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <lucore/Types.hpp>
|
||||
#include <string_view>
|
||||
|
||||
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<Library> 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 {};
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
// 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<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
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace lucore {
|
|||
}
|
||||
} // namespace
|
||||
|
||||
Library* Library::OpenExisting(std::string_view dllname) {
|
||||
Unique<Library> 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<Library>(new Library(detail::OsOpenLibrary(name.c_str())));
|
||||
}
|
||||
|
||||
bool Library::Loaded(std::string_view dllname) {
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -37,6 +37,7 @@ namespace riscv::devices {
|
|||
|
||||
Address memoryBase {};
|
||||
|
||||
// TODO: convert to Unique<u8[]>
|
||||
u8* memory {};
|
||||
usize memorySize {};
|
||||
};
|
||||
|
|
|
@ -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<System> Create(Address ramSize);
|
||||
|
||||
void Step();
|
||||
|
||||
std::function<void()> OnPowerOff;
|
||||
std::function<void()> OnReboot;
|
||||
|
||||
Bus* bus;
|
||||
lucore::Unique<Bus> bus;
|
||||
|
||||
// Required devices.
|
||||
CPU* cpu;
|
||||
devices::RamDevice* ram;
|
||||
devices::SysconDevice* syscon;
|
||||
devices::ClntDevice* clnt;
|
||||
lucore::Unique<CPU> cpu;
|
||||
lucore::Unique<devices::RamDevice> ram;
|
||||
lucore::Unique<devices::SysconDevice> syscon;
|
||||
lucore::Unique<devices::ClntDevice> 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
|
||||
|
|
|
@ -2,24 +2,24 @@
|
|||
|
||||
namespace riscv {
|
||||
|
||||
System* System::Create(Address ramSize) {
|
||||
auto* system = new System;
|
||||
lucore::Unique<System> System::Create(Address ramSize) {
|
||||
auto system = std::make_unique<System>();
|
||||
|
||||
// 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<Bus>();
|
||||
system->cpu = std::make_unique<CPU>();
|
||||
system->ram = std::make_unique<devices::RamDevice>(0x80000000, ramSize);
|
||||
system->clnt = std::make_unique<devices::ClntDevice>();
|
||||
system->syscon = std::make_unique<devices::SysconDevice>(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();
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
"Configurations": {
|
||||
"Debug": {
|
||||
"CCompileFlags": "-O0",
|
||||
"CppCompileFlags": "-O0",
|
||||
"LinkerFlags": ""
|
||||
"CppCompileFlags": "-O0"
|
||||
},
|
||||
"Release": {
|
||||
"CCompileFlags": "-O2",
|
||||
|
|
Loading…
Reference in New Issue