Flow-IPC 3.0.0
Flow-IPC project: Full implementation reference.
msg.cpp
Go to the documentation of this file.
1/* Flow-IPC: Structured Transport
2 * Copyright (c) 2023 Akamai Technologies, Inc.; and other contributors.
3 * Each commit is copyright by its respective author or author's employer.
4 *
5 * Licensed under the MIT License:
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE. */
24
25/// @file
26
29#include <flow/error/error.hpp>
30#include <cassert>
31#include <cstdint>
32#include <cstring>
33
35{
36
37// Capped_sz_capnp_message_*der implementations.
38
40 m_seg(seg.data(),
41 (seg.size() / sizeof(::capnp::word)) * sizeof(::capnp::word)), // Round down to nearest word.
42 m_returned_seg(false)
43{
44 if (zero_it_please)
45 {
46 std::memset(m_seg.data(), 0, m_seg.size());
47 }
48 assert(m_seg.size() != 0);
49 assert(((reinterpret_cast<uintptr_t>(m_seg.data()) % alignof(::capnp::word)) == 0)
50 && "capnp requires word-aligned segments; beware, e.g., a stack byte-array sans alignas.");
51}
52
53kj::ArrayPtr<::capnp::word> Capped_sz_capnp_message_builder::allocateSegment([[maybe_unused]] unsigned int min_sz_words)
54 // Virtual.
55{
56 using flow::error::Runtime_error;
57 using Word = capnp::word;
58 using Capnp_word_buf = kj::ArrayPtr<Word>;
59
61 {
62 // Segment 1 was not enough to store the needed data; so seg.size() (size cap) was insufficient.
63 throw Runtime_error{error::Code::S_INVALID_ARGUMENT,
64 "Capped_sz_capnp_message_builder::allocateSegment()[bad size cap]"};
65 }
66 m_returned_seg = true;
67
68 assert(((min_sz_words * sizeof(Word)) <= m_seg.size())
69 && "Should not be possible: First allocateSegment() always takes `1`; second would have thrown above.");
70
71 return Capnp_word_buf{reinterpret_cast<Word*>(m_seg.data()),
72 reinterpret_cast<Word*>(static_cast<uint8_t*>(m_seg.data()) + m_seg.size())};
73}
74
76 // Init this private base (glorified data member) first. See class doc header Impl section for brief explanation.
77 Word_array_ptr_base(static_cast<const ::capnp::word*>(seg.data()),
78 seg.size() / sizeof(::capnp::word)),
80 (kj::ArrayPtr<const Word_array_ptr_base>
81 {static_cast<const Word_array_ptr_base*>(this), 1})
82{
83 assert(((reinterpret_cast<uintptr_t>(seg.data()) % alignof(::capnp::word)) == 0)
84 && "capnp requires word-aligned segments; beware, e.g., a stack byte-array sans alignas.");
85}
86
87} // namespace ipc::transport::struc
kj::ArrayPtr<::capnp::word > allocateSegment(unsigned int min_sz_words) override
Implements MessageBuilder API.
Definition: msg.cpp:53
util::Blob_mutable m_seg
Copy of seg (the buffer description... not the actual buffer!) given to ctor.
Definition: msg.hpp:117
Capped_sz_capnp_message_builder(const util::Blob_mutable &seg, bool zero_it_please)
Constructor that remembers the given memory area.
Definition: msg.cpp:39
bool m_returned_seg
true if and only if allocateSegment() has been called.
Definition: msg.hpp:120
kj::ArrayPtr< const ::capnp::word > Word_array_ptr_base
Convenience alias for capnp equivalent of util::Blob_const but in words as opposed to bytes.
Definition: msg.hpp:168
Capped_sz_capnp_message_reader(const util::Blob_const &seg)
Constructs MessageReader based on an existing contiguous memory area; is equivalent to the simple Seg...
Definition: msg.cpp:75
::capnp::SegmentArrayMessageReader Seg_array_msg_reader_base
Convenient alias for capnp SegmentArrayMessageReader.
Definition: msg.hpp:171
Small group of miscellaneous utilities to ease work with capnp (Cap'n Proto), joining its capnp names...
@ S_INVALID_ARGUMENT
User called an API with 1 or more arguments against the API spec.
Sub-module of Flow-IPC module ipc::transport providing transmission of structured messages specifical...
Definition: channel.cpp:31
boost::asio::mutable_buffer Blob_mutable
Short-hand for a mutable blob somewhere in memory, stored as exactly a void* and a size_t.
Definition: util_fwd.hpp:137
boost::asio::const_buffer Blob_const
Short-hand for an immutable blob somewhere in memory, stored as exactly a void const * and a size_t.
Definition: util_fwd.hpp:131