fix dyn buffer
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "buffer.h"
|
||||
|
||||
// implementation for a FIFO-buffer with ints
|
||||
@@ -18,20 +20,6 @@ static FIFO_element_t* FIFO_LastElement_p = NULL;
|
||||
static unsigned int FIFO_count = 0;
|
||||
bool FIFO_Full = false;
|
||||
|
||||
/** incrementPointer
|
||||
*
|
||||
* add one and rotates if it overflows the buffersize
|
||||
*/
|
||||
unsigned int incrementPointer(unsigned int p)
|
||||
{
|
||||
p++;
|
||||
if (p == FIFO_SIZE)
|
||||
{
|
||||
p = 0;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
bool buffer_put(int i)
|
||||
{
|
||||
bool ok = false;
|
||||
@@ -45,12 +33,13 @@ bool buffer_put(int i)
|
||||
FIFO_FirstElement_p->nextElement = NULL;
|
||||
|
||||
FIFO_LastElement_p = FIFO_FirstElement_p;
|
||||
FIFO_count++;
|
||||
FIFO_count = 1;
|
||||
ok = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
FIFO_Full = true;
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
else if ((FIFO_LastElement_p != NULL) && (FIFO_FirstElement_p != NULL))
|
||||
@@ -61,6 +50,7 @@ bool buffer_put(int i)
|
||||
{
|
||||
el->value = i;
|
||||
el->nextElement = NULL;
|
||||
FIFO_LastElement_p->nextElement = el;
|
||||
FIFO_LastElement_p = el;
|
||||
FIFO_count++;
|
||||
ok = true;
|
||||
@@ -68,11 +58,16 @@ bool buffer_put(int i)
|
||||
else
|
||||
{
|
||||
FIFO_Full = true;
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// buffer is unhealthy. try to clear it and reinit
|
||||
if (FIFO_FirstElement_p == NULL)
|
||||
{
|
||||
printf("buffer_dyn - buffer_put(): ERROR: first pointer is NULL\n");
|
||||
}
|
||||
//TODO: this while loop is unsave
|
||||
while (FIFO_FirstElement_p != NULL)
|
||||
{
|
||||
@@ -82,6 +77,7 @@ bool buffer_put(int i)
|
||||
{
|
||||
free(FIFO_LastElement_p);
|
||||
FIFO_LastElement_p = NULL;
|
||||
printf("buffer_dyn - buffer_put(): ERROR: last pointer is NULL\n");
|
||||
}
|
||||
buffer_put(i);
|
||||
ok = false;
|
||||
@@ -97,6 +93,7 @@ bool buffer_get(int *p)
|
||||
*p = FIFO_FirstElement_p->value;
|
||||
FIFO_element_t* next = FIFO_FirstElement_p->nextElement;
|
||||
free(FIFO_FirstElement_p);
|
||||
// FIFO_FirstElement_p = NULL;
|
||||
FIFO_count--;
|
||||
FIFO_Full = false;
|
||||
if (next == NULL)
|
||||
@@ -106,6 +103,7 @@ bool buffer_get(int *p)
|
||||
{
|
||||
// clear last element becouse it's the same
|
||||
FIFO_LastElement_p = NULL;
|
||||
FIFO_FirstElement_p = NULL;
|
||||
FIFO_count = 0;
|
||||
ok = true;
|
||||
}
|
||||
@@ -113,11 +111,16 @@ bool buffer_get(int *p)
|
||||
{
|
||||
// buffer chain is broken; skip to last element
|
||||
FIFO_FirstElement_p = FIFO_LastElement_p;
|
||||
printf("buffer_dyn - buffer_get(): ERROR: next element is NULL, but it's not the last element. skip to last element\n");
|
||||
ok = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// somehow the last element is NULL
|
||||
FIFO_count = 0;
|
||||
FIFO_FirstElement_p = NULL;
|
||||
printf("buffer_dyn - buffer_get(): ERROR: last element pointer is NULL\n");
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -141,6 +144,11 @@ bool buffer_is_full(void)
|
||||
|
||||
bool buffer_is_empty(void)
|
||||
{
|
||||
// printf("empty %s: %s; FIFO_LastElement_p = %i; FIFO_FirstElement_p = %i\n",
|
||||
// (FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL) ? "true" : "false",
|
||||
// (FIFO_LastElement_p == FIFO_FirstElement_p) ? "same" : "diff",
|
||||
// (int) FIFO_LastElement_p,
|
||||
// (int) FIFO_FirstElement_p);
|
||||
return ((FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user