fix dyn buffer

This commit is contained in:
2024-03-08 11:47:50 +01:00
parent 3b23e37949
commit db199dd343

View File

@@ -1,6 +1,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h>
#include "buffer.h" #include "buffer.h"
// implementation for a FIFO-buffer with ints // 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; static unsigned int FIFO_count = 0;
bool FIFO_Full = false; 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 buffer_put(int i)
{ {
bool ok = false; bool ok = false;
@@ -45,12 +33,13 @@ bool buffer_put(int i)
FIFO_FirstElement_p->nextElement = NULL; FIFO_FirstElement_p->nextElement = NULL;
FIFO_LastElement_p = FIFO_FirstElement_p; FIFO_LastElement_p = FIFO_FirstElement_p;
FIFO_count++; FIFO_count = 1;
ok = true; ok = true;
} }
else else
{ {
FIFO_Full = true; FIFO_Full = true;
ok = false;
} }
} }
else if ((FIFO_LastElement_p != NULL) && (FIFO_FirstElement_p != NULL)) else if ((FIFO_LastElement_p != NULL) && (FIFO_FirstElement_p != NULL))
@@ -61,6 +50,7 @@ bool buffer_put(int i)
{ {
el->value = i; el->value = i;
el->nextElement = NULL; el->nextElement = NULL;
FIFO_LastElement_p->nextElement = el;
FIFO_LastElement_p = el; FIFO_LastElement_p = el;
FIFO_count++; FIFO_count++;
ok = true; ok = true;
@@ -68,11 +58,16 @@ bool buffer_put(int i)
else else
{ {
FIFO_Full = true; FIFO_Full = true;
ok = false;
} }
} }
else else
{ {
// buffer is unhealthy. try to clear it and reinit // 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 //TODO: this while loop is unsave
while (FIFO_FirstElement_p != NULL) while (FIFO_FirstElement_p != NULL)
{ {
@@ -82,6 +77,7 @@ bool buffer_put(int i)
{ {
free(FIFO_LastElement_p); free(FIFO_LastElement_p);
FIFO_LastElement_p = NULL; FIFO_LastElement_p = NULL;
printf("buffer_dyn - buffer_put(): ERROR: last pointer is NULL\n");
} }
buffer_put(i); buffer_put(i);
ok = false; ok = false;
@@ -97,6 +93,7 @@ bool buffer_get(int *p)
*p = FIFO_FirstElement_p->value; *p = FIFO_FirstElement_p->value;
FIFO_element_t* next = FIFO_FirstElement_p->nextElement; FIFO_element_t* next = FIFO_FirstElement_p->nextElement;
free(FIFO_FirstElement_p); free(FIFO_FirstElement_p);
// FIFO_FirstElement_p = NULL;
FIFO_count--; FIFO_count--;
FIFO_Full = false; FIFO_Full = false;
if (next == NULL) if (next == NULL)
@@ -106,6 +103,7 @@ bool buffer_get(int *p)
{ {
// clear last element becouse it's the same // clear last element becouse it's the same
FIFO_LastElement_p = NULL; FIFO_LastElement_p = NULL;
FIFO_FirstElement_p = NULL;
FIFO_count = 0; FIFO_count = 0;
ok = true; ok = true;
} }
@@ -113,11 +111,16 @@ bool buffer_get(int *p)
{ {
// buffer chain is broken; skip to last element // buffer chain is broken; skip to last element
FIFO_FirstElement_p = FIFO_LastElement_p; 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 else
{ {
// somehow the last element is NULL // somehow the last element is NULL
FIFO_count = 0; FIFO_count = 0;
FIFO_FirstElement_p = NULL;
printf("buffer_dyn - buffer_get(): ERROR: last element pointer is NULL\n");
ok = false;
} }
} }
else else
@@ -141,6 +144,11 @@ bool buffer_is_full(void)
bool buffer_is_empty(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)); return ((FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL));
} }