ladybird/Kernel/Devices/NullDevice.cpp
Liav A f5de4f24b2 Kernel/Devices: Defer creation of SysFS component after the constructor
Instead of doing so in the constructor, let's do immediately after the
constructor, so we can safely pass a reference of a Device, so the
SysFSDeviceComponent constructor can use that object to identify whether
it's a block device or a character device.
This allows to us to not hold a device in SysFSDeviceComponent with a
RefPtr.
Also, we also call the before_removing method in both SlavePTY::unref
and File::unref, so because Device has that method being overrided, it
can ensure the device is removed always cleanly.
2021-09-11 11:41:14 +02:00

50 lines
898 B
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Singleton.h>
#include <Kernel/Devices/NullDevice.h>
#include <Kernel/Sections.h>
namespace Kernel {
static Singleton<NullDevice> s_the;
UNMAP_AFTER_INIT void NullDevice::initialize()
{
s_the.ensure_instance();
s_the->after_inserting();
}
NullDevice& NullDevice::the()
{
return *s_the;
}
UNMAP_AFTER_INIT NullDevice::NullDevice()
: CharacterDevice(1, 3)
{
}
UNMAP_AFTER_INIT NullDevice::~NullDevice()
{
}
bool NullDevice::can_read(const OpenFileDescription&, size_t) const
{
return true;
}
KResultOr<size_t> NullDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t)
{
return 0;
}
KResultOr<size_t> NullDevice::write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t buffer_size)
{
return buffer_size;
}
}