General purpose ring buffer library

Dependents:   iSerial DGWWebServer iSerial Dumb_box_rev2 ... more

Files at this revision

API Documentation at this revision

Comitter:
ykuroda
Date:
Thu Sep 27 13:15:54 2012 +0000
Parent:
1:1c3a10f2eb04
Commit message:
pointer edition

Changed in this revision

RingBuffer.cpp Show annotated file Show diff for this revision Revisions of this file
RingBuffer.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
 }
 
--- a/RingBuffer.h	Fri Aug 31 17:13:40 2012 +0000
+++ b/RingBuffer.h	Thu Sep 27 13:15:54 2012 +0000
@@ -14,8 +14,10 @@
 
   protected:
     unsigned char* buf;
-    unsigned int sp;
-    unsigned int ep;
+    unsigned char* sp;
+    unsigned char* ep;
+
+
     int bufsize;
 
   public: