| 234 | // flatbuffers-encoded region and wraps it in a `Message<T>` to handle buffer |
| 235 | // ownership. |
| 236 | template<class T> Message<T> GetMessage() { |
| 237 | auto buf_data = buf_.scratch_data(); // pointer to memory |
| 238 | auto buf_size = buf_.capacity(); // size of memory |
| 239 | auto msg_data = buf_.data(); // pointer to msg |
| 240 | auto msg_size = buf_.size(); // size of msg |
| 241 | // Do some sanity checks on data/size |
| 242 | FLATBUFFERS_ASSERT(msg_data); |
| 243 | FLATBUFFERS_ASSERT(msg_size); |
| 244 | FLATBUFFERS_ASSERT(msg_data >= buf_data); |
| 245 | FLATBUFFERS_ASSERT(msg_data + msg_size <= buf_data + buf_size); |
| 246 | // Calculate offsets from the buffer start |
| 247 | auto begin = msg_data - buf_data; |
| 248 | auto end = begin + msg_size; |
| 249 | // Get the slice we are working with (no refcount change) |
| 250 | grpc_slice slice = slice_allocator_.get_slice(buf_data, buf_size); |
| 251 | // Extract a subslice of the existing slice (increment refcount) |
| 252 | grpc_slice subslice = grpc_slice_sub(slice, begin, end); |
| 253 | // Wrap the subslice in a `Message<T>`, but don't increment refcount |
| 254 | Message<T> msg(subslice, false); |
| 255 | return msg; |
| 256 | } |
| 257 | |
| 258 | template<class T> Message<T> ReleaseMessage() { |
| 259 | Message<T> msg = GetMessage<T>(); |