Main Page | Class List | File List | Class Members | File Members

Buffer.c

Go to the documentation of this file.
00001 /*
00002  * Routines for manipulating buffers.  These buffers are going to be circular.
00003  * 
00004  */
00005 #include "Buffer.h"
00006 
00007 
00008 Buffer *mkbuffer(int n, size_t len)
00009 /*
00010  * mkbuffer creates a buffer for n records of length len and returns a pointer
00011  * to the created buffer.
00012  * Returns NULL if there is a problem.
00013  */ 
00014 {
00015   Buffer *r;
00016 
00017   r = (Buffer *) malloc(sizeof(Buffer));
00018   if (r == NULL) 
00019     return NULL;
00020   
00021   r->data = (void *) malloc(n * len);
00022   if (r->data == NULL) {
00023     free(r);
00024     return NULL;
00025   }
00026   memset(r->data, 0, n * len);
00027   r->head=0;
00028   r->tail=0;
00029   r->full=0;
00030   r->n=n;
00031   r->len=len;
00032   return r;
00033 }
00034 
00035 
00036 int bsize(Buffer *b)
00037 /*
00038  * returns the total size of the buffer (n)
00039  */
00040 {
00041   return b->n;
00042 }
00043 
00044 
00045 int bhead(Buffer *b)
00046 /*
00047  * returns the position of the buffer's head 
00048  */
00049 {
00050   return b->head;
00051 }
00052 
00053 
00054 int btail(Buffer *b)
00055 /*
00056  * returns the position of the buffer's tail
00057  */
00058 {
00059   return b->tail;
00060 }
00061 
00062 
00063 void bwrite(Buffer *b, void *data)
00064 /* 
00065  * writes a record from *data to the buffer
00066  *
00067  * returns void because it can never fail.
00068  */
00069 {
00070   int offset = b->head*b->len;
00071   memcpy(b->data+offset, data, b->len);
00072 
00073   if ((b->head == b->tail) && b->full) {
00074     b->tail++;
00075     if (b->tail==b->n)
00076       b->tail=0;
00077   }
00078   b->head++;
00079 
00080   if (b->head == b->n)
00081     b->head=0;
00082   if (b->head == b->tail)
00083     b->full=1;
00084 }
00085 
00086 
00087 int bread(Buffer *b, void *data)
00088 /* 
00089  * copies a record from the buffer to *data
00090  * 
00091  * returns:
00092  *   1 = ok
00093  *   0 = failed
00094  */
00095 {
00096   if ((b->head==b->tail) && !(b->full))
00097     return 0;
00098 
00099   memcpy(data, b->data + b->tail*b->len, b->len);
00100 
00101   b->tail++;
00102   if (b->tail==b->n)
00103     b->tail=0;
00104 
00105   b->full=0;
00106 
00107   return 1;
00108 }
00109 
00110 
00111 int blevel(Buffer *b)
00112 /*
00113  * returns the number of records in the buffer
00114  */
00115 {
00116   if (b->head == b->tail) {
00117     if (b->full)
00118       return b->n;
00119     else
00120       return 0;
00121   }
00122   if (b->head > b->tail)
00123     return (b->head - b->tail);
00124   if (b->head < b->tail)
00125     return (b->n + b->head - b->tail);
00126 }
00127 
00128 
00129 void *bpeek(Buffer *b, int n)
00130 /*
00131  * bpeek returns a pointer to the nth record in the buffer, or NULL if 
00132  * it there is no nth element.
00133  */
00134 {
00135   if (n<0)
00136     return NULL;
00137 
00138   if (blevel(b) < n+1)
00139     return NULL;
00140 
00141   return (b->data + ((b->tail + n) % b->n) * b->len);
00142 }
00143 
00144 
00145 void closebuffer(Buffer *b)
00146 /*
00147  * frees up memory used by buffer b
00148  */
00149 {
00150   free(b->data);
00151   free(b);
00152 }

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1