From 6e35a51feb09cdafaa8c898ae0fc8b381088092b Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Thu, 2 Mar 2023 16:26:19 +0800 Subject: [PATCH] note: optimize noteram_add, copy as much content as possible at a time Signed-off-by: yinshengkai --- drivers/note/noteram_driver.c | 54 +++++++++++++++-------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/drivers/note/noteram_driver.c b/drivers/note/noteram_driver.c index 39445399ba7..30454a3826f 100644 --- a/drivers/note/noteram_driver.c +++ b/drivers/note/noteram_driver.c @@ -567,7 +567,8 @@ static void noteram_add(FAR struct note_driver_s *drv, { FAR const char *buf = note; unsigned int head; - unsigned int next; + unsigned int remain; + unsigned int space; irqstate_t flags; flags = spin_lock_irqsave_wo_note(&g_noteram_lock); @@ -578,46 +579,37 @@ static void noteram_add(FAR struct note_driver_s *drv, return; } - /* Get the index to the head of the circular buffer */ - DEBUGASSERT(note != NULL && notelen < CONFIG_DRIVERS_NOTERAM_BUFSIZE); - head = g_noteram_info.ni_head; + remain = CONFIG_DRIVERS_NOTERAM_BUFSIZE - noteram_length(); - /* Loop until all bytes have been transferred to the circular buffer */ - - while (notelen > 0) + if (remain < notelen) { - /* Get the next head index. Would it collide with the current tail - * index? - */ - - next = noteram_next(head, 1); - if (next == g_noteram_info.ni_tail) + if (g_noteram_info.ni_overwrite == NOTERAM_MODE_OVERWRITE_DISABLE) { - if (g_noteram_info.ni_overwrite == NOTERAM_MODE_OVERWRITE_DISABLE) - { - /* Stop recording if not in overwrite mode */ + /* Stop recording if not in overwrite mode */ - g_noteram_info.ni_overwrite = NOTERAM_MODE_OVERWRITE_OVERFLOW; - spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags); - return; - } - - /* Yes, then remove the note at the tail index */ - - noteram_remove(); + g_noteram_info.ni_overwrite = NOTERAM_MODE_OVERWRITE_OVERFLOW; + spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags); + return; } - /* Save the next byte at the head index */ + /* Remove the note at the tail index , make sure there is enough space + */ - g_noteram_info.ni_buffer[head] = *buf++; - - head = next; - notelen--; + do + { + noteram_remove(); + remain = CONFIG_DRIVERS_NOTERAM_BUFSIZE - noteram_length(); + } + while (remain < notelen); } - g_noteram_info.ni_head = head; - + head = g_noteram_info.ni_head; + space = CONFIG_DRIVERS_NOTERAM_BUFSIZE - head; + space = space < notelen ? space : notelen; + memcpy(g_noteram_info.ni_buffer + head, note, space); + memcpy(g_noteram_info.ni_buffer, buf + space, notelen - space); + g_noteram_info.ni_head = noteram_next(head, notelen); spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags); }