-
Notifications
You must be signed in to change notification settings - Fork 153
Description
I see this getSize implementation has gone through a few iterations based on commit history. It seems to me there's still a bug.
modm/src/modm/platform/uart/rtt/rtt.cpp.in
Lines 47 to 53 in 3fbe3a1
bool isEmpty() const { return (head == tail); } | |
uint32_t getSize() const | |
{ | |
const uint32_t rhead{head}; | |
const uint32_t rtail{tail}; | |
return ((rhead > rtail) ? 0 : size) + rhead - rtail; | |
} |
On construction:
- head = 0
- tail = 0
- getSize() returns
MAX + 0 - 0 = MAX
After writing one byte:
- head = 1
- tail = 0
- getSize() returns
0 + 1 - 0 = 1
My best interpretation for the intent of this method is to return the number of occupied bytes. If so, the behavior for an empty buffer is incorrect.
I think the implementation in the submitted version of #610 is correct, with one caveat. If head == tail
, it is ambiguous whether the buffer is full or empty. I suspect baa88d2 was attempting to work around this limitation. I think the change moves the bug but doesn't fix it.
I am not sure what the RTT format expects for boundary conditions. One entry must be reserved to disambiguate unless there's additional state (e.g. entry count). The original implementation works so long as one entry is reserved. If that's the expected behavior, isEmpty() and write() would need to be updated to match.