diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-28 22:31:31 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-28 22:31:31 +0200 |
commit | a104d7cc93e3c306b36227ee200d5f0ab0787dd3 (patch) | |
tree | 83d3fd109a77513cabd399786a50b03a370acba2 /Kernel | |
parent | a6d407fec5b85963043521aa186fd09b1e815b5e (diff) | |
download | serenity-a104d7cc93e3c306b36227ee200d5f0ab0787dd3.zip |
Kernel: Make SharedMemory inherit from File.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/File.h | 1 | ||||
-rw-r--r-- | Kernel/FileSystem/FileDescriptor.cpp | 29 | ||||
-rw-r--r-- | Kernel/FileSystem/FileDescriptor.h | 12 | ||||
-rw-r--r-- | Kernel/SharedMemory.cpp | 21 | ||||
-rw-r--r-- | Kernel/SharedMemory.h | 14 |
5 files changed, 57 insertions, 20 deletions
diff --git a/Kernel/File.h b/Kernel/File.h index e3e67d9d01..0733d0a8a5 100644 --- a/Kernel/File.h +++ b/Kernel/File.h @@ -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; } diff --git a/Kernel/FileSystem/FileDescriptor.cpp b/Kernel/FileSystem/FileDescriptor.cpp index 9d4d22a258..91f59b8ffc 100644 --- a/Kernel/FileSystem/FileDescriptor.cpp +++ b/Kernel/FileSystem/FileDescriptor.cpp @@ -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()); +} diff --git a/Kernel/FileSystem/FileDescriptor.h b/Kernel/FileSystem/FileDescriptor.h index 8a4a8fd41c..d99eecf1f6 100644 --- a/Kernel/FileSystem/FileDescriptor.h +++ b/Kernel/FileSystem/FileDescriptor.h @@ -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 }; }; diff --git a/Kernel/SharedMemory.cpp b/Kernel/SharedMemory.cpp index c2239624eb..698efbf8db 100644 --- a/Kernel/SharedMemory.cpp +++ b/Kernel/SharedMemory.cpp @@ -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(); +} diff --git a/Kernel/SharedMemory.h b/Kernel/SharedMemory.h index 4dc2463f07..a7347274ea 100644 --- a/Kernel/SharedMemory.h +++ b/Kernel/SharedMemory.h @@ -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; |