Пример #1
0
unsigned char *sg_bip_buf_get(const sg_bip_buf_t *buf, const unsigned int size)
{
    struct sg_bip_buf_real *me = (struct sg_bip_buf_real *)buf;

    if (sg_bip_buf_is_empty(buf))
        return NULL;

    /* make sure we can actually poll this buf */
    if (me->buf_size < me->a_start + size)
        return NULL;

    void *end = me->buf + me->a_start;
    me->a_start += size;

    /* we seem to be empty.. */
    if (me->a_start == me->a_end) {
        /* replace a with region b */
        if (1 == me->b_inuse) {
            me->a_start = 0;
            me->a_end = me->b_end;
            me->b_end = me->b_inuse = 0;
        } else
            /* safely move cursor back to the start because we are empty */
            me->a_start = me->a_end = 0;
    }

    __check_for_switch_to_b(me);
    return end;
}
Пример #2
0
unsigned char *bipbuf_poll(bipbuf_t* me, const unsigned int size)
{
    if (bipbuf_is_empty(me))
        return NULL;

    /* make sure we can actually poll this data */
    if (me->size < me->a_start + size)
        return NULL;

    void *end = me->data + me->a_start;
    me->a_start += size;

    /* we seem to be empty.. */
    if (me->a_start == me->a_end)
    {
        /* replace a with region b */
        if (1 == me->b_inuse)
        {
            me->a_start = 0;
            me->a_end = me->b_end;
            me->b_end = me->b_inuse = 0;
        }
        else
            /* safely move cursor back to the start because we are empty */
            me->a_start = me->a_end = 0;
    }

    __check_for_switch_to_b(me);
    return end;
}
Пример #3
0
int bipbuf_push(bipbuf_t* me, const int size)
{
    if (bipbuf_unused(me) < size)
        return 0;

    if (1 == me->b_inuse)
    {
        me->b_end += size;
    }
    else
    {
        me->a_end += size;
    }

    __check_for_switch_to_b(me);
    return size;
}
Пример #4
0
int sg_bip_buf_put(const sg_bip_buf_t *buf, const unsigned char *data, const int size)
{
    struct sg_bip_buf_real *me = (struct sg_bip_buf_real *)buf;

    /* not enough space */
    if (sg_bip_buf_unused_size(buf) < size)
        return 0;

    if (1 == me->b_inuse) {
        memcpy(me->buf + me->b_end, data, size);
        me->b_end += size;
    } else {
        memcpy(me->buf + me->a_end, data, size);
        me->a_end += size;
    }

    __check_for_switch_to_b(me);
    return size;
}
Пример #5
0
int bipbuf_offer(bipbuf_t* me, const unsigned char *data, const int size)
{
    /* not enough space */
    if (bipbuf_unused(me) < size)
        return 0;

    if (1 == me->b_inuse)
    {
        memcpy(me->data + me->b_end, data, size);
        me->b_end += size;
    }
    else
    {
        memcpy(me->data + me->a_end, data, size);
        me->a_end += size;
    }

    __check_for_switch_to_b(me);
    return size;
}