Handle allocate/reallocate errors in ring_buf
Use is_some() in clear to simplify the clear loop.