Kernel: Make SharedMemory inherit from File.
This commit is contained in:
parent
a6d407fec5
commit
a104d7cc93
Notes:
sideshowbarker
2024-07-19 14:33:29 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/a104d7cc93e
5 changed files with 57 additions and 20 deletions
|
@ -27,6 +27,7 @@ public:
|
|||
|
||||
virtual bool is_seekable() const { return false; }
|
||||
|
||||
virtual bool is_shared_memory() const { return false; }
|
||||
virtual bool is_device() const { return false; }
|
||||
virtual bool is_tty() const { return false; }
|
||||
virtual bool is_master_pty() const { return false; }
|
||||
|
|
|
@ -23,11 +23,6 @@ Retained<FileDescriptor> FileDescriptor::create(RetainPtr<File>&& file)
|
|||
return adopt(*new FileDescriptor(move(file)));
|
||||
}
|
||||
|
||||
Retained<FileDescriptor> FileDescriptor::create(RetainPtr<SharedMemory>&& shared_memory)
|
||||
{
|
||||
return adopt(*new FileDescriptor(move(shared_memory)));
|
||||
}
|
||||
|
||||
Retained<FileDescriptor> FileDescriptor::create(RetainPtr<Socket>&& socket, SocketRole role)
|
||||
{
|
||||
return adopt(*new FileDescriptor(move(socket), role));
|
||||
|
@ -53,11 +48,6 @@ FileDescriptor::FileDescriptor(RetainPtr<File>&& file)
|
|||
{
|
||||
}
|
||||
|
||||
FileDescriptor::FileDescriptor(RetainPtr<SharedMemory>&& shared_memory)
|
||||
: m_shared_memory(move(shared_memory))
|
||||
{
|
||||
}
|
||||
|
||||
FileDescriptor::FileDescriptor(RetainPtr<Socket>&& socket, SocketRole role)
|
||||
: m_socket(move(socket))
|
||||
{
|
||||
|
@ -448,3 +438,22 @@ KResult FileDescriptor::truncate(off_t length)
|
|||
ASSERT(is_shared_memory());
|
||||
return shared_memory()->truncate(length);
|
||||
}
|
||||
|
||||
bool FileDescriptor::is_shared_memory() const
|
||||
{
|
||||
return m_file && m_file->is_shared_memory();
|
||||
}
|
||||
|
||||
SharedMemory* FileDescriptor::shared_memory()
|
||||
{
|
||||
if (!is_shared_memory())
|
||||
return nullptr;
|
||||
return static_cast<SharedMemory*>(m_file.ptr());
|
||||
}
|
||||
|
||||
const SharedMemory* FileDescriptor::shared_memory() const
|
||||
{
|
||||
if (!is_shared_memory())
|
||||
return nullptr;
|
||||
return static_cast<const SharedMemory*>(m_file.ptr());
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
#include <AK/Retainable.h>
|
||||
#include <AK/Badge.h>
|
||||
#include <Kernel/Net/Socket.h>
|
||||
#include <Kernel/SharedMemory.h>
|
||||
|
||||
class File;
|
||||
class TTY;
|
||||
|
@ -17,13 +16,13 @@ class MasterPTY;
|
|||
class Process;
|
||||
class Region;
|
||||
class CharacterDevice;
|
||||
class SharedMemory;
|
||||
|
||||
class FileDescriptor : public Retainable<FileDescriptor> {
|
||||
public:
|
||||
static Retained<FileDescriptor> create(RetainPtr<Socket>&&, SocketRole = SocketRole::None);
|
||||
static Retained<FileDescriptor> create(RetainPtr<Inode>&&);
|
||||
static Retained<FileDescriptor> create(RetainPtr<File>&&);
|
||||
static Retained<FileDescriptor> create(RetainPtr<SharedMemory>&&);
|
||||
static Retained<FileDescriptor> create_pipe_writer(FIFO&);
|
||||
static Retained<FileDescriptor> create_pipe_reader(FIFO&);
|
||||
~FileDescriptor();
|
||||
|
@ -85,9 +84,9 @@ public:
|
|||
FIFO::Direction fifo_direction() { return m_fifo_direction; }
|
||||
|
||||
bool is_fsfile() const;
|
||||
bool is_shared_memory() const { return m_shared_memory; }
|
||||
SharedMemory* shared_memory() { return m_shared_memory.ptr(); }
|
||||
const SharedMemory* shared_memory() const { return m_shared_memory.ptr(); }
|
||||
bool is_shared_memory() const;
|
||||
SharedMemory* shared_memory();
|
||||
const SharedMemory* shared_memory() const;
|
||||
|
||||
ByteBuffer& generator_cache() { return m_generator_cache; }
|
||||
|
||||
|
@ -103,7 +102,6 @@ private:
|
|||
FileDescriptor(RetainPtr<Socket>&&, SocketRole);
|
||||
explicit FileDescriptor(RetainPtr<Inode>&&);
|
||||
explicit FileDescriptor(RetainPtr<File>&&);
|
||||
explicit FileDescriptor(RetainPtr<SharedMemory>&&);
|
||||
FileDescriptor(FIFO&, FIFO::Direction);
|
||||
|
||||
RetainPtr<Inode> m_inode;
|
||||
|
@ -122,8 +120,6 @@ private:
|
|||
RetainPtr<FIFO> m_fifo;
|
||||
FIFO::Direction m_fifo_direction { FIFO::Neither };
|
||||
|
||||
RetainPtr<SharedMemory> m_shared_memory;
|
||||
|
||||
bool m_closed { false };
|
||||
};
|
||||
|
||||
|
|
|
@ -67,3 +67,24 @@ KResult SharedMemory::truncate(int length)
|
|||
ASSERT_NOT_REACHED();
|
||||
return KResult(-ENOTIMPL);
|
||||
}
|
||||
|
||||
String SharedMemory::absolute_path() const
|
||||
{
|
||||
return String::format("shm:%u", this);
|
||||
}
|
||||
|
||||
int SharedMemory::read(Process&, byte* buffer, int buffer_size)
|
||||
{
|
||||
UNUSED_PARAM(buffer);
|
||||
UNUSED_PARAM(buffer_size);
|
||||
// FIXME: Implement.
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
int SharedMemory::write(Process&, const byte* data, int data_size)
|
||||
{
|
||||
UNUSED_PARAM(data);
|
||||
UNUSED_PARAM(data_size);
|
||||
// FIXME: Implement.
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
|
|
@ -5,14 +5,15 @@
|
|||
#include <AK/AKString.h>
|
||||
#include <Kernel/KResult.h>
|
||||
#include <Kernel/UnixTypes.h>
|
||||
#include <Kernel/File.h>
|
||||
|
||||
class VMObject;
|
||||
|
||||
class SharedMemory : public Retainable<SharedMemory> {
|
||||
class SharedMemory : public File {
|
||||
public:
|
||||
static KResultOr<Retained<SharedMemory>> open(const String& name, int flags, mode_t);
|
||||
static KResult unlink(const String& name);
|
||||
~SharedMemory();
|
||||
virtual ~SharedMemory() override;
|
||||
|
||||
String name() const { return m_name; }
|
||||
KResult truncate(off_t);
|
||||
|
@ -22,6 +23,15 @@ public:
|
|||
gid_t gid() const { return m_gid; }
|
||||
|
||||
private:
|
||||
// ^File
|
||||
virtual bool can_read(Process&) const override { return true; }
|
||||
virtual bool can_write(Process&) const override { return true; }
|
||||
virtual int read(Process&, byte*, int) override;
|
||||
virtual int write(Process&, const byte*, int) override;
|
||||
virtual String absolute_path() const override;
|
||||
virtual const char* class_name() const override { return "SharedMemory"; }
|
||||
virtual bool is_shared_memory() const override { return true; }
|
||||
|
||||
SharedMemory(const String& name, uid_t, gid_t, mode_t);
|
||||
|
||||
String m_name;
|
||||
|
|
Loading…
Add table
Reference in a new issue