rename lib_pathbuffer to lib_tempbuffer

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
This commit is contained in:
zhangshoukui 2025-01-07 10:17:58 +08:00 committed by Xiang Xiao
parent 553406e801
commit aef24f5f3c
5 changed files with 65 additions and 60 deletions

View file

@ -120,16 +120,19 @@ FAR struct file_struct *lib_get_stream(int fd);
unsigned long nrand(unsigned long limit); unsigned long nrand(unsigned long limit);
/* Functions defined in lib_pathbuffer.c ************************************/ /* Functions defined in lib_tempbuffer.c ************************************/
#ifdef CONFIG_LIBC_PATHBUFFER #ifdef CONFIG_LIBC_TEMPBUFFER
FAR char *lib_get_pathbuffer(void); FAR char *lib_get_tempbuffer(size_t nbytes);
void lib_put_pathbuffer(FAR char *buffer); void lib_put_tempbuffer(FAR char *buffer);
#else #else
# define lib_get_pathbuffer() alloca(PATH_MAX) # define lib_get_tempbuffer(n) alloca(n)
# define lib_put_pathbuffer(b) # define lib_put_tempbuffer(b)
#endif #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 **************************************/ /* Functions defined in lib_realpath.c **************************************/
FAR char *lib_realpath(FAR const char *path, FAR char *resolved, FAR char *lib_realpath(FAR const char *path, FAR char *resolved,

View file

@ -49,8 +49,8 @@ list(
lib_mallopt.c lib_mallopt.c
lib_getnprocs.c) lib_getnprocs.c)
if(CONFIG_LIBC_PATHBUFFER) if(CONFIG_LIBC_TEMPBUFFER)
list(APPEND SRCS lib_pathbuffer.c) list(APPEND SRCS lib_tempbuffer.c)
endif() endif()
# Support for platforms that do not have long long types # Support for platforms that do not have long long types

View file

@ -119,31 +119,30 @@ config LIBC_MEM_FD_VFS_PATH
---help--- ---help---
The relative path to where memfd will exist in the tmpfs namespace. The relative path to where memfd will exist in the tmpfs namespace.
config LIBC_PATHBUFFER config LIBC_TEMPBUFFER
bool "Enable global path buffer" bool "Enable global temp buffer"
default !DEFAULT_SMALL default !DEFAULT_SMALL
---help--- ---help---
Enable this option to enable the global path buffer, otherwise use stack variables via 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 PATH_MAX size support and toolchain supports 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. we could turn off this option to improve performance.
if LIBC_PATHBUFFER if LIBC_TEMPBUFFER
config LIBC_PATHBUFFER_MAX config LIBC_MAX_TEMPBUFFER
int "Maximum size of a temporary file path buffer array" int "Maximum size of a temporary file temp buffer array"
range 0 32 range 0 32
default 2 default 2
---help--- ---help---
This value is the maximum size of the buffer that will hold the full This value is the maximum size of the buffer that will hold the full line.
file path.
config LIBC_PATHBUFFER_MALLOC config LIBC_TEMPBUFFER_MALLOC
bool "Enable malloc pathbuffer" bool "Enable malloc tempbuffer"
default y default y
---help--- ---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 config LIBC_BACKTRACE_BUFFSIZE
int "The size of backtrace record buffer" int "The size of backtrace record buffer"

View file

@ -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_mkdirat.c lib_utimensat.c lib_mallopt.c
CSRCS += lib_idr.c lib_getnprocs.c CSRCS += lib_idr.c lib_getnprocs.c
ifeq ($(CONFIG_LIBC_PATHBUFFER),y) ifeq ($(CONFIG_LIBC_TEMPBUFFER),y)
CSRCS += lib_pathbuffer.c CSRCS += lib_tempbuffer.c
endif endif
# Support for platforms that do not have long long types # Support for platforms that do not have long long types

View file

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* libs/libc/misc/lib_pathbuffer.c * libs/libc/misc/lib_tempbuffer.c
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
@ -36,28 +36,28 @@
****************************************************************************/ ****************************************************************************/
#if CONFIG_PATH_MAX > CONFIG_LINE_MAX #if CONFIG_PATH_MAX > CONFIG_LINE_MAX
# define PATH_MAX_SIZE CONFIG_PATH_MAX # define TEMP_MAX_SIZE CONFIG_PATH_MAX
#else #else
# define PATH_MAX_SIZE CONFIG_LINE_MAX # define TEMP_MAX_SIZE CONFIG_LINE_MAX
#endif #endif
/**************************************************************************** /****************************************************************************
* Private Types * Private Types
****************************************************************************/ ****************************************************************************/
struct pathbuffer_s struct tempbuffer_s
{ {
atomic_t free_bitmap; /* Bitmap of free buffer */ atomic_t free_bitmap; /* Bitmap of free buffer */
char buffer[CONFIG_LIBC_PATHBUFFER_MAX][PATH_MAX_SIZE]; char buffer[CONFIG_LIBC_MAX_TEMPBUFFER][TEMP_MAX_SIZE];
}; };
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
****************************************************************************/ ****************************************************************************/
static struct pathbuffer_s g_pathbuffer = static struct tempbuffer_s g_tempbuffer =
{ {
(1u << CONFIG_LIBC_PATHBUFFER_MAX) - 1, (1u << CONFIG_LIBC_MAX_TEMPBUFFER) - 1,
}; };
/**************************************************************************** /****************************************************************************
@ -69,58 +69,61 @@ static struct pathbuffer_s g_pathbuffer =
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: lib_get_pathbuffer * Name: lib_get_tempbuffer
* *
* Description: * 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 * buffer. The buffer is allocated from a pool of pre-allocated buffers
* and if the pool is exhausted, a new buffer is allocated through * and if the pool is exhausted, a new buffer is allocated through
* kmm_malloc(). The size of the buffer is PATH_MAX_SIZE, and must freed by * kmm_malloc(). The size of the buffer is nbytes, and must freed by
* calling lib_put_pathbuffer(). * calling lib_put_tempbuffer().
* *
* Returned Value: * 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. * buffer. On failure, NULL is returned.
* *
****************************************************************************/ ****************************************************************************/
FAR char *lib_get_pathbuffer(void) FAR char *lib_get_tempbuffer(size_t nbytes)
{ {
for (; ; ) if (nbytes <= TEMP_MAX_SIZE)
{ {
int32_t update; for (; ; )
int32_t free_bitmap = atomic_read(&g_pathbuffer.free_bitmap);
int index = ffsl(free_bitmap) - 1;
if (index < 0 || index >= CONFIG_LIBC_PATHBUFFER_MAX)
{ {
break; int32_t update;
} int32_t free_bitmap = atomic_read(&g_tempbuffer.free_bitmap);
int index = ffsl(free_bitmap) - 1;
if (index < 0 || index >= CONFIG_LIBC_MAX_TEMPBUFFER)
{
break;
}
update = free_bitmap & ~(1u << index); update = free_bitmap & ~(1u << index);
if (atomic_cmpxchg(&g_pathbuffer.free_bitmap, &free_bitmap, if (atomic_cmpxchg(&g_tempbuffer.free_bitmap, &free_bitmap,
update)) update))
{ {
return g_pathbuffer.buffer[index]; return g_tempbuffer.buffer[index];
}
} }
} }
/* If no free buffer is found, allocate a new one if /* 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 #ifdef CONFIG_LIBC_TEMPBUFFER_MALLOC
return lib_malloc(PATH_MAX_SIZE); return lib_malloc(nbytes);
#else #else
return NULL; return NULL;
#endif #endif
} }
/**************************************************************************** /****************************************************************************
* Name: lib_put_pathbuffer * Name: lib_put_tempbuffer
* *
* Description: * Description:
* The lib_put_pathbuffer() function frees a temporary buffer that was * The lib_put_tempbuffer() function frees a temporary buffer that was
* allocated by lib_get_pathbuffer(). If the buffer was allocated * allocated by lib_get_tempbuffer(). If the buffer was allocated
* dynamically, it is freed by calling kmm_free(). Otherwise, the buffer * dynamically, it is freed by calling kmm_free(). Otherwise, the buffer
* is marked as free in the pool of pre-allocated buffers. * is marked as free in the pool of pre-allocated buffers.
* *
@ -129,20 +132,20 @@ FAR char *lib_get_pathbuffer(void)
* *
****************************************************************************/ ****************************************************************************/
void lib_put_pathbuffer(FAR char *buffer) void lib_put_tempbuffer(FAR char *buffer)
{ {
int index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX_SIZE; int index = (buffer - &g_tempbuffer.buffer[0][0]) / TEMP_MAX_SIZE;
if (index >= 0 && index < CONFIG_LIBC_PATHBUFFER_MAX) if (index >= 0 && index < CONFIG_LIBC_MAX_TEMPBUFFER)
{ {
DEBUGASSERT((atomic_read(&g_pathbuffer.free_bitmap) & DEBUGASSERT((atomic_read(&g_tempbuffer.free_bitmap) &
(1u << index)) == 0); (1u << index)) == 0);
atomic_fetch_or_acquire(&g_pathbuffer.free_bitmap, 1u << index); atomic_fetch_or_acquire(&g_tempbuffer.free_bitmap, 1u << index);
return; return;
} }
/* Free the buffer if it was dynamically allocated */ /* Free the buffer if it was dynamically allocated */
#ifdef CONFIG_LIBC_PATHBUFFER_MALLOC #ifdef CONFIG_LIBC_TEMPBUFFER_MALLOC
lib_free(buffer); lib_free(buffer);
#endif #endif
} }