Monitoring real-time air waves I have a program that monitors real-time air waves and demodulates them into useable data. The program runs fine and with very little issues over time. The only thing I am really interested in is whether the calls to memcpy can be handled differently.

I have tried using valgrind and this code does not seem to be leaking memory. 

static output_buffer o_buffers[MAX_OUTPUT_BUFF_COUNT]; //MAX_OUTPUT_BUFF_COUNT = 100
static int o_buffer_count = 0;

struct output_buffer
{
    int16_t buf[MAXIMUM_BUF_LENGTH]; //= 262144
    int len;
};

static void rtlsdr_data_callback(unsigned char *buf, uint32_t len, void *ctx)
{
    if (do_exit)
        return;
    rtl_dev *dev_ctx = (rtl_dev *)ctx;

    demod_state *d = &amp;dev_ctx-&gt;demod;

    rotate_90(buf, len);

    int16_t buf16[MAXIMUM_BUF_LENGTH];

    //normalize the data
    for (int i = 0; i &lt; (int)len; i++)
        buf16[i] = (int16_t)buf[i] - 127;

    //create a new output buffer
    output_buffer o;
    memcpy(o.buf, buf16, 2 * len);
    o.len = len;

    if (do_exit)
        return;

    //notify the demodulation thread
    pthread_rwlock_wrlock(&amp;d-&gt;rw);
    o_buffers[o_buffer_count] = o;
    o_buffer_count++;
    pthread_rwlock_unlock(&amp;d-&gt;rw);
}

static void *demod_thread_fn(void *arg)
{
    rtl_dev *ctx = (rtl_dev *)arg;

    demod_state *d = &amp;ctx-&gt;demod;

    int fbuf_cnt = 0;
    float fbuf[MAX_DEMOD_BUFFERSIZE];
    int16_t *sp;

    d-&gt;lowpassed[0] = 0;

    while (!do_exit)
    {
        if (do_exit)
        {

            return 0;
        }
        if (!o_buffer_count)
        {
            continue;
        }

        //copy the buffers and reset the counter.. TODO: make quicker, watch for leaks
        pthread_rwlock_wrlock(&amp;d-&gt;rw);
        int old_buffer_count = o_buffer_count;
        output_buffer *old_buffers = (output_buffer *)malloc(sizeof(output_buffer) * MAX_OUTPUT_BUFF_COUNT);
        memcpy(old_buffers, o_buffers, sizeof(output_buffer) * MAX_OUTPUT_BUFF_COUNT);

        o_buffer_count = 0;
        pthread_rwlock_unlock(&amp;d-&gt;rw);

        for (int i = 0; i &lt; old_buffer_count; i++)
        {
            output_buffer o = old_buffers[i];

            memcpy(d-&gt;lowpassed, o.buf, 2 * o.len);
            d-&gt;lp_len = o.len;
            //do work with the data...
        }
        free(old_buffers);
    }

    return 0;
}


Now the actual program is quite large so a working sample will be very hard to create.

However, some notes:


pthread_rwlock_wrlock(&amp;d-&gt;rw); is referencing a pthread_rwlock_t rw;
rtlsdr_data_callback is called as soon as a buffer is ready. This method needs to exit as quick as possible to prevent data loss.
demod_thread_fn is not as important for performance as we are on another thread not responding to real-time airwaves however the block pthread_rwlock_wrlock(&amp;d-&gt;rw); to pthread_rwlock_unlock(&amp;d-&gt;rw); will consequently block the rtlsdr_data_callback
The "air-waves" are simple FLEX pager waves and transmissions may be within milliseconds.
This loop looks odd...

static void *demod_thread_fn(void *arg)
{
    while (!do_exit)
    {
        if (do_exit)
        {
            return 0;
        }

        ...
    }

    return 0;
}


Looks to me like you can get rid of the if. If do_exit can change
between the first check and the second, and that's important, then you have
a race condition -- it could change after the second check as well.  No
amount of rechecking can provide you any assurances.
Consider using a semaphore rather than busy-waiting for o_buffer_count to change.
Decide whether this is to be C or C++. :P  If it is supposed to be C++, then you should be using new and delete rather than malloc and free. If it's not, then you ought to either typedef your struct types or actually say struct output_buffer etc.