NoPaste

fifo.c

von blan

SNIPPET_TEXT:
  1. fifo_t *fifo_new(uint8_t size, uint8_t fragment)
  2. {
  3.   if(size % fragment != 0)
  4.   {
  5.     return NULLPOINTER;
  6.   }
  7.  
  8.   fifo_t *fifo = (fifo_t*) malloc(sizeof(fifo_t));
  9.  
  10.   if(fifo == NULLPOINTER)
  11.   {
  12.     return NULLPOINTER;
  13.   }
  14.   else
  15.   {
  16.     fifo->size          = size;
  17.     fifo->fragment.size = fragment;
  18.  
  19.     fifo->data = (int8_t*) malloc(size + sizeof(NULLBYTE));
  20.  
  21.     if(fifo->data)
  22.     {
  23.       if(memset((int8_t*) fifo->data, NULLBYTE, fifo->size + sizeof(NULLBYTE)))
  24.       {
  25.         return fifo;
  26.       }
  27.       else
  28.       {
  29.         return NULLPOINTER;
  30.       }
  31.     }
  32.     else
  33.     {
  34.       return NULLPOINTER;
  35.     }
  36.   }
  37. }
  38.  
  39. int8_t fifo_push(volatile fifo_t *fifo, const int8_t *data)
  40. {
  41.   if(fifo == NULLPOINTER)
  42.   {
  43.     return 10;
  44.   }
  45.  
  46.   if(data == NULLPOINTER)
  47.   {
  48.     return 11;
  49.   }
  50.  
  51.   if(strlen(fifo->data) + strlen(data) > fifo->size)
  52.   {
  53.     return 12;
  54.   }
  55.   else
  56.   {
  57.     if(memcpy((int8_t*) fifo->data + strlen(fifo->data), (int8_t*) data, strlen(data)))
  58.     {
  59.       return FIFO_SUCCESS;
  60.     }
  61.     else
  62.     {
  63.       return 13;
  64.     }
  65.   }
  66. }
  67.  
  68. int8_t fifo_pop(volatile const fifo_t *fifo, int8_t *data)
  69. {
  70.   if(fifo == NULLPOINTER)
  71.   {
  72.     return FIFO_ERROR;
  73.   }
  74.  
  75.   if(data == NULLPOINTER)
  76.   {
  77.     return FIFO_ERROR;
  78.   }
  79.  
  80.   if(strlen(fifo->data) >= fifo->fragment.size)
  81.   {
  82.     if(memcpy((int8_t*) data, (int8_t*) fifo->data, fifo->fragment.size))
  83.     {
  84.       if(memcpy((int8_t*) fifo->data, (int8_t*) fifo->data + fifo->fragment.size, strlen(fifo->data) - fifo->fragment.size))
  85.       {
  86.         if(memset((int8_t*) fifo->data + (strlen(fifo->data) - fifo->fragment.size), NULLBYTE, fifo->size - strlen(fifo->data) - fifo->fragment.size))
  87.         {
  88.           return FIFO_SUCCESS;
  89.         }
  90.         else
  91.         {
  92.           return FIFO_ERROR;
  93.         }
  94.       }
  95.       else
  96.       {
  97.         return FIFO_ERROR;
  98.       }
  99.     }
  100.     else
  101.     {
  102.       return FIFO_ERROR;
  103.     }
  104.   }
  105.   else
  106.   {
  107.     return FIFO_ERROR;
  108.   }
  109. }
  110.  
  111. int8_t fifo_flush(volatile fifo_t *fifo)
  112. {
  113.   if(fifo == NULLPOINTER)
  114.   {
  115.     return FIFO_ERROR;
  116.   }
  117.  
  118.   if(memset((int8_t*) fifo->data, NULLBYTE, fifo->size))
  119.   {
  120.     return FIFO_SUCCESS;
  121.   }
  122.   else
  123.   {
  124.     return FIFO_ERROR;
  125.   }
  126. }
  127.  
  128. void fifo_destroy(volatile fifo_t *fifo)
  129. {
  130.   if(fifo != NULLPOINTER)
  131.   {
  132.     free((int8_t*) fifo->data);
  133.     free((fifo_t*) fifo);
  134.   }
  135. }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN