I can’t seem to find a clear distinction between the MergeFrom* and the ParseFrom* methods of the MessageLite class in protobuf.
I am trying to minimize the amount of data copying I have to do, so I have written the following code below to decode a length-prefixed-message:
bool StreamMessageDelimiter::receiveWithLengthPrefix(Message& message)
{
google::protobuf::uint32 messageSize;
auto_ptr<google::protobuf::uint8> prefixBuf(new google::protobuf::uint8[sizeof(messageSize)]);
int receivedBytes = receiveNBytes(prefixBuf.get(), sizeof(messageSize));
if(receivedBytes != sizeof(messageSize))
{
return false;
}
CodedInputStream prefixInput(prefixBuf.get(), sizeof(messageSize));
prefixInput.ReadLittleEndian32(&messageSize);
google::protobuf::uint8* payloadBuf = new google::protobuf::uint8[messageSize];
receivedBytes = receiveNBytes(payloadBuf, messageSize);
if(receivedBytes != messageSize)
{
return false;
}
ArrayInputStream rawInput(payloadBuf, messageSize);
CodedInputStream codedInput(&rawInput);
if(!message.MergeFromCodedStream(&codedInput))
{
return false;
}
return true;
}
My question is does using the MergeFromCodedStream cause message to take ownership of the payloadBuf, or does message make a copy of the underlying data? If message does indeed make a copy, then I obviously should use an auto_ptr for payloadBuf like I did for the prefixBuf.
Thanks for the input!
Check the documentation for ArrayInputStream:
So no, it doesn’t take ownership, and you should make sure to release the memory at the right time.
I think you might be confused by the use of the word
Mergein the function names. It does not come to say “data is merged from the passed buffer” (which I think caused you to consider ownership), but rather “data is merged into the message”. So,ParsecallsClear()before filling the message, whereasMergeuses the message you passed directly as-is.