Skip to content

Commit 468ebb7

Browse files
Robert Roeserfacebook-github-bot
Robert Roeser
authored andcommitted
aligned buffer
Summary: Creates a buffer so the first byte of the first field of a binary encoded thrift struct is 8-byte word aligned. Differential Revision: D70034974 fbshipit-source-id: 30fc82e9c27d23cf8ac64693d141cfc8e841bad8
1 parent 4f7538d commit 468ebb7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

third-party/thrift/src/thrift/lib/cpp2/transport/rocket/framing/parser/AlignedParserStrategy.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class AlignedParserStrategy {
111111
std::unique_ptr<folly::IOBuf> createDataBuffer();
112112
std::unique_ptr<folly::IOBuf> createRequestResponseDataBuffer();
113113
std::unique_ptr<folly::IOBuf> createPayloadDataBuffer();
114+
std::unique_ptr<folly::IOBuf> createAlignedBuffer(size_t size);
114115

115116
void handleAwaitingHeader(size_t len);
116117

@@ -134,16 +135,28 @@ std::unique_ptr<folly::IOBuf> AlignedParserStrategy<T>::createHeaderBuffer() {
134135
return folly::IOBuf::createCombined(kHeaderBufferLength);
135136
}
136137

138+
template <typename T>
139+
std::unique_ptr<folly::IOBuf> AlignedParserStrategy<T>::createAlignedBuffer(
140+
const size_t size) {
141+
// We need to allocate an extra byte for the IOBuf to be able to
142+
// make sure the first field in a binary encoded thrift struct is
143+
// aligned. There are 7 bytes between the start of the first field and
144+
// the start of the struct.
145+
auto buffer = folly::IOBuf::createSeparate(size + 1);
146+
buffer->advance(1);
147+
return buffer;
148+
}
149+
137150
template <typename T>
138151
std::unique_ptr<folly::IOBuf>
139152
AlignedParserStrategy<T>::createRequestResponseDataBuffer() {
140-
return folly::IOBuf::createCombined(remainingData_);
153+
return createAlignedBuffer(remainingData_);
141154
}
142155

143156
template <typename T>
144157
std::unique_ptr<folly::IOBuf>
145158
AlignedParserStrategy<T>::createPayloadDataBuffer() {
146-
return folly::IOBuf::createCombined(remainingData_);
159+
return createAlignedBuffer(remainingData_);
147160
}
148161

149162
template <typename T>

0 commit comments

Comments
 (0)