[lib/cbuf] [app/tests] fix off by one in lib/cbuf. add a test.
Previously, if tail was == 0, and we wrote exactly enough bytes to the end of the buffer, then head would end up at 0 as well. This would make the buffer instaneously empty, as head == tail.
This commit is contained in:
committed by
Travis Geiselbrecht
parent
a77295ae63
commit
61d06e19fc
@@ -84,8 +84,19 @@ size_t cbuf_write(cbuf_t *cbuf, const void *_buf, size_t len, bool canreschedule
|
||||
|
||||
while (pos < len && cbuf_space_avail(cbuf) > 0) {
|
||||
if (cbuf->head >= cbuf->tail) {
|
||||
write_len = MIN(valpow2(cbuf->len_pow2) - cbuf->head, len - pos);
|
||||
if (cbuf->tail == 0) {
|
||||
// Special case - if tail is at position 0, we can't write all
|
||||
// the way to the end of the buffer. Otherwise, head ends up at
|
||||
// 0, head == tail, and buffer is considered "empty" again.
|
||||
write_len =
|
||||
MIN(valpow2(cbuf->len_pow2) - cbuf->head - 1, len - pos);
|
||||
} else {
|
||||
// Write to the end of the buffer.
|
||||
write_len =
|
||||
MIN(valpow2(cbuf->len_pow2) - cbuf->head, len - pos);
|
||||
}
|
||||
} else {
|
||||
// Write from head to tail-1.
|
||||
write_len = MIN(cbuf->tail - cbuf->head - 1, len - pos);
|
||||
}
|
||||
|
||||
@@ -260,4 +271,3 @@ retry:
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ typedef struct cbuf {
|
||||
*
|
||||
* @param[in] cbuf A pointer to the cbuf structure to allocate.
|
||||
* @param[in] len The minimum number of bytes for the underlying data buffer.
|
||||
* Must be a power of two.
|
||||
*/
|
||||
void cbuf_initialize(cbuf_t *cbuf, size_t len);
|
||||
|
||||
@@ -56,7 +57,8 @@ void cbuf_initialize(cbuf_t *cbuf, size_t len);
|
||||
* Initialize a cbuf structure using the supplied buffer for internal storage.
|
||||
*
|
||||
* @param[in] cbuf A pointer to the cbuf structure to allocate.
|
||||
* @param[in] len The size of the supplied buffer, in bytes.
|
||||
* @param[in] len The size of the supplied buffer, in bytes. Must be a power
|
||||
* of two.
|
||||
* @param[in] buf A pointer to the memory to be used for internal storage.
|
||||
*/
|
||||
void cbuf_initialize_etc(cbuf_t *cbuf, size_t len, void *buf);
|
||||
|
||||
Reference in New Issue
Block a user