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

xiaoxiang781216 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 84abb4756d3 libc/machine: add an option to not inline the atomic 
functions
84abb4756d3 is described below

commit 84abb4756d37c1f5c229dc7a54aaa249edf7b3f3
Author: zhangyuan29 <[email protected]>
AuthorDate: Thu Nov 19 17:26:08 2026 +0800

    libc/machine: add an option to not inline the atomic functions
    
    With LIBC_ATOMIC_IRQ or LIBC_ATOMIC_HWSPINLOCK, each atomic function
    is an always-inline function that disables the interrupts (or takes
    the hardware spinlock) around the access.  Each use expands to this
    sequence, also atomic_read() and atomic_set().  On armv6-m that is
    about 14 bytes at each use, against a few bytes for a call.
    
    Add LIBC_ATOMIC_NOINLINE.  With it, atomic.h only declares the atomic
    functions, and arch_atomic.c builds them once in libc.  KASAN calls
    the atomic functions itself, so they are built without KASAN and
    function instrumentation.  The option is off by default, so the
    existing configurations keep the inline functions.
    
    Signed-off-by: zhangyuan29 <[email protected]>
---
 include/nuttx/atomic.h          | 10 ++++++++++
 include/nuttx/lib/arch_atomic.h | 13 ++++++++++++-
 libs/libc/machine/Kconfig       | 13 +++++++++++++
 libs/libc/machine/arch_atomic.c |  1 +
 4 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/include/nuttx/atomic.h b/include/nuttx/atomic.h
index cff33d07f1e..13d6d2713d7 100644
--- a/include/nuttx/atomic.h
+++ b/include/nuttx/atomic.h
@@ -406,6 +406,16 @@ ARCH_SYNC_DECLARE(sync, uint16_t, 2)
 ARCH_ATOMIC_DECLARE(atomic, int64_t, 8)
 ARCH_SYNC_DECLARE(sync, uint64_t, 8)
 #  endif
+#elif defined(CONFIG_LIBC_ATOMIC_NOINLINE)
+ARCH_ATOMIC_DECLARE(atomic, uint8_t, 1)
+ARCH_ATOMIC_DECLARE(atomic, uint16_t, 2)
+ARCH_ATOMIC_DECLARE(atomic, int32_t, 4)
+ARCH_ATOMIC_DECLARE(atomic, int64_t, 8)
+
+ARCH_SYNC_DECLARE(sync, uint8_t, 1)
+ARCH_SYNC_DECLARE(sync, uint16_t, 2)
+ARCH_SYNC_DECLARE(sync, uint32_t, 4)
+ARCH_SYNC_DECLARE(sync, uint64_t, 8)
 #endif
 
 #if defined(__cplusplus)
diff --git a/include/nuttx/lib/arch_atomic.h b/include/nuttx/lib/arch_atomic.h
index 1368e8840cc..58a8030832f 100644
--- a/include/nuttx/lib/arch_atomic.h
+++ b/include/nuttx/lib/arch_atomic.h
@@ -45,7 +45,15 @@
  ****************************************************************************/
 
 #ifndef ARCH_ATOMIC_SPECIFIER
-#  define ARCH_ATOMIC_SPECIFIER static always_inline_function
+#  ifdef CONFIG_LIBC_ATOMIC_NOINLINE
+
+/* KASAN calls the atomic functions, so they must not be instrumented */
+
+#    define ARCH_ATOMIC_SPECIFIER \
+       noinline_function nosanitize_address noinstrument_function
+#  else
+#    define ARCH_ATOMIC_SPECIFIER static always_inline_function
+#  endif
 #endif
 
 #if !defined(CONFIG_BUILD_FLAT) && !defined(__KERNEL__)
@@ -276,6 +284,8 @@ extern struct hwspinlock_dev_s g_atomic_hwspinlock;
  * Inline Functions
  ****************************************************************************/
 
+#  if !defined(CONFIG_LIBC_ATOMIC_NOINLINE) || \
+      defined(ARCH_ATOMIC_NOINLINE_DEFINE)
 ARCH_ATOMIC_DEFINE(atomic, uint8_t, 1)
 ARCH_ATOMIC_DEFINE(atomic, uint16_t, 2)
 ARCH_ATOMIC_DEFINE(atomic, int32_t, 4)
@@ -285,6 +295,7 @@ ARCH_SYNC_DEFINE(sync, uint8_t, 1)
 ARCH_SYNC_DEFINE(sync, uint16_t, 2)
 ARCH_SYNC_DEFINE(sync, uint32_t, 4)
 ARCH_SYNC_DEFINE(sync, uint64_t, 8)
+#  endif
 #endif
 
 #endif /* __INCLUDE_NUTTX_LIB_ARCH_ATOMIC_H */
diff --git a/libs/libc/machine/Kconfig b/libs/libc/machine/Kconfig
index 8f6b1175195..032fc3790f6 100644
--- a/libs/libc/machine/Kconfig
+++ b/libs/libc/machine/Kconfig
@@ -48,6 +48,19 @@ config LIBC_ATOMIC_IRQ
        ---help---
                atomic function by irq disable/enable
 
+config LIBC_ATOMIC_NOINLINE
+       bool "Do not inline the atomic functions"
+       default n
+       depends on LIBC_ATOMIC_IRQ || LIBC_ATOMIC_HWSPINLOCK
+       ---help---
+               Build the atomic functions of the IRQ and hardware spinlock
+               backends once in libc and call them, instead of inlining them at
+               each use.  This reduces the code size when the atomic functions
+               are used at many places, at the cost of a call for each atomic
+               operation.  Code that runs from RAM while the flash cannot be
+               read must also place arch_atomic.o in RAM, and loadable modules
+               that use the atomic functions need them in the symbol table.
+
 config LIBC_ATOMIC_TOOLCHAIN
        bool
        default y if !LIBC_ATOMIC_ARCH && !LIBC_ATOMIC_HWSPINLOCK && 
!LIBC_ATOMIC_IRQ
diff --git a/libs/libc/machine/arch_atomic.c b/libs/libc/machine/arch_atomic.c
index 21580de4863..84002e97189 100644
--- a/libs/libc/machine/arch_atomic.c
+++ b/libs/libc/machine/arch_atomic.c
@@ -26,6 +26,7 @@
 
 #include <nuttx/config.h>
 
+#define ARCH_ATOMIC_NOINLINE_DEFINE
 #include <nuttx/atomic.h>
 
 #undef ARCH_ATOMIC_SPECIFIER

Reply via email to