remove compound statements

This commit is contained in:
Wirtos_new 2021-06-19 18:06:43 +03:00
parent 7343b233e4
commit de58dc21e5
2 changed files with 39 additions and 43 deletions

View file

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

View file

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