Files
serenity/Kernel/Devices/Generic/RandomDevice.cpp
Liav A. 96e1391c23 Kernel/Devices: Remove the DeviceManagement singleton
This change has many improvements:
- We don't use `LockRefPtr` to hold instances of many base devices as
  with the DeviceManagement class. Instead, we have a saner pattern of
  holding them in a `NonnullRefPtr<T> const`, in a small-text footprint
  class definition in the `Device.cpp` file.
- The awkwardness of using `::the()` each time we need to get references
  to mostly-static objects (like the Event queue) in runtime is now gone
  in the migration to using the `Device` class.
- Acquiring a device feel more obvious because we use now the Device
  class for this method. The method name is improved as well.
2024-10-05 12:26:48 +02:00

50 lines
1.3 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/API/MajorNumberAllocation.h>
#include <Kernel/Devices/Device.h>
#include <Kernel/Devices/Generic/RandomDevice.h>
#include <Kernel/Sections.h>
#include <Kernel/Security/Random.h>
namespace Kernel {
UNMAP_AFTER_INIT NonnullLockRefPtr<RandomDevice> RandomDevice::must_create()
{
auto random_device_or_error = Device::try_create_device<RandomDevice>();
// FIXME: Find a way to propagate errors
VERIFY(!random_device_or_error.is_error());
return random_device_or_error.release_value();
}
UNMAP_AFTER_INIT RandomDevice::RandomDevice()
: CharacterDevice(MajorAllocation::CharacterDeviceFamily::Generic, 8)
{
}
UNMAP_AFTER_INIT RandomDevice::~RandomDevice() = default;
bool RandomDevice::can_read(OpenFileDescription const&, u64) const
{
return true;
}
ErrorOr<size_t> RandomDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
{
return buffer.write_buffered<256>(size, [&](Bytes bytes) {
get_good_random_bytes(bytes);
return bytes.size();
});
}
ErrorOr<size_t> RandomDevice::write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t size)
{
// FIXME: Use input for entropy? I guess that could be a neat feature?
return size;
}
}