AlignedBuffer remove operator() to prevent out of scope usage

This commit is contained in:
Willem Melching 2021-03-17 14:22:57 +01:00
parent 1a55d4dbac
commit 8203d55702
2 changed files with 5 additions and 6 deletions

View File

@ -123,9 +123,7 @@ private:
class AlignedBuffer {
public:
AlignedBuffer() = default;
AlignedBuffer(const char *data, const size_t size) { get(data, size); }
kj::ArrayPtr<const capnp::word> get(const char *data, const size_t size) {
kj::ArrayPtr<const capnp::word> align(const char *data, const size_t size) {
words_size = size / sizeof(capnp::word) + 1;
if (aligned_buf.size() < words_size) {
aligned_buf = kj::heapArray<capnp::word>(words_size < 512 ? 512 : words_size);
@ -133,8 +131,9 @@ public:
memcpy(aligned_buf.begin(), data, size);
return aligned_buf.slice(0, words_size);
}
inline kj::ArrayPtr<const capnp::word> get(Message *m) { return get(m->getData(), m->getSize()); }
inline operator kj::ArrayPtr<const capnp::word>() { return aligned_buf.slice(0, words_size); }
inline kj::ArrayPtr<const capnp::word> align(Message *m) {
return align(m->getData(), m->getSize());
}
private:
kj::Array<capnp::word> aligned_buf;
size_t words_size;

View File

@ -77,7 +77,7 @@ int SubMaster::update(int timeout) {
if (m->msg_reader) {
m->msg_reader->~FlatArrayMessageReader();
}
m->msg_reader = new (m->allocated_msg_reader) capnp::FlatArrayMessageReader(m->aligned_buf.get(msg));
m->msg_reader = new (m->allocated_msg_reader) capnp::FlatArrayMessageReader(m->aligned_buf.align(msg));
delete msg;
m->event = m->msg_reader->getRoot<cereal::Event>();
m->updated = true;