kernel: Make DeviceRegistry store the created devices instead of creating them when requested
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-05-09 18:31:27 +02:00
parent 35b1b36599
commit 4753e80583
Signed by: apio
GPG Key ID: B8A7D06E42258954
11 changed files with 29 additions and 28 deletions

View File

@ -1,6 +1,7 @@
#include "fs/devices/ConsoleDevice.h"
#include "Log.h"
#include "arch/Keyboard.h"
#include "fs/devices/DeviceRegistry.h"
#include "memory/MemoryManager.h"
#include "thread/Scheduler.h"
#include "video/TextConsole.h"
@ -14,9 +15,10 @@ static Buffer g_console_input;
static bool g_eof { false };
static bool g_echo { true };
Result<SharedPtr<Device>> ConsoleDevice::create()
Result<void> ConsoleDevice::create()
{
return (SharedPtr<Device>)TRY(make_shared<ConsoleDevice>());
auto device = (SharedPtr<Device>)TRY(make_shared<ConsoleDevice>());
return DeviceRegistry::register_special_device(DeviceRegistry::Console, 0, device);
}
Result<usize> ConsoleDevice::read(u8* buf, usize, usize length) const

View File

@ -5,7 +5,7 @@ class ConsoleDevice : public Device
{
public:
// Initializer for DeviceRegistry.
static Result<SharedPtr<Device>> create();
static Result<void> create();
Result<usize> read(u8*, usize, usize) const override;

View File

@ -8,7 +8,7 @@
struct DeviceDescriptor
{
device_create_func_t initializer;
SharedPtr<Device> device;
u32 major;
u32 minor;
};
@ -17,17 +17,17 @@ Vector<DeviceDescriptor> g_available_devices = {};
namespace DeviceRegistry
{
Result<SharedPtr<Device>> create_special_device(u32 major, u32 minor)
Result<SharedPtr<Device>> fetch_special_device(u32 major, u32 minor)
{
for (const auto& descriptor : g_available_devices)
{
if (descriptor.major == major && descriptor.minor == minor) return descriptor.initializer();
if (descriptor.major == major && descriptor.minor == minor) return descriptor.device;
}
return err(ENODEV);
}
Result<void> register_special_device(u32 major, u32 minor, device_create_func_t initializer)
Result<void> register_special_device(u32 major, u32 minor, SharedPtr<Device> device)
{
for (const auto& descriptor : g_available_devices)
{
@ -36,17 +36,15 @@ namespace DeviceRegistry
kdbgln("DeviceRegistry: registered new device type %u:%u", major, minor);
return g_available_devices.try_append(
DeviceDescriptor { .initializer = initializer, .major = major, .minor = minor });
return g_available_devices.try_append(DeviceDescriptor { .device = device, .major = major, .minor = minor });
}
Result<void> init()
{
// Hardcode default devices.
TRY(register_special_device(DeviceMajorTypes::Memory, 0, NullDevice::create));
TRY(register_special_device(DeviceMajorTypes::Memory, 1, ZeroDevice::create));
TRY(register_special_device(DeviceMajorTypes::Console, 0, ConsoleDevice::create));
TRY(register_special_device(DeviceMajorTypes::Framebuffer, 0, FramebufferDevice::create));
NullDevice::create();
ZeroDevice::create();
ConsoleDevice::create();
FramebufferDevice::create();
return {};
}

View File

@ -3,8 +3,6 @@
#include "fs/devices/Device.h"
#include <luna/SharedPtr.h>
typedef Result<SharedPtr<Device>> (*device_create_func_t)(void);
namespace DeviceRegistry
{
enum DeviceMajorTypes : u32
@ -15,9 +13,9 @@ namespace DeviceRegistry
Framebuffer = 3,
};
Result<SharedPtr<Device>> create_special_device(u32 major, u32 minor);
Result<SharedPtr<Device>> fetch_special_device(u32 major, u32 minor);
Result<void> register_special_device(u32 major, u32 minor, device_create_func_t initializer);
Result<void> register_special_device(u32 major, u32 minor, SharedPtr<Device> device);
Result<void> init();
}

View File

@ -3,9 +3,10 @@
#include <bits/ioctl-defs.h>
#include <luna/CString.h>
Result<SharedPtr<Device>> FramebufferDevice::create()
Result<void> FramebufferDevice::create()
{
return (SharedPtr<Device>)TRY(make_shared<FramebufferDevice>());
auto device = (SharedPtr<Device>)TRY(make_shared<FramebufferDevice>());
return DeviceRegistry::register_special_device(DeviceRegistry::Framebuffer, 0, device);
}
Result<usize> FramebufferDevice::read(u8*, usize, usize) const

View File

@ -5,7 +5,7 @@ class FramebufferDevice : public Device
{
public:
// Initializer for DeviceRegistry.
static Result<SharedPtr<Device>> create();
static Result<void> create();
Result<usize> read(u8*, usize, usize) const override;

View File

@ -1,6 +1,7 @@
#include "fs/devices/NullDevice.h"
Result<SharedPtr<Device>> NullDevice::create()
Result<void> NullDevice::create()
{
return (SharedPtr<Device>)TRY(make_shared<NullDevice>());
auto device = (SharedPtr<Device>)TRY(make_shared<NullDevice>());
return DeviceRegistry::register_special_device(DeviceRegistry::Memory, 0, device);
}

View File

@ -5,7 +5,7 @@ class NullDevice : public Device
{
public:
// Initializer for DeviceRegistry.
static Result<SharedPtr<Device>> create();
static Result<void> create();
Result<usize> read(u8*, usize, usize) const override
{

View File

@ -1,6 +1,7 @@
#include "fs/devices/ZeroDevice.h"
Result<SharedPtr<Device>> ZeroDevice::create()
Result<void> ZeroDevice::create()
{
return (SharedPtr<Device>)TRY(make_shared<ZeroDevice>());
auto device = (SharedPtr<Device>)TRY(make_shared<ZeroDevice>());
return DeviceRegistry::register_special_device(DeviceRegistry::Memory, 1, device);
}

View File

@ -5,7 +5,7 @@ class ZeroDevice : public Device
{
public:
// Initializer for DeviceRegistry.
static Result<SharedPtr<Device>> create();
static Result<void> create();
Result<usize> read(u8* buf, usize, usize length) const override
{

View File

@ -39,7 +39,7 @@ namespace TmpFS
Result<SharedPtr<VFS::Inode>> FileSystem::create_device_inode(u32 major, u32 minor)
{
SharedPtr<Device> device = TRY(DeviceRegistry::create_special_device(major, minor));
SharedPtr<Device> device = TRY(DeviceRegistry::fetch_special_device(major, minor));
SharedPtr<DeviceInode> inode = TRY(make_shared<DeviceInode>());
inode->set_fs(*this, {});