Fixed a bug  in calculating ring buffer space available