This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 6e35a51feb note: optimize noteram_add, copy as much content as 
possible at a time
6e35a51feb is described below

commit 6e35a51feb09cdafaa8c898ae0fc8b381088092b
Author: yinshengkai <[email protected]>
AuthorDate: Thu Mar 2 16:26:19 2023 +0800

    note: optimize noteram_add, copy as much content as possible at a time
    
    Signed-off-by: yinshengkai <[email protected]>
---
 drivers/note/noteram_driver.c | 52 ++++++++++++++++++-------------------------
 1 file changed, 22 insertions(+), 30 deletions(-)

diff --git a/drivers/note/noteram_driver.c b/drivers/note/noteram_driver.c
index 39445399ba..30454a3826 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;
-            }
+          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 */
+      /* Remove the note at the tail index , make sure there is enough space
+       */
 
+      do
+        {
           noteram_remove();
+          remain = CONFIG_DRIVERS_NOTERAM_BUFSIZE - noteram_length();
         }
-
-      /* Save the next byte at the head index */
-
-      g_noteram_info.ni_buffer[head] = *buf++;
-
-      head = next;
-      notelen--;
+      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);
 }
 

Reply via email to