Re: [LAD] easiest way to serialize messages for sending over a ringbuffer?

From: Paul Davis <paul@email-addr-hidden>
Date: Wed Dec 14 2011 - 05:33:27 EET

On Tue, Dec 13, 2011 at 10:02 PM, Iain Duncan <iainduncanlists@email-addr-hidden> 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.
_______________________________________________
Linux-audio-dev mailing list
Linux-audio-dev@email-addr-hidden
http://lists.linuxaudio.org/listinfo/linux-audio-dev
Received on Wed Dec 14 08:15:01 2011

This archive was generated by hypermail 2.1.8 : Wed Dec 14 2011 - 08:15:01 EET