1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:57:44 +00:00

LibCore+LibIPC: Add Core::AnonymousBuffer, an IPC-friendly buffer class

This will be used to migrate remaining clients off of shbufs.
This commit is contained in:
Andreas Kling 2021-01-16 17:18:58 +01:00
parent a7f63eab84
commit 9c6c18d9b6
6 changed files with 243 additions and 0 deletions

View file

@ -26,6 +26,7 @@
#include <AK/MemoryStream.h>
#include <AK/URL.h>
#include <LibCore/AnonymousBuffer.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Dictionary.h>
#include <LibIPC/File.h>
@ -183,4 +184,24 @@ bool Decoder::decode([[maybe_unused]] File& file)
#endif
}
bool decode(Decoder& decoder, Core::AnonymousBuffer& buffer)
{
bool valid = false;
if (!decoder.decode(valid))
return false;
if (!valid) {
buffer = {};
return true;
}
u32 size;
if (!decoder.decode(size))
return false;
IPC::File anon_file;
if (!decoder.decode(anon_file))
return false;
buffer = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), size);
return buffer.is_valid();
}
}

View file

@ -27,6 +27,7 @@
#include <AK/ByteBuffer.h>
#include <AK/String.h>
#include <AK/URL.h>
#include <LibCore/AnonymousBuffer.h>
#include <LibIPC/Dictionary.h>
#include <LibIPC/Encoder.h>
#include <LibIPC/File.h>
@ -170,4 +171,13 @@ Encoder& Encoder::operator<<(const File& file)
return *this;
}
bool encode(Encoder& encoder, const Core::AnonymousBuffer& buffer)
{
encoder << buffer.is_valid();
if (buffer.is_valid()) {
encoder << (u32)buffer.size();
encoder << IPC::File(buffer.fd());
}
return true;
}
}