Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC bipbup_offer() : try to fit after A before activating B #6

Open
fenugrec opened this issue Dec 6, 2017 · 0 comments
Open

RFC bipbup_offer() : try to fit after A before activating B #6

fenugrec opened this issue Dec 6, 2017 · 0 comments

Comments

@fenugrec
Copy link

fenugrec commented Dec 6, 2017

Here's a different implementation of bipbup_offer() :

  • if B is in use, append data there (no change)
  • if B is unused, and there is enough space after A, append data there
  • fallback : activate B area and write data there, if possible.

Advantage is slightly increased efficiency, at basically 0 cost.

Note : currently untested

*****
int bipbuf_offer(bipbuf_t* me, const unsigned char *data, const int size)
{
    if (1 == me->b_inuse)
    {
	/* check available space between region B and region A */
        if ((me->a_start - me->b_end) < size) return 0;

        memcpy(me->data + me->b_end, data, size);
        me->b_end += size;
	return size;
    }

    /* no B area (yet). Check if we can fit in the remaining space */
    if (size < (me->size - me->a_end))
    {
        memcpy(me->data + me->a_end, data, size);
        me->a_end += size;
	return size;
    }

    /* didn't fit, activate B if possible */
    if (size > (me->a_start))
    {
	/* won't fit */
	return 0;
    }

    me->b_inuse = 1;
    memcpy(me->data, data, size);
    me->b_end += size;
    return size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant