mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 10:22:05 -05:00
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.
This commit is contained in:
parent
00d26457c5
commit
ef658594e4
3 changed files with 102 additions and 6 deletions
|
@ -201,6 +201,7 @@ int main(int argc, char** argv)
|
|||
dbg() << "#include <AK/OwnPtr.h>";
|
||||
dbg() << "#include <LibDraw/Color.h>";
|
||||
dbg() << "#include <LibDraw/Rect.h>";
|
||||
dbg() << "#include <LibIPC/IEncoder.h>";
|
||||
dbg() << "#include <LibIPC/IEndpoint.h>";
|
||||
dbg() << "#include <LibIPC/IMessage.h>";
|
||||
dbg();
|
||||
|
@ -358,11 +359,11 @@ int main(int argc, char** argv)
|
|||
dbg() << " size_in_bytes = stream.offset();";
|
||||
dbg() << " return make<" << name << ">(" << builder.to_string() << ");";
|
||||
dbg() << " }";
|
||||
dbg() << " virtual ByteBuffer encode() const override";
|
||||
dbg() << " virtual IMessageBuffer encode() const override";
|
||||
dbg() << " {";
|
||||
// FIXME: Support longer messages:
|
||||
dbg() << " auto buffer = ByteBuffer::create_uninitialized(4096);";
|
||||
dbg() << " BufferStream stream(buffer);";
|
||||
dbg() << " IMessageBuffer buffer;";
|
||||
dbg() << " IEncoder stream(buffer);";
|
||||
dbg() << " stream << endpoint_magic();";
|
||||
dbg() << " stream << (int)MessageID::" << name << ";";
|
||||
for (auto& parameter : parameters) {
|
||||
|
@ -398,7 +399,6 @@ int main(int argc, char** argv)
|
|||
dbg() << " stream << m_" << parameter.name << ";";
|
||||
}
|
||||
}
|
||||
dbg() << " stream.snip();";
|
||||
dbg() << " return buffer;";
|
||||
dbg() << " }";
|
||||
for (auto& parameter : parameters) {
|
||||
|
|
95
Libraries/LibIPC/IEncoder.h
Normal file
95
Libraries/LibIPC/IEncoder.h
Normal file
|
@ -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;
|
||||
};
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Reference in a new issue