diff --git a/drivers/syslog/syslog_rpmsg.c b/drivers/syslog/syslog_rpmsg.c index 9d1e24426b..adb520503a 100644 --- a/drivers/syslog/syslog_rpmsg.c +++ b/drivers/syslog/syslog_rpmsg.c @@ -43,10 +43,10 @@ * Pre-processor definitions ****************************************************************************/ -#define SYSLOG_RPMSG_WORK_DELAY MSEC2TICK(CONFIG_SYSLOG_RPMSG_WORK_DELAY) +#define SYSLOG_RPMSG_WORK_DELAY MSEC2TICK(CONFIG_SYSLOG_RPMSG_WORK_DELAY) -#define SYSLOG_RPMSG_COUNT(h, t, size) (((h)>=(t)) ? (h)-(t) : (size)-((t)-(h))) -#define SYSLOG_RPMSG_SPACE(h, t, size) ((size) - 1 - SYSLOG_RPMSG_COUNT(h, t, size)) +#define SYSLOG_RPMSG_COUNT(p) ((p)->head - (p)->tail) +#define SYSLOG_RPMSG_SPACE(p) ((p)->size - 1 - SYSLOG_RPMSG_COUNT(p)) /**************************************************************************** * Private Types @@ -101,6 +101,7 @@ static void syslog_rpmsg_work(FAR void *priv_) irqstate_t flags; uint32_t space; size_t len; + size_t off; size_t len_end; if (is_rpmsg_ept_ready(&priv->ept)) @@ -120,8 +121,9 @@ static void syslog_rpmsg_work(FAR void *priv_) flags = enter_critical_section(); space -= sizeof(*msg); - len = SYSLOG_RPMSG_COUNT(priv->head, priv->tail, priv->size); - len_end = priv->size - priv->tail; + len = SYSLOG_RPMSG_COUNT(priv); + off = priv->tail % priv->size; + len_end = priv->size - off; if (len > space) { @@ -130,12 +132,12 @@ static void syslog_rpmsg_work(FAR void *priv_) if (len > len_end) { - memcpy(msg->data, &priv->buffer[priv->tail], len_end); + memcpy(msg->data, &priv->buffer[off], len_end); memcpy(msg->data + len_end, priv->buffer, len - len_end); } else { - memcpy(msg->data, &priv->buffer[priv->tail], len); + memcpy(msg->data, &priv->buffer[off], len); } priv->trans_len = len; @@ -156,12 +158,8 @@ static void syslog_rpmsg_putchar(FAR struct syslog_rpmsg_s *priv, int ch, while (1) { next = priv->head + 1; - if (next >= priv->size) - { - next = 0; - } - if (next == priv->tail) + if (next - priv->tail >= priv->size) { #ifndef CONFIG_SYSLOG_RPMSG_OVERWRITE if (!up_interrupt_context() && !sched_idletask()) @@ -173,14 +171,9 @@ static void syslog_rpmsg_putchar(FAR struct syslog_rpmsg_s *priv, int ch, { /* Overwrite */ - priv->buffer[priv->tail] = 0; + priv->buffer[priv->tail % priv->size] = 0; priv->tail += 1; - if (priv->tail >= priv->size) - { - priv->tail = 0; - } - if (priv->transfer) { priv->trans_len--; @@ -195,14 +188,14 @@ static void syslog_rpmsg_putchar(FAR struct syslog_rpmsg_s *priv, int ch, } } - priv->buffer[priv->head] = ch & 0xff; + priv->buffer[priv->head % priv->size] = ch & 0xff; priv->head = next; if (last && !priv->suspend && !priv->transfer && is_rpmsg_ept_ready(&priv->ept)) { clock_t delay = SYSLOG_RPMSG_WORK_DELAY; - size_t space = SYSLOG_RPMSG_SPACE(priv->head, priv->tail, priv->size); + size_t space = SYSLOG_RPMSG_SPACE(priv); /* Start work immediately when data more then 75% and meet '\n' */ @@ -271,29 +264,27 @@ static int syslog_rpmsg_ept_cb(FAR struct rpmsg_endpoint *ept, { irqstate_t flags; ssize_t len_end; + size_t off; int sval; flags = enter_critical_section(); if (priv->trans_len > 0) { - len_end = priv->size - priv->tail; + off = priv->tail % priv->size; + len_end = priv->size - off; if (priv->trans_len > len_end) { - memset(&priv->buffer[priv->tail], 0, len_end); + memset(&priv->buffer[off], 0, len_end); memset(priv->buffer, 0, priv->trans_len - len_end); } else { - memset(&priv->buffer[priv->tail], 0, priv->trans_len); + memset(&priv->buffer[off], 0, priv->trans_len); } priv->tail += priv->trans_len; - if (priv->tail >= priv->size) - { - priv->tail -= priv->size; - } nxsem_get_value(&priv->sem, &sval); while (sval++ < 0) @@ -304,7 +295,7 @@ static int syslog_rpmsg_ept_cb(FAR struct rpmsg_endpoint *ept, priv->transfer = false; - if (SYSLOG_RPMSG_COUNT(priv->head, priv->tail, priv->size)) + if (SYSLOG_RPMSG_COUNT(priv)) { work_queue(HPWORK, &priv->work, syslog_rpmsg_work, priv, 0); }