Commit b597143 1 parent 9654151 commit b597143 Copy full SHA for b597143
File tree 1 file changed +14
-5
lines changed
1 file changed +14
-5
lines changed Original file line number Diff line number Diff line change 1
1
#pragma once
2
2
3
+ #include < algorithm>
3
4
#include < cstddef>
4
5
#include < map>
5
6
#include < string>
@@ -96,17 +97,25 @@ class PubMaster {
96
97
class AlignedBuffer {
97
98
public:
98
99
kj::ArrayPtr<const capnp::word> align (const char *data, const size_t size) {
99
- words_size = size / sizeof (capnp::word) + 1 ;
100
- if (aligned_buf.size () < words_size) {
101
- aligned_buf = kj::heapArray<capnp::word>(words_size < 512 ? 512 : words_size);
100
+ size_t word_count = (size + sizeof (capnp::word) - 1 ) / sizeof (capnp::word);
101
+
102
+ // Check if data is already aligned
103
+ if (reinterpret_cast <uintptr_t >(data) % alignof (capnp::word) == 0 ) {
104
+ return kj::ArrayPtr<const capnp::word>((const capnp::word *)data, word_count);
105
+ }
106
+
107
+ // Data is not aligned, perform alignment
108
+ if (aligned_buf.size () < word_count) {
109
+ aligned_buf = kj::heapArray<capnp::word>(std::max (word_count, size_t (512 )));
102
110
}
103
111
memcpy (aligned_buf.begin (), data, size);
104
- return aligned_buf.slice (0 , words_size );
112
+ return aligned_buf.slice (0 , word_count );
105
113
}
114
+
106
115
inline kj::ArrayPtr<const capnp::word> align (Message *m) {
107
116
return align (m->getData (), m->getSize ());
108
117
}
118
+
109
119
private:
110
120
kj::Array<capnp::word> aligned_buf;
111
- size_t words_size;
112
121
};
You can’t perform that action at this time.
0 commit comments