This commit is contained in:
Wirtos_new 2021-07-08 08:30:07 +02:00 committed by GitHub
commit 5abc1dc3cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 43 deletions

View file

@ -2,11 +2,6 @@
#ifndef CBUF_H #ifndef CBUF_H
#define CBUF_H #define CBUF_H
#include "common.h"
#include <stdbool.h>
#include <unistd.h>
// To define a circular buffer type of 20 ints: // To define a circular buffer type of 20 ints:
// struct cbuf_int CBUF(int, 20); // struct cbuf_int CBUF(int, 20);
// //
@ -30,23 +25,25 @@
(void) ((PCBUF)->head = (PCBUF)->tail = 0) (void) ((PCBUF)->head = (PCBUF)->tail = 0)
#define cbuf_push(PCBUF, ITEM) \ #define cbuf_push(PCBUF, ITEM) \
({ \ ( \
bool ok = !cbuf_is_full(PCBUF); \ (!cbuf_is_full(PCBUF)) \
if (ok) { \ ? ( \
(PCBUF)->data[(PCBUF)->head] = (ITEM); \ (PCBUF)->data[(PCBUF)->head] = (ITEM), \
(PCBUF)->head = ((PCBUF)->head + 1) % cbuf_size_(PCBUF); \ (PCBUF)->head = ((PCBUF)->head + 1) % cbuf_size_(PCBUF), \
} \ 1 \
ok; \ ) \
}) : 0 \
)
#define cbuf_take(PCBUF, PITEM) \ #define cbuf_take(PCBUF, PITEM) \
({ \ ( \
bool ok = !cbuf_is_empty(PCBUF); \ (!cbuf_is_empty(PCBUF)) \
if (ok) { \ ? ( \
*(PITEM) = (PCBUF)->data[(PCBUF)->tail]; \ *(PITEM) = (PCBUF)->data[(PCBUF)->tail], \
(PCBUF)->tail = ((PCBUF)->tail + 1) % cbuf_size_(PCBUF); \ (PCBUF)->tail = ((PCBUF)->tail + 1) % cbuf_size_(PCBUF), \
} \ 1 \
ok; \ ) \
}) : 0 \
)
#endif #endif

View file

@ -2,8 +2,6 @@
#ifndef QUEUE_H #ifndef QUEUE_H
#define QUEUE_H #define QUEUE_H
#include "common.h"
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@ -51,26 +49,27 @@
// push a new item into the queue // push a new item into the queue
#define queue_push(PQ, NEXTFIELD, ITEM) \ #define queue_push(PQ, NEXTFIELD, ITEM) \
(void) ({ \ (void) ( \
(ITEM)->NEXTFIELD = NULL; \ (ITEM)->NEXTFIELD = NULL, \
if (queue_is_empty(PQ)) { \ (queue_is_empty(PQ)) \
(PQ)->first = (PQ)->last = (ITEM); \ ? (PQ)->first = (PQ)->last = (ITEM) \
} else { \ : ( \
(PQ)->last->NEXTFIELD = (ITEM); \ (PQ)->last->NEXTFIELD = (ITEM), \
(PQ)->last = (ITEM); \ (PQ)->last = (ITEM) \
} \ ) \
}) \
)
// take the next item and remove it from the queue (the queue must not be empty) // take the next item and remove it from the queue (the queue must not be empty)
// the result is stored in *(PITEM) // the result is stored in *(PITEM)
// (without typeof(), we could not store a local variable having the correct // (without typeof(), we could not store a local variable having the correct
// type so that we can "return" it) // type so that we can "return" it)
#define queue_take(PQ, NEXTFIELD, PITEM) \ #define queue_take(PQ, NEXTFIELD, PITEM) \
(void) ({ \ (void) ( \
assert(!queue_is_empty(PQ)); \ assert(!queue_is_empty(PQ)), \
*(PITEM) = (PQ)->first; \ *(PITEM) = (PQ)->first, \
(PQ)->first = (PQ)->first->NEXTFIELD; \ (PQ)->first = (PQ)->first->NEXTFIELD \
}) )
// no need to update (PQ)->last if the queue is left empty: // no need to update (PQ)->last if the queue is left empty:
// (PQ)->last is undefined if !(PQ)->first anyway // (PQ)->last is undefined if !(PQ)->first anyway