Skip to content

Commit 16afee6

Browse files
guohao15xiaoxiang781216
authored andcommitted
notify:add path tmp buffer
Signed-off-by: guohao15 <[email protected]>
1 parent dba77ff commit 16afee6

File tree

5 files changed

+154
-2
lines changed

5 files changed

+154
-2
lines changed

include/nuttx/lib/lib.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ FAR struct file_struct *lib_get_stream(int fd);
116116

117117
unsigned long nrand(unsigned long limit);
118118

119+
/* Functions defined in lib_pathbuffer.c ************************************/
120+
121+
FAR char *lib_get_pathbuffer(void);
122+
void lib_put_pathbuffer(FAR char *buffer);
123+
119124
#undef EXTERN
120125
#ifdef __cplusplus
121126
}

libs/libc/misc/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ list(
4343
lib_mkdirat.c
4444
lib_utimensat.c
4545
lib_memoryregion.c
46-
lib_getnprocs.c)
46+
lib_getnprocs.c
47+
lib_pathbuffer.c)
4748

4849
# Support for platforms that do not have long long types
4950

libs/libc/misc/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,11 @@ config LIBC_MEM_FD_VFS_PATH
112112
depends on !LIBC_MEMFD_ERROR
113113
---help---
114114
The relative path to where memfd will exist in the tmpfs namespace.
115+
116+
config LIBC_MAX_PATHBUFFER
117+
int "Maximum size of a temporary file path buffer array"
118+
range 0 32
119+
default 2
120+
---help---
121+
This value is the maximum size of the buffer that will hold the full
122+
file path.

libs/libc/misc/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ CSRCS += lib_xorshift128.c lib_tea_encrypt.c lib_tea_decrypt.c
2525
CSRCS += lib_cxx_initialize.c lib_impure.c lib_memfd.c lib_mutex.c
2626
CSRCS += lib_fchmodat.c lib_fstatat.c lib_getfullpath.c lib_openat.c
2727
CSRCS += lib_mkdirat.c lib_utimensat.c lib_mallopt.c lib_memoryregion.c
28-
CSRCS += lib_idr.c lib_getnprocs.c
28+
CSRCS += lib_idr.c lib_getnprocs.c lib_pathbuffer.c
2929

3030
# Support for platforms that do not have long long types
3131

libs/libc/misc/lib_pathbuffer.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/****************************************************************************
2+
* libs/libc/misc/lib_pathbuffer.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
#include <nuttx/mutex.h>
27+
#include <nuttx/lib/lib.h>
28+
29+
#include <stdlib.h>
30+
#include <string.h>
31+
32+
/****************************************************************************
33+
* Pre-processor definitions
34+
****************************************************************************/
35+
36+
/****************************************************************************
37+
* Private Types
38+
****************************************************************************/
39+
40+
struct pathbuffer_s
41+
{
42+
mutex_t lock; /* Lock for the buffer */
43+
int free_bitmap; /* Bitmap of free buffer */
44+
char buffer[CONFIG_LIBC_MAX_PATHBUFFER][PATH_MAX];
45+
};
46+
47+
/****************************************************************************
48+
* Private Data
49+
****************************************************************************/
50+
51+
static struct pathbuffer_s g_pathbuffer =
52+
{
53+
NXMUTEX_INITIALIZER,
54+
(1u << CONFIG_LIBC_MAX_PATHBUFFER) - 1,
55+
};
56+
57+
/****************************************************************************
58+
* Private Functions
59+
****************************************************************************/
60+
61+
/****************************************************************************
62+
* Public Functions
63+
****************************************************************************/
64+
65+
/****************************************************************************
66+
* Name: lib_get_pathbuffer
67+
*
68+
* Description:
69+
* The lib_get_pathbuffer() function returns a pointer to a temporary
70+
* buffer. The buffer is allocated from a pool of pre-allocated buffers
71+
* and if the pool is exhausted, a new buffer is allocated through
72+
* kmm_malloc(). The size of the buffer is PATH_MAX, and must freed by
73+
* calling lib_put_pathbuffer().
74+
*
75+
* Returned Value:
76+
* On success, lib_get_pathbuffer() returns a pointer to a temporary
77+
* buffer. On failure, NULL is returned.
78+
*
79+
****************************************************************************/
80+
81+
FAR char *lib_get_pathbuffer(void)
82+
{
83+
int index;
84+
85+
/* Try to find a free buffer */
86+
87+
nxmutex_lock(&g_pathbuffer.lock);
88+
index = ffs(g_pathbuffer.free_bitmap) - 1;
89+
if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER)
90+
{
91+
g_pathbuffer.free_bitmap &= ~(1u << index);
92+
nxmutex_unlock(&g_pathbuffer.lock);
93+
return g_pathbuffer.buffer[index];
94+
}
95+
96+
nxmutex_unlock(&g_pathbuffer.lock);
97+
98+
/* If no free buffer is found, allocate a new one */
99+
100+
return lib_malloc(PATH_MAX);
101+
}
102+
103+
/****************************************************************************
104+
* Name: lib_put_pathbuffer
105+
*
106+
* Description:
107+
* The lib_put_pathbuffer() function frees a temporary buffer that was
108+
* allocated by lib_get_pathbuffer(). If the buffer was allocated
109+
* dynamically, it is freed by calling kmm_free(). Otherwise, the buffer
110+
* is marked as free in the pool of pre-allocated buffers.
111+
*
112+
* Returned Value:
113+
* None
114+
*
115+
****************************************************************************/
116+
117+
void lib_put_pathbuffer(FAR char *buffer)
118+
{
119+
int index;
120+
121+
nxmutex_lock(&g_pathbuffer.lock);
122+
index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX;
123+
124+
if (index >= 0 && index < CONFIG_LIBC_MAX_PATHBUFFER)
125+
{
126+
/* Mark the corresponding bit as free */
127+
128+
g_pathbuffer.free_bitmap |= 1u << index;
129+
nxmutex_unlock(&g_pathbuffer.lock);
130+
return;
131+
}
132+
133+
nxmutex_unlock(&g_pathbuffer.lock);
134+
135+
/* Free the buffer if it was dynamically allocated */
136+
137+
lib_free(buffer);
138+
}

0 commit comments

Comments
 (0)