diff --git a/include/nuttx/lib/lib.h b/include/nuttx/lib/lib.h index 59fb9b9c13..547d8367b7 100644 --- a/include/nuttx/lib/lib.h +++ b/include/nuttx/lib/lib.h @@ -116,6 +116,11 @@ FAR struct file_struct *lib_get_stream(int fd); unsigned long nrand(unsigned long limit); +/* Functions defined in lib_pathbuffer.c ************************************/ + +FAR char *lib_get_pathbuffer(void); +void lib_put_pathbuffer(FAR char *buffer); + #undef EXTERN #ifdef __cplusplus } diff --git a/libs/libc/misc/CMakeLists.txt b/libs/libc/misc/CMakeLists.txt index d1fa2211c0..7ddb3f5979 100644 --- a/libs/libc/misc/CMakeLists.txt +++ b/libs/libc/misc/CMakeLists.txt @@ -43,7 +43,8 @@ list( lib_mkdirat.c lib_utimensat.c lib_memoryregion.c - lib_getnprocs.c) + lib_getnprocs.c + lib_pathbuffer.c) # Support for platforms that do not have long long types diff --git a/libs/libc/misc/Kconfig b/libs/libc/misc/Kconfig index 6227d520fb..abd78dba5d 100644 --- a/libs/libc/misc/Kconfig +++ b/libs/libc/misc/Kconfig @@ -112,3 +112,11 @@ config LIBC_MEM_FD_VFS_PATH depends on !LIBC_MEMFD_ERROR ---help--- The relative path to where memfd will exist in the tmpfs namespace. + +config LIBC_MAX_PATHBUFFER + int "Maximum size of a temporary file path buffer array" + range 0 32 + default 2 + ---help--- + This value is the maximum size of the buffer that will hold the full + file path. diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs index b515bf65ec..c6d4c107a8 100644 --- a/libs/libc/misc/Make.defs +++ b/libs/libc/misc/Make.defs @@ -25,7 +25,7 @@ CSRCS += 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 lib_memoryregion.c -CSRCS += lib_idr.c lib_getnprocs.c +CSRCS += lib_idr.c lib_getnprocs.c lib_pathbuffer.c # Support for platforms that do not have long long types diff --git a/libs/libc/misc/lib_pathbuffer.c b/libs/libc/misc/lib_pathbuffer.c new file mode 100644 index 0000000000..f82a9454a2 --- /dev/null +++ b/libs/libc/misc/lib_pathbuffer.c @@ -0,0 +1,138 @@ +/**************************************************************************** + * libs/libc/misc/lib_pathbuffer.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#include +#include + +/**************************************************************************** + * Pre-processor definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct pathbuffer_s +{ + mutex_t lock; /* Lock for the buffer */ + int free_bitmap; /* Bitmap of free buffer */ + char buffer[CONFIG_LIBC_MAX_PATHBUFFER][PATH_MAX]; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct pathbuffer_s g_pathbuffer = +{ + NXMUTEX_INITIALIZER, + (1u << CONFIG_LIBC_MAX_PATHBUFFER) - 1, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lib_get_pathbuffer + * + * Description: + * The lib_get_pathbuffer() 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(). + * + * Returned Value: + * On success, lib_get_pathbuffer() returns a pointer to a temporary + * buffer. On failure, NULL is returned. + * + ****************************************************************************/ + +FAR char *lib_get_pathbuffer(void) +{ + int index; + + /* Try to find a free buffer */ + + nxmutex_lock(&g_pathbuffer.lock); + index = ffs(g_pathbuffer.free_bitmap) - 1; + if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER) + { + g_pathbuffer.free_bitmap &= ~(1u << index); + nxmutex_unlock(&g_pathbuffer.lock); + return g_pathbuffer.buffer[index]; + } + + nxmutex_unlock(&g_pathbuffer.lock); + + /* If no free buffer is found, allocate a new one */ + + return lib_malloc(PATH_MAX); +} + +/**************************************************************************** + * Name: lib_put_pathbuffer + * + * Description: + * The lib_put_pathbuffer() function frees a temporary buffer that was + * allocated by lib_get_pathbuffer(). 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. + * + * Returned Value: + * None + * + ****************************************************************************/ + +void lib_put_pathbuffer(FAR char *buffer) +{ + int index; + + nxmutex_lock(&g_pathbuffer.lock); + index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX; + + if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER) + { + /* Mark the corresponding bit as free */ + + g_pathbuffer.free_bitmap |= 1u << index; + nxmutex_unlock(&g_pathbuffer.lock); + return; + } + + nxmutex_unlock(&g_pathbuffer.lock); + + /* Free the buffer if it was dynamically allocated */ + + lib_free(buffer); +}