Azinix

circbuf.c

Go to the documentation of this file.
00001 /** \file circbuf.c
00002 
00003 \brief Circular buffer code
00004 
00005 
00006 ******************************************************************************/
00007 
00008 #include "circbuf.h"
00009 
00010 /**AutomaticStart*************************************************************/
00011 
00012 /*---------------------------------------------------------------------------*/
00013 /* Static function prototypes                                                */
00014 /*---------------------------------------------------------------------------*/
00015 
00016 /**AutomaticEnd***************************************************************/
00017 
00018 /*---------------------------------------------------------------------------*/
00019 /* Definition of exported functions                                          */
00020 /*---------------------------------------------------------------------------*/
00021 
00022 /**
00023   * \brief  Allocate a circular buffer with entries of size entrySize
00024   * 
00025   */
00026 
00027 Circbuf_t *
00028 Circbuf_DoAlloc (int entrySize, int N)
00029 {
00030   Circbuf_t *result = (Circbuf_t *) malloc (sizeof (Circbuf_t));
00031   result->N = N;
00032   result->numEntries = 0;
00033   result->entries = array_do_alloc (entrySize, N);
00034   result->head = 0;
00035   result->tail = 0;
00036   return result;
00037 }
00038 
00039 
00040 /**
00041   * \brief  Number of entries
00042   * 
00043   */
00044 
00045 int
00046 Circbuf_Num (Circbuf_t * cb)
00047 {
00048   int result;
00049   if (cb->head <= cb->tail)
00050     {
00051       result = (cb->tail - cb->head);
00052     }
00053   else
00054     {
00055       result = cb->N - (cb->head - cb->tail);
00056     }
00057   return result;
00058 }
00059 
00060 
00061 /**
00062   * \brief  Print the queue
00063   * 
00064   */
00065 
00066 int
00067 Circbuf_Print (Circbuf_t * cb)
00068 {
00069   printf
00070     ("cb->N = %d ; array_n( cb->entries ) = %d ; cb->head = %d ; cb->tail = %d\n",
00071      cb->N, array_n (cb->entries), cb->head, cb->tail);
00072   return 0;
00073 }
00074 
00075 
00076 /**
00077   * \brief  Dummy struct for testing out circbuf code
00078   * 
00079   */
00080 
00081 struct Circbuf_test_t
00082 {
00083   int a;
00084   char b;
00085 };
00086 
00087 typedef struct Circbuf_test_t Circbuf_test_t;
00088 
00089 
00090 /**
00091   * \brief  Test code for circular buffer
00092   * 
00093   */
00094 
00095 int
00096 Circbuf_test ()
00097 {
00098   Circbuf_t *aBuf;
00099   Circbuf_t *iBuf;
00100 
00101   const int N = 5;
00102   Circbuf_test_t A[N];
00103 
00104   int i;
00105   Circbuf_test_t datum;
00106   for (i = 0; i < N; i++)
00107     {
00108       A[i].a = i * i;
00109       A[i].b = 'a' + i;
00110     }
00111 
00112   aBuf = Circbuf_Init (Circbuf_test_t, N);
00113   iBuf = Circbuf_Init (int, N);
00114 
00115   Circbuf_Insert (int, iBuf, 0);
00116   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00117   Circbuf_Insert (int, iBuf, 1);
00118   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00119   Circbuf_Insert (int, iBuf, 2);
00120   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00121   Circbuf_Insert (int, iBuf, 3);
00122   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00123   Circbuf_Delete (int, iBuf);
00124   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00125   Circbuf_Delete (int, iBuf);
00126   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00127   Circbuf_Insert (int, iBuf, 0);
00128   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00129   Circbuf_Insert (int, iBuf, 1);
00130   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00131   Circbuf_Delete (int, iBuf);
00132   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00133   Circbuf_Insert (int, iBuf, 0);
00134   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00135   Circbuf_Insert (int, iBuf, -1);
00136   printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00137 
00138   for (i = 0; i < N; i++)
00139     {
00140       printf ("Entry = %d\n", Circbuf_Delete (int, iBuf));
00141       printf ("Circbuf size = %d\n", Circbuf_Num (iBuf));
00142     }
00143   return 0;
00144 
00145   for (i = 0; i < N; i++)
00146     {
00147       printf ("Insert index %d\n", i);
00148       fflush (stdout);
00149       Circbuf_Insert (Circbuf_test_t, aBuf, A[i]);
00150     }
00151   for (i = 0; i < N; i++)
00152     {
00153       printf ("Delete index %d\n", i);
00154       fflush (stdout);
00155       datum = Circbuf_Delete (Circbuf_test_t, aBuf);
00156       printf ("Datum = %d %c\n", datum.a, datum.b);
00157     }
00158   for (i = 0; i < N; i++)
00159     {
00160       printf ("Insert index %d\n", i);
00161       fflush (stdout);
00162       Circbuf_Insert (Circbuf_test_t, aBuf, A[i]);
00163     }
00164   for (i = 0; i < N; i++)
00165     {
00166       printf ("Delete index %d\n", i);
00167       fflush (stdout);
00168       datum = Circbuf_Delete (Circbuf_test_t, aBuf);
00169       printf ("Datum = %d %c\n", datum.a, datum.b);
00170     }
00171   return 0;
00172 }