Skip to content

Commit

Permalink
fix: cutom memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
EDB02 committed Nov 2, 2023
1 parent 42f7e87 commit f5d1ade
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
10 changes: 6 additions & 4 deletions generic-queue/generic_queue.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "generic_queue.h"

void GENQ_init(generic_queue_t *q, size_t elem_size)
void GENQ_init(generic_queue_t *q, size_t size, size_t elem_size, uint8_t *queue)
{
q->size = size;
q->queue_elem_size = elem_size;
q->tail = q->head = q->cnt_elems = 0;
q->max_elems = GENQ_SIZE/q->queue_elem_size;
q->max_elems = q->size/q->queue_elem_size;
q->queue = queue;
}
bool GENQ_is_empty(generic_queue_t *q)
{
Expand All @@ -14,7 +16,7 @@ bool GENQ_is_full(generic_queue_t *q)
{
return q->cnt_elems >= q->max_elems;
}
bool GENQ_pop(generic_queue_t *q, void *e)
bool GENQ_pop(generic_queue_t *q, uint8_t *e)
{
if(GENQ_is_empty(q)) return 0;
q->cnt_elems--;
Expand All @@ -24,7 +26,7 @@ bool GENQ_pop(generic_queue_t *q, void *e)
q->head %= q->max_elems;
return 1;
}
bool GENQ_push(generic_queue_t *q, void *e)
bool GENQ_push(generic_queue_t *q, uint8_t *e)
{
if(GENQ_is_full(q)) return 0;
q->cnt_elems++;
Expand Down
22 changes: 14 additions & 8 deletions generic-queue/generic_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
#include "stdlib.h"
#include "string.h"

#define GENQ_SIZE 1000

typedef struct{
size_t head, tail;
uint8_t queue[GENQ_SIZE];
size_t size;
size_t queue_elem_size;
size_t max_elems;
int cnt_elems;
uint8_t *queue;
} generic_queue_t;

void GENQ_init(generic_queue_t *, size_t);
bool GENQ_is_empty(generic_queue_t*);
bool GENQ_is_full(generic_queue_t*);
bool GENQ_pop(generic_queue_t*, void *);
bool GENQ_push(generic_queue_t*, void *);
/**
*
* @param q pointer to the queue
* @param size size of the allocated memory in bytes
* @param elem_size max size of the single element of the queue
* @param queue pointer to the allocated memory for the queue
*/
void GENQ_init(generic_queue_t *q, size_t size, size_t elem_size, uint8_t *queue);
bool GENQ_is_empty(generic_queue_t *q);
bool GENQ_is_full(generic_queue_t *q);
bool GENQ_pop(generic_queue_t *q, uint8_t *e);
bool GENQ_push(generic_queue_t*, uint8_t *e);

#endif
16 changes: 10 additions & 6 deletions generic-queue/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ typedef struct{

int main()
{
GENQ_init(&q, sizeof(test));
int tot = 30;

uint8_t *mem = (uint8_t*)malloc(sizeof(test)*tot);
GENQ_init(&q, sizeof(test)*tot, sizeof(test), mem);

test v[n];
char prova[] = "asdaa";
Expand All @@ -21,11 +24,10 @@ int main()
v[i].v = i+1;
memcpy(v[i].prova, prova, sizeof(char)*5);
}
int tot = GENQ_SIZE / sizeof(test);
int cnt = 0;
for(int i=0;i<n;i++)
{
if(!GENQ_push(&q, v+i))
if(!GENQ_push(&q, (uint8_t*)(v+i)))
{
//printf("errore push\n");
break;
Expand All @@ -38,7 +40,7 @@ int main()
for(int i=0;i<10;i++)
{
test tmp;
if(!GENQ_pop(&q, &tmp))
if(!GENQ_pop(&q, (uint8_t*)(&tmp)) && tmp.v == i+1)
{
printf("error\n");
return 11;
Expand All @@ -47,7 +49,7 @@ int main()
}
for(int i=0;i<n;i++)
{
if(!GENQ_push(&q, v+i))
if(!GENQ_push(&q, (uint8_t*)(v+i)))
{
//printf("errore push\n");
break;
Expand All @@ -60,7 +62,7 @@ int main()
for(int i=0;i<n;i++)
{
test tmp;
if(GENQ_pop(&q, &tmp))
if(GENQ_pop(&q, (uint8_t*)(&tmp)) && tmp.v == i+11)
{
//printf("%d %s\n", tmp.v, tmp.prova);
cnt++;
Expand All @@ -75,5 +77,7 @@ int main()
printf("maximum elements: %d\n", tot);
if(check) printf("test passed\n");
else printf("test not passed\n");

free(mem);
//printf("popped %d elements\n", cnt);
}

0 comments on commit f5d1ade

Please sign in to comment.