[LAD] easiest way to serialize messages for sending over a ringbuffer?
Paul Davis
paul at linuxaudiosystems.com
Wed Dec 14 03:33:27 UTC 2011
On Tue, Dec 13, 2011 at 10:02 PM, Iain Duncan <iainduncanlists at gmail.com> wrote:
> Thanks Paul. I'm working in C++, but I'm using the jack C api, which from
> the docs I see has a signature for
> size_t jack_ringbuffer_write ( jack_ringbuffer_t * rb, const char * src,
> size_t cnt )
>
> My DataMessage structure is just a simple C structure for now. Is there a
> recommended way of writing it to the ringbuffer given that I want to do
> something like this:
>
> void MessageQueue::push( DataMessage msg ){
> // write to the ring buffer, converting DataMessage to a string
> unsigned int written = jack_ringbuffer_write( mRingBuffer, (char *) &msg
> , sizeof(DataMessage) );
> // etc
> }
as long as the struct is POD (Plain Old Data - no embedded pointers,
etc), this will work fine.
however, you need to keep in mind that under some easily encounterable
circumstances, the write may not return sizeof(DataMessage). this is a
very easy mistake to make with ringbuffers (ditto for read).
it can be avoided via careful sizing of the ringbuffer and always
read/writing "whole objects" OR by carefully checking the results of
read/write.
the jack ringbuffer design is particular bad in this respect because
it can only hold size-1 bytes (where size is its actual size).
ardour has some slightly better behaved, templated ringbuffers for this purpose.
More information about the Linux-audio-dev
mailing list