syslog_rpmsg: change head & tail NON-wrap

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd 2021-12-14 14:59:20 +08:00 committed by Xiang Xiao
parent f5665db1f2
commit 61cb79d192

View File

@ -43,10 +43,10 @@
* Pre-processor definitions * 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_COUNT(p) ((p)->head - (p)->tail)
#define SYSLOG_RPMSG_SPACE(h, t, size) ((size) - 1 - SYSLOG_RPMSG_COUNT(h, t, size)) #define SYSLOG_RPMSG_SPACE(p) ((p)->size - 1 - SYSLOG_RPMSG_COUNT(p))
/**************************************************************************** /****************************************************************************
* Private Types * Private Types
@ -101,6 +101,7 @@ static void syslog_rpmsg_work(FAR void *priv_)
irqstate_t flags; irqstate_t flags;
uint32_t space; uint32_t space;
size_t len; size_t len;
size_t off;
size_t len_end; size_t len_end;
if (is_rpmsg_ept_ready(&priv->ept)) if (is_rpmsg_ept_ready(&priv->ept))
@ -120,8 +121,9 @@ static void syslog_rpmsg_work(FAR void *priv_)
flags = enter_critical_section(); flags = enter_critical_section();
space -= sizeof(*msg); space -= sizeof(*msg);
len = SYSLOG_RPMSG_COUNT(priv->head, priv->tail, priv->size); len = SYSLOG_RPMSG_COUNT(priv);
len_end = priv->size - priv->tail; off = priv->tail % priv->size;
len_end = priv->size - off;
if (len > space) if (len > space)
{ {
@ -130,12 +132,12 @@ static void syslog_rpmsg_work(FAR void *priv_)
if (len > len_end) 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); memcpy(msg->data + len_end, priv->buffer, len - len_end);
} }
else else
{ {
memcpy(msg->data, &priv->buffer[priv->tail], len); memcpy(msg->data, &priv->buffer[off], len);
} }
priv->trans_len = len; priv->trans_len = len;
@ -156,12 +158,8 @@ static void syslog_rpmsg_putchar(FAR struct syslog_rpmsg_s *priv, int ch,
while (1) while (1)
{ {
next = priv->head + 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 #ifndef CONFIG_SYSLOG_RPMSG_OVERWRITE
if (!up_interrupt_context() && !sched_idletask()) if (!up_interrupt_context() && !sched_idletask())
@ -173,14 +171,9 @@ static void syslog_rpmsg_putchar(FAR struct syslog_rpmsg_s *priv, int ch,
{ {
/* Overwrite */ /* Overwrite */
priv->buffer[priv->tail] = 0; priv->buffer[priv->tail % priv->size] = 0;
priv->tail += 1; priv->tail += 1;
if (priv->tail >= priv->size)
{
priv->tail = 0;
}
if (priv->transfer) if (priv->transfer)
{ {
priv->trans_len--; 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; priv->head = next;
if (last && !priv->suspend && !priv->transfer && if (last && !priv->suspend && !priv->transfer &&
is_rpmsg_ept_ready(&priv->ept)) is_rpmsg_ept_ready(&priv->ept))
{ {
clock_t delay = SYSLOG_RPMSG_WORK_DELAY; 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' */ /* 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; irqstate_t flags;
ssize_t len_end; ssize_t len_end;
size_t off;
int sval; int sval;
flags = enter_critical_section(); flags = enter_critical_section();
if (priv->trans_len > 0) 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) 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); memset(priv->buffer, 0, priv->trans_len - len_end);
} }
else else
{ {
memset(&priv->buffer[priv->tail], 0, priv->trans_len); memset(&priv->buffer[off], 0, priv->trans_len);
} }
priv->tail += priv->trans_len; priv->tail += priv->trans_len;
if (priv->tail >= priv->size)
{
priv->tail -= priv->size;
}
nxsem_get_value(&priv->sem, &sval); nxsem_get_value(&priv->sem, &sval);
while (sval++ < 0) while (sval++ < 0)
@ -304,7 +295,7 @@ static int syslog_rpmsg_ept_cb(FAR struct rpmsg_endpoint *ept,
priv->transfer = false; 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); work_queue(HPWORK, &priv->work, syslog_rpmsg_work, priv, 0);
} }