diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-12-30 02:41:45 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-12-30 02:41:45 +0100 |
commit | ef658594e494d34ed6af899b3524dc3b6aeed254 (patch) | |
tree | 55f49aa34df36b1d3bae3768dad151631ffa8bd0 /Libraries/LibIPC | |
parent | 00d26457c5390237f0c88a5f55ffc0c24aa6d6e8 (diff) | |
download | serenity-ef658594e494d34ed6af899b3524dc3b6aeed254.zip |
LibIPC: Let's start building custom message codecs for LibIPC
Instead of using ByteBuffer (which always malloc() their storage) for
IPC message encoding, we now use a Vector<u8, 1024>, which means that
messages smaller than 1 KB avoid heap allocation entirely.
Diffstat (limited to 'Libraries/LibIPC')
-rw-r--r-- | Libraries/LibIPC/IEncoder.h | 95 | ||||
-rw-r--r-- | Libraries/LibIPC/IMessage.h | 5 |
2 files changed, 98 insertions, 2 deletions
diff --git a/Libraries/LibIPC/IEncoder.h b/Libraries/LibIPC/IEncoder.h new file mode 100644 index 0000000000..11dc713a19 --- /dev/null +++ b/Libraries/LibIPC/IEncoder.h @@ -0,0 +1,95 @@ +#pragma once + +#include <LibIPC/IMessage.h> + +class IEncoder { +public: + explicit IEncoder(IMessageBuffer& buffer) + : m_buffer(buffer) + { + } + + IEncoder& operator<<(bool value) + { + return *this << (u8)value; + } + + IEncoder& operator<<(u8 value) + { + m_buffer.append(value); + return *this; + } + + IEncoder& operator<<(u16 value) + { + m_buffer.ensure_capacity(m_buffer.size() + 2); + m_buffer.unchecked_append((u8)value); + m_buffer.unchecked_append((u8)(value >> 8)); + return *this; + } + + IEncoder& operator<<(u32 value) + { + m_buffer.ensure_capacity(m_buffer.size() + 4); + m_buffer.unchecked_append((u8)value); + m_buffer.unchecked_append((u8)(value >> 8)); + m_buffer.unchecked_append((u8)(value >> 16)); + m_buffer.unchecked_append((u8)(value >> 24)); + return *this; + } + + IEncoder& operator<<(i8 value) + { + m_buffer.append((u8)value); + return *this; + } + + IEncoder& operator<<(i16 value) + { + m_buffer.ensure_capacity(m_buffer.size() + 2); + m_buffer.unchecked_append((u8)value); + m_buffer.unchecked_append((u8)(value >> 8)); + return *this; + } + + IEncoder& operator<<(i32 value) + { + m_buffer.ensure_capacity(m_buffer.size() + 4); + m_buffer.unchecked_append((u8)value); + m_buffer.unchecked_append((u8)(value >> 8)); + m_buffer.unchecked_append((u8)(value >> 16)); + m_buffer.unchecked_append((u8)(value >> 24)); + return *this; + } + +#ifdef __serenity__ + IEncoder& operator<<(size_t value) + { + return *this << (u32)value; + } +#endif + + IEncoder& operator<<(float value) + { + union bits { + float as_float; + u32 as_u32; + } u; + u.as_float = value; + return *this << u.as_u32; + } + + IEncoder& operator<<(const char* value) + { + return *this << StringView(value); + } + + IEncoder& operator<<(const StringView& value) + { + m_buffer.append((const u8*)value.characters_without_null_termination(), value.length()); + return *this; + } + +private: + IMessageBuffer& m_buffer; +}; diff --git a/Libraries/LibIPC/IMessage.h b/Libraries/LibIPC/IMessage.h index a6e37bdd90..96ff01125d 100644 --- a/Libraries/LibIPC/IMessage.h +++ b/Libraries/LibIPC/IMessage.h @@ -1,7 +1,8 @@ #pragma once #include <AK/String.h> -#include <AK/ByteBuffer.h> + +typedef Vector<u8, 1024> IMessageBuffer; class IMessage { public: @@ -10,7 +11,7 @@ public: virtual int endpoint_magic() const = 0; virtual int message_id() const = 0; virtual String message_name() const = 0; - virtual ByteBuffer encode() const = 0; + virtual IMessageBuffer encode() const = 0; protected: IMessage(); |