Description
This is Issue 1078 moved from a Google Code project.
Added by 2012-10-21T15:57:40.000Z by KevinCat...@gmail.com.
Please review that bug for more context and additional comments, but update this bug.
Original labels: Type-Defect, Priority-Medium
Original description
In HardwareSerial.cpp the ring buffer is defined as
struct ring_buffer
{
unsigned char buffer[SERIAL_BUFFER_SIZE];
volatile unsigned int head;
volatile unsigned int tail;
};
The head and tail variables should be redefined as uint8_t since the value is always between 0 and 64 (inclusive). This saves 2 bytes of SRAM per buffer.
Also, the current code looks like it has a subtle bug. Reading head and tail from outside the ISR is not atomic since they are 16 bit, yet the code does not disable interrupts to ensure atomicity. In general that could result in reading a value that the variable never actually had.
It turns out though that in this case the high byte will always be 0, and there is no hidden bug. Nevertheless some people have been confused by this[0]. Changing to uint8_t eliminates this confusion, while also saving space.
Footnote:
[0] For an example of people thinking there may be a bug here, see http://bleaklow.com/2012/02/29/why_im_ditching_the_arduino_software_platform.htm