[Libwebsockets] lws_ring_get_count_free_elements() question/remark

Andy Green andy at warmcat.com
Tue Mar 30 18:26:30 CEST 2021



On 3/30/21 5:16 PM, Olivier Langlois wrote:
> Hi,
> 
> If I read misc/lws_ring.c
> 
> I have come to the following observation. On a freshly created 100% 
> empty ring with a given COUNT,
> 
> lws_ring_get_count_free_elements() will return COUNT-1.
> 
> Is that correct?
> 
> I kinda understand that if head == tail, you cannot differentiate 
> between 100% full with 100% empty if you would allow the head to be 
> incremented to reach the same value of tail. Therefore, to avoid this 
> ambiguity, only the tail is allowed to make that move.
> 
> I see 2 ways that this could be resolved:

... there's another two ways, 3) just accept that's how that works, or 
4) just use some other implementation...

> 1. In lws_ring_create(), allocate an extra element so that an empty ring 
> does have the # of requested free elements
> 2. Maybe if struct ring had a waiting_elem count, this would remove this 
> ambiguity and would allow the code to not waste an element. By assuming 
> that the element size is significantly bigger than an extra int, this 
> added struct member would be justified memory wise.

What's the problem if you just tell it +1 size for the ringbuffer from 
user code?

-Andy



More information about the Libwebsockets mailing list