From aaa6a571657cfbe2255c2401a6ba55ccf0a29bc1 Mon Sep 17 00:00:00 2001 From: zhangshoukui Date: Wed, 18 Dec 2024 19:32:11 +0800 Subject: [PATCH 1/4] Add LINE_MAX configuration Signed-off-by: zhangshoukui --- include/limits.h | 2 +- sched/Kconfig | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/limits.h b/include/limits.h index bf61300648..f46e3d2b9a 100644 --- a/include/limits.h +++ b/include/limits.h @@ -130,7 +130,7 @@ #define _POSIX_PIPE_BUF 512 #define _POSIX_STREAM_MAX 16 #define _POSIX_TZNAME_MAX 3 -#define _POSIX2_LINE_MAX 80 +#define _POSIX2_LINE_MAX CONFIG_LINE_MAX #ifdef CONFIG_SMALL_MEMORY diff --git a/sched/Kconfig b/sched/Kconfig index ac4f703ca6..4c1cc8d6d7 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -1412,6 +1412,16 @@ config PATH_MAX ---help--- The maximum size of path name. +config LINE_MAX + int "Maximum size of line" + default 64 if DEFAULT_SMALL + default 80 if !DEFAULT_SMALL + ---help--- + The maximum size of line. Unless otherwise noted, the maximum length, in bytes, + of a utility's input line (either standard input or another file), when the + utility is described as processing text files. The length includes room for + the trailing newline. + endmenu # Files and I/O menuconfig PRIORITY_INHERITANCE From 69525d6d13b4e7b0760dd27f80bbb1f9fc7d02b0 Mon Sep 17 00:00:00 2001 From: zhangshoukui Date: Tue, 17 Dec 2024 09:46:23 +0800 Subject: [PATCH 2/4] lib_get_pathbuffer: Modify size to the maximum value of PATH_MAX and CONFIG_LINE_MAX Signed-off-by: zhangshoukui --- libs/libc/misc/lib_pathbuffer.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libs/libc/misc/lib_pathbuffer.c b/libs/libc/misc/lib_pathbuffer.c index 3928130dc8..8c164e40fb 100644 --- a/libs/libc/misc/lib_pathbuffer.c +++ b/libs/libc/misc/lib_pathbuffer.c @@ -35,6 +35,12 @@ * Pre-processor definitions ****************************************************************************/ +#if CONFIG_PATH_MAX > CONFIG_LINE_MAX +# define PATH_MAX_SIZE CONFIG_PATH_MAX +#else +# define PATH_MAX_SIZE CONFIG_LINE_MAX +#endif + /**************************************************************************** * Private Types ****************************************************************************/ @@ -43,7 +49,7 @@ struct pathbuffer_s { spinlock_t lock; /* Lock for the buffer */ unsigned long free_bitmap; /* Bitmap of free buffer */ - char buffer[CONFIG_LIBC_MAX_PATHBUFFER][PATH_MAX]; + char buffer[CONFIG_LIBC_MAX_PATHBUFFER][PATH_MAX_SIZE]; }; /**************************************************************************** @@ -103,7 +109,7 @@ FAR char *lib_get_pathbuffer(void) */ #ifdef CONFIG_LIBC_PATHBUFFER_MALLOC - return lib_malloc(PATH_MAX); + return lib_malloc(PATH_MAX_SIZE); #else return NULL; #endif @@ -128,7 +134,7 @@ void lib_put_pathbuffer(FAR char *buffer) irqstate_t flags; int index; - index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX; + index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX_SIZE; if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER) { /* Mark the corresponding bit as free */ From e9dcd08e472184bd607fc2ac0df7c5e4551fedd6 Mon Sep 17 00:00:00 2001 From: chao an Date: Tue, 10 Dec 2024 12:45:49 +0800 Subject: [PATCH 3/4] libs/libc: add a option to disable temp buffer by default If the current platform does not require a large TEMP_MAX_SIZE size support and toolchain supports alloca(), we could turn off this option to improve performance. Signed-off-by: chao an --- include/nuttx/lib/lib.h | 8 ++++++++ libs/libc/misc/CMakeLists.txt | 7 +++++-- libs/libc/misc/Kconfig | 12 ++++++++++++ libs/libc/misc/Make.defs | 6 +++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/nuttx/lib/lib.h b/include/nuttx/lib/lib.h index 9593d9bd74..16691024fd 100644 --- a/include/nuttx/lib/lib.h +++ b/include/nuttx/lib/lib.h @@ -31,6 +31,9 @@ #include #include +#include +#include + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -119,8 +122,13 @@ unsigned long nrand(unsigned long limit); /* Functions defined in lib_pathbuffer.c ************************************/ +#ifdef CONFIG_LIBC_PATHBUFFER FAR char *lib_get_pathbuffer(void); void lib_put_pathbuffer(FAR char *buffer); +#else +# define lib_get_pathbuffer(n) alloca(n) +# define lib_put_pathbuffer(b) +#endif /* Functions defined in lib_realpath.c **************************************/ diff --git a/libs/libc/misc/CMakeLists.txt b/libs/libc/misc/CMakeLists.txt index 39b4d86ecb..6c4a5cd09f 100644 --- a/libs/libc/misc/CMakeLists.txt +++ b/libs/libc/misc/CMakeLists.txt @@ -47,8 +47,11 @@ list( lib_mkdirat.c lib_utimensat.c lib_mallopt.c - lib_getnprocs.c - lib_pathbuffer.c) + lib_getnprocs.c) + +if(CONFIG_LIBC_PATHBUFFER) + list(APPEND SRCS lib_pathbuffer.c) +endif() # Support for platforms that do not have long long types diff --git a/libs/libc/misc/Kconfig b/libs/libc/misc/Kconfig index d834ca0c63..44d66689d6 100644 --- a/libs/libc/misc/Kconfig +++ b/libs/libc/misc/Kconfig @@ -119,6 +119,16 @@ config LIBC_MEM_FD_VFS_PATH ---help--- The relative path to where memfd will exist in the tmpfs namespace. +config LIBC_PATHBUFFER + bool "Enable global path buffer" + default !DEFAULT_SMALL + ---help--- + Enable this option to enable the global path buffer, otherwise use stack variables via alloca(). + If the current platform does not require a large PATH_MAX size support and toolchain supports alloca(), + we could turn off this option to improve performance. + +if LIBC_PATHBUFFER + config LIBC_MAX_PATHBUFFER int "Maximum size of a temporary file path buffer array" range 0 32 @@ -133,6 +143,8 @@ config LIBC_PATHBUFFER_MALLOC ---help--- Enable malloc path buffer from the heap when pathbuffer is insufficient. +endif # LIBC_PATHBUFFER + config LIBC_BACKTRACE_BUFFSIZE int "The size of backtrace record buffer" depends on SCHED_BACKTRACE diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs index a3c22278a7..ae93069639 100644 --- a/libs/libc/misc/Make.defs +++ b/libs/libc/misc/Make.defs @@ -27,7 +27,11 @@ CSRCS += lib_getrandom.c lib_xorshift128.c lib_tea_encrypt.c lib_tea_decrypt.c CSRCS += lib_cxx_initialize.c lib_impure.c lib_memfd.c lib_mutex.c CSRCS += lib_fchmodat.c lib_fstatat.c lib_getfullpath.c lib_openat.c CSRCS += lib_mkdirat.c lib_utimensat.c lib_mallopt.c -CSRCS += lib_idr.c lib_getnprocs.c lib_pathbuffer.c +CSRCS += lib_idr.c lib_getnprocs.c + +ifeq ($(CONFIG_LIBC_PATHBUFFER),y) +CSRCS += lib_pathbuffer.c +endif # Support for platforms that do not have long long types From d13cfc7284208fb5d45116e8c73e4775486bcd77 Mon Sep 17 00:00:00 2001 From: zhangshoukui Date: Wed, 8 Jan 2025 11:18:42 +0800 Subject: [PATCH 4/4] rename lib_pathbuffer to lib_tempbuffer Signed-off-by: zhangshoukui --- include/nuttx/lib/lib.h | 15 ++-- libs/libc/misc/CMakeLists.txt | 4 +- libs/libc/misc/Kconfig | 25 +++---- libs/libc/misc/Make.defs | 4 +- .../{lib_pathbuffer.c => lib_tempbuffer.c} | 75 ++++++++++--------- 5 files changed, 64 insertions(+), 59 deletions(-) rename libs/libc/misc/{lib_pathbuffer.c => lib_tempbuffer.c} (67%) diff --git a/include/nuttx/lib/lib.h b/include/nuttx/lib/lib.h index 16691024fd..92c2436988 100644 --- a/include/nuttx/lib/lib.h +++ b/include/nuttx/lib/lib.h @@ -120,16 +120,19 @@ FAR struct file_struct *lib_get_stream(int fd); unsigned long nrand(unsigned long limit); -/* Functions defined in lib_pathbuffer.c ************************************/ +/* Functions defined in lib_tempbuffer.c ************************************/ -#ifdef CONFIG_LIBC_PATHBUFFER -FAR char *lib_get_pathbuffer(void); -void lib_put_pathbuffer(FAR char *buffer); +#ifdef CONFIG_LIBC_TEMPBUFFER +FAR char *lib_get_tempbuffer(size_t nbytes); +void lib_put_tempbuffer(FAR char *buffer); #else -# define lib_get_pathbuffer(n) alloca(n) -# define lib_put_pathbuffer(b) +# define lib_get_tempbuffer(n) alloca(n) +# define lib_put_tempbuffer(b) #endif +#define lib_get_pathbuffer() lib_get_tempbuffer(PATH_MAX) +#define lib_put_pathbuffer(b) lib_put_tempbuffer(b) + /* Functions defined in lib_realpath.c **************************************/ FAR char *lib_realpath(FAR const char *path, FAR char *resolved, diff --git a/libs/libc/misc/CMakeLists.txt b/libs/libc/misc/CMakeLists.txt index 6c4a5cd09f..d4c46b2a2d 100644 --- a/libs/libc/misc/CMakeLists.txt +++ b/libs/libc/misc/CMakeLists.txt @@ -49,8 +49,8 @@ list( lib_mallopt.c lib_getnprocs.c) -if(CONFIG_LIBC_PATHBUFFER) - list(APPEND SRCS lib_pathbuffer.c) +if(CONFIG_LIBC_TEMPBUFFER) + list(APPEND SRCS lib_tempbuffer.c) endif() # Support for platforms that do not have long long types diff --git a/libs/libc/misc/Kconfig b/libs/libc/misc/Kconfig index 44d66689d6..bf47b07abb 100644 --- a/libs/libc/misc/Kconfig +++ b/libs/libc/misc/Kconfig @@ -119,31 +119,30 @@ config LIBC_MEM_FD_VFS_PATH ---help--- The relative path to where memfd will exist in the tmpfs namespace. -config LIBC_PATHBUFFER - bool "Enable global path buffer" +config LIBC_TEMPBUFFER + bool "Enable global temp buffer" default !DEFAULT_SMALL ---help--- - Enable this option to enable the global path buffer, otherwise use stack variables via alloca(). - If the current platform does not require a large PATH_MAX size support and toolchain supports alloca(), + Enable this option to enable the global temp buffer, otherwise use stack variables via alloca(). + If the current platform does not require a large TEMP_MAX_SIZE size support and toolchain supports alloca(), we could turn off this option to improve performance. -if LIBC_PATHBUFFER +if LIBC_TEMPBUFFER -config LIBC_MAX_PATHBUFFER - int "Maximum size of a temporary file path buffer array" +config LIBC_MAX_TEMPBUFFER + int "Maximum size of a temporary file temp buffer array" range 0 32 default 2 ---help--- - This value is the maximum size of the buffer that will hold the full - file path. + This value is the maximum size of the buffer that will hold the full line. -config LIBC_PATHBUFFER_MALLOC - bool "Enable malloc pathbuffer" +config LIBC_TEMPBUFFER_MALLOC + bool "Enable malloc tempbuffer" default y ---help--- - Enable malloc path buffer from the heap when pathbuffer is insufficient. + Enable malloc temp buffer from the heap when tempbuffer is insufficient. -endif # LIBC_PATHBUFFER +endif # LIBC_TEMPBUFFER config LIBC_BACKTRACE_BUFFSIZE int "The size of backtrace record buffer" diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs index ae93069639..e1fc5ba009 100644 --- a/libs/libc/misc/Make.defs +++ b/libs/libc/misc/Make.defs @@ -29,8 +29,8 @@ CSRCS += lib_fchmodat.c lib_fstatat.c lib_getfullpath.c lib_openat.c CSRCS += lib_mkdirat.c lib_utimensat.c lib_mallopt.c CSRCS += lib_idr.c lib_getnprocs.c -ifeq ($(CONFIG_LIBC_PATHBUFFER),y) -CSRCS += lib_pathbuffer.c +ifeq ($(CONFIG_LIBC_TEMPBUFFER),y) +CSRCS += lib_tempbuffer.c endif # Support for platforms that do not have long long types diff --git a/libs/libc/misc/lib_pathbuffer.c b/libs/libc/misc/lib_tempbuffer.c similarity index 67% rename from libs/libc/misc/lib_pathbuffer.c rename to libs/libc/misc/lib_tempbuffer.c index 8c164e40fb..342f524935 100644 --- a/libs/libc/misc/lib_pathbuffer.c +++ b/libs/libc/misc/lib_tempbuffer.c @@ -1,5 +1,5 @@ /**************************************************************************** - * libs/libc/misc/lib_pathbuffer.c + * libs/libc/misc/lib_tempbuffer.c * * SPDX-License-Identifier: Apache-2.0 * @@ -36,30 +36,30 @@ ****************************************************************************/ #if CONFIG_PATH_MAX > CONFIG_LINE_MAX -# define PATH_MAX_SIZE CONFIG_PATH_MAX +# define TEMP_MAX_SIZE CONFIG_PATH_MAX #else -# define PATH_MAX_SIZE CONFIG_LINE_MAX +# define TEMP_MAX_SIZE CONFIG_LINE_MAX #endif /**************************************************************************** * Private Types ****************************************************************************/ -struct pathbuffer_s +struct tempbuffer_s { spinlock_t lock; /* Lock for the buffer */ unsigned long free_bitmap; /* Bitmap of free buffer */ - char buffer[CONFIG_LIBC_MAX_PATHBUFFER][PATH_MAX_SIZE]; + char buffer[CONFIG_LIBC_MAX_TEMPBUFFER][TEMP_MAX_SIZE]; }; /**************************************************************************** * Private Data ****************************************************************************/ -static struct pathbuffer_s g_pathbuffer = +static struct tempbuffer_s g_tempbuffer = { SP_UNLOCKED, - (1u << CONFIG_LIBC_MAX_PATHBUFFER) - 1, + (1u << CONFIG_LIBC_MAX_TEMPBUFFER) - 1, }; /**************************************************************************** @@ -71,56 +71,59 @@ static struct pathbuffer_s g_pathbuffer = ****************************************************************************/ /**************************************************************************** - * Name: lib_get_pathbuffer + * Name: lib_get_tempbuffer * * Description: - * The lib_get_pathbuffer() function returns a pointer to a temporary + * The lib_get_tempbuffer() function returns a pointer to a temporary * buffer. The buffer is allocated from a pool of pre-allocated buffers * and if the pool is exhausted, a new buffer is allocated through - * kmm_malloc(). The size of the buffer is PATH_MAX, and must freed by - * calling lib_put_pathbuffer(). + * kmm_malloc(). The size of the buffer is nbytes, and must freed by + * calling lib_put_tempbuffer(). * * Returned Value: - * On success, lib_get_pathbuffer() returns a pointer to a temporary + * On success, lib_get_tempbuffer() returns a pointer to a temporary * buffer. On failure, NULL is returned. * ****************************************************************************/ -FAR char *lib_get_pathbuffer(void) +FAR char *lib_get_tempbuffer(size_t nbytes) { irqstate_t flags; int index; - /* Try to find a free buffer */ - - flags = spin_lock_irqsave(&g_pathbuffer.lock); - index = ffsl(g_pathbuffer.free_bitmap) - 1; - if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER) + if (nbytes <= TEMP_MAX_SIZE) { - g_pathbuffer.free_bitmap &= ~(1u << index); - spin_unlock_irqrestore(&g_pathbuffer.lock, flags); - return g_pathbuffer.buffer[index]; + /* Try to find a free buffer */ + + flags = spin_lock_irqsave(&g_tempbuffer.lock); + index = ffsl(g_tempbuffer.free_bitmap) - 1; + if (index >= 0 && index < CONFIG_LIBC_MAX_TEMPBUFFER) + { + g_tempbuffer.free_bitmap &= ~(1u << index); + spin_unlock_irqrestore(&g_tempbuffer.lock, flags); + return g_tempbuffer.buffer[index]; + } + + spin_unlock_irqrestore(&g_tempbuffer.lock, flags); } - spin_unlock_irqrestore(&g_pathbuffer.lock, flags); - /* If no free buffer is found, allocate a new one if - * CONFIG_LIBC_PATHBUFFER_MALLOC is enabled + * CONFIG_LIBC_TEMPBUFFER_MALLOC is enabled */ -#ifdef CONFIG_LIBC_PATHBUFFER_MALLOC - return lib_malloc(PATH_MAX_SIZE); +#ifdef CONFIG_LIBC_TEMPBUFFER_MALLOC + return lib_malloc(nbytes); #else return NULL; #endif } /**************************************************************************** - * Name: lib_put_pathbuffer + * Name: lib_put_tempbuffer * * Description: - * The lib_put_pathbuffer() function frees a temporary buffer that was - * allocated by lib_get_pathbuffer(). If the buffer was allocated + * The lib_put_tempbuffer() function frees a temporary buffer that was + * allocated by lib_get_tempbuffer(). If the buffer was allocated * dynamically, it is freed by calling kmm_free(). Otherwise, the buffer * is marked as free in the pool of pre-allocated buffers. * @@ -129,25 +132,25 @@ FAR char *lib_get_pathbuffer(void) * ****************************************************************************/ -void lib_put_pathbuffer(FAR char *buffer) +void lib_put_tempbuffer(FAR char *buffer) { irqstate_t flags; int index; - index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX_SIZE; - if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER) + index = (buffer - &g_tempbuffer.buffer[0][0]) / TEMP_MAX_SIZE; + if (index >= 0 && index < CONFIG_LIBC_MAX_TEMPBUFFER) { /* Mark the corresponding bit as free */ - flags = spin_lock_irqsave(&g_pathbuffer.lock); - g_pathbuffer.free_bitmap |= 1u << index; - spin_unlock_irqrestore(&g_pathbuffer.lock, flags); + flags = spin_lock_irqsave(&g_tempbuffer.lock); + g_tempbuffer.free_bitmap |= 1u << index; + spin_unlock_irqrestore(&g_tempbuffer.lock, flags); return; } /* Free the buffer if it was dynamically allocated */ -#ifdef CONFIG_LIBC_PATHBUFFER_MALLOC +#ifdef CONFIG_LIBC_TEMPBUFFER_MALLOC lib_free(buffer); #endif }