General purpose ring buffer library. RTOS proof, mutex protected.
Dependents: RingBuffer_RTOS_test
Fork of RingBuffer by
Diff: RingBuffer.cpp
- Revision:
- 2:ea6d02ba96ae
- Parent:
- 1:1c3a10f2eb04
- Child:
- 3:9adf0d5e0522
--- a/RingBuffer.cpp Fri Aug 31 17:13:40 2012 +0000 +++ b/RingBuffer.cpp Thu Sep 27 13:15:54 2012 +0000 @@ -18,8 +18,7 @@ : bufsize(_bufsize) { buf = new unsigned char [bufsize+1]; - - sp = ep = (unsigned int)buf; + sp = ep = buf; memset(buf,0,bufsize); } @@ -32,15 +31,16 @@ RingBuffer::save(unsigned char c) { if( (ep==sp-1)|| - ((sp==(unsigned int)buf)&& - (ep==(unsigned int)buf+bufsize-1)) ) /* buffer full */ + ((sp==buf)&& + (ep==buf+bufsize-1)) ) /* buffer full */ return 0; - *(unsigned char*)ep = c; + *ep = c; ep++; - if(ep > (unsigned int)buf+bufsize) - ep = (unsigned int)buf; + if(ep > buf+bufsize) + ep = buf; + return 1; } @@ -52,12 +52,13 @@ if(sp == ep) return 0; /* buffer empty */ - ret = *(unsigned char*)sp; - *(unsigned char*)sp = 0; + ret = *sp; + *sp = 0; sp++; - if(sp> (unsigned int)buf+bufsize) - sp = (unsigned int)buf; + if(sp > buf+bufsize) + sp = buf; + return ret; }