note: optimize noteram_add, copy as much content as possible at a time

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2023-03-02 16:26:19 +08:00 committed by Xiang Xiao
parent 081b04f05e
commit 6e35a51feb

View File

@ -567,7 +567,8 @@ static void noteram_add(FAR struct note_driver_s *drv,
{ {
FAR const char *buf = note; FAR const char *buf = note;
unsigned int head; unsigned int head;
unsigned int next; unsigned int remain;
unsigned int space;
irqstate_t flags; irqstate_t flags;
flags = spin_lock_irqsave_wo_note(&g_noteram_lock); flags = spin_lock_irqsave_wo_note(&g_noteram_lock);
@ -578,46 +579,37 @@ static void noteram_add(FAR struct note_driver_s *drv,
return; return;
} }
/* Get the index to the head of the circular buffer */
DEBUGASSERT(note != NULL && notelen < CONFIG_DRIVERS_NOTERAM_BUFSIZE); 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 */ if (remain < notelen)
while (notelen > 0)
{ {
/* Get the next head index. Would it collide with the current tail if (g_noteram_info.ni_overwrite == NOTERAM_MODE_OVERWRITE_DISABLE)
* index?
*/
next = noteram_next(head, 1);
if (next == g_noteram_info.ni_tail)
{ {
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; g_noteram_info.ni_overwrite = NOTERAM_MODE_OVERWRITE_OVERFLOW;
spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags); spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags);
return; return;
}
/* Yes, then remove the note at the tail index */
noteram_remove();
} }
/* 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++; do
{
head = next; noteram_remove();
notelen--; 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); spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags);
} }