CIRCT 23.0.0git
Loading...
Searching...
No Matches
Common.cpp
Go to the documentation of this file.
1//===- Common.cpp ---------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// DO NOT EDIT!
10// This file is distributed as part of an ESI package. The source for this file
11// should always be modified within CIRCT (lib/dialect/ESI/runtime/cpp/).
12//
13//===----------------------------------------------------------------------===//
14
15#include "esi/Common.h"
16
17#include <iostream>
18#include <sstream>
19
20using namespace esi;
21
22std::string MessageData::toHex() const {
23 std::ostringstream ss;
24 ss << std::hex;
25 for (size_t i = 0, e = data.size(); i != e; ++i) {
26 // Add spaces every 8 bytes.
27 if (i % 8 == 0 && i != 0)
28 ss << ' ';
29 // Add an extra space every 64 bytes.
30 if (i % 64 == 0 && i != 0)
31 ss << ' ';
32 ss << static_cast<unsigned>(data[i]);
33 }
34 return ss.str();
35}
36
37std::string esi::toHex(void *val) {
38 return toHex(reinterpret_cast<uint64_t>(val));
39}
40
41std::string esi::toHex(uint64_t val) {
42 std::ostringstream ss;
43 ss << std::hex << val;
44 return ss.str();
45}
46
47//===----------------------------------------------------------------------===//
48// SegmentedMessageData
49//===----------------------------------------------------------------------===//
50
52 size_t total = 0;
53 for (size_t i = 0, e = numSegments(); i < e; ++i)
54 total += segment(i).size;
55 return total;
56}
57
58bool SegmentedMessageData::empty() const { return totalSize() == 0; }
59
61 size_t total = totalSize();
62 if (total == 0)
63 return MessageData();
64 std::vector<uint8_t> buf;
65 buf.reserve(total);
66 for (size_t i = 0, e = numSegments(); i < e; ++i) {
67 Segment seg = segment(i);
68 if (seg.size == 0)
69 continue;
70 buf.insert(buf.end(), seg.data, seg.data + seg.size);
71 }
72 return MessageData(std::move(buf));
73}
74
75//===----------------------------------------------------------------------===//
76// SegmentedMessageDataCursor
77//===----------------------------------------------------------------------===//
78
79std::span<const uint8_t> SegmentedMessageDataCursor::remaining() const {
80 // Scan forward past empty segments without mutating cursor state.
81 size_t idx = segIdx;
82 size_t off = offset;
83 while (idx < msg.numSegments()) {
84 Segment seg = msg.segment(idx);
85 if (seg.size > off)
86 return {seg.data + off, seg.size - off};
87 ++idx;
88 off = 0;
89 }
90 return {};
91}
92
94 while (n > 0 && !done()) {
96 size_t left = seg.size - offset;
97 if (left == 0) {
98 // Skip empty segments.
99 ++segIdx;
100 offset = 0;
101 continue;
102 }
103 if (n < left) {
104 offset += n;
105 return;
106 }
107 n -= left;
108 ++segIdx;
109 offset = 0;
110 }
111}
112
114 return segIdx >= msg.numSegments();
115}
116
118 segIdx = 0;
119 offset = 0;
120}
A logical chunk of data representing serialized data.
Definition Common.h:113
std::string toHex() const
Convert the data to a hex string.
Definition Common.cpp:22
std::vector< uint8_t > data
Definition Common.h:166
const SegmentedMessageData & msg
Definition Common.h:232
void reset()
Reset to the beginning.
Definition Common.cpp:117
void advance(size_t n)
Advance by n bytes, crossing segment boundaries as needed.
Definition Common.cpp:93
bool done() const
True when all segments have been consumed.
Definition Common.cpp:113
std::span< const uint8_t > remaining() const
Contiguous span from current position to end of current segment.
Definition Common.cpp:79
virtual Segment segment(size_t idx) const =0
Get a segment by index.
MessageData toMessageData() const
Flatten all segments into a standard MessageData.
Definition Common.cpp:60
bool empty() const
True if totalSize() == 0.
Definition Common.cpp:58
size_t totalSize() const
Total size across all segments.
Definition Common.cpp:51
virtual size_t numSegments() const =0
Number of segments in the message.
Definition esi.py:1
std::string toHex(void *val)
Definition Common.cpp:37
A contiguous, non-owning view of bytes within a SegmentedMessageData.
Definition Common.h:175
const uint8_t * data
Definition Common.h:176
size_t size
Definition Common.h:177