Skip to content

Commit b613863

Browse files
crafcat7xiaoxiang781216
authored andcommitted
fs:replase all asprintf / strdup in fs with fs_heap_xxx
Signed-off-by: chenrun1 <[email protected]>
1 parent 7d218f9 commit b613863

File tree

20 files changed

+120
-59
lines changed

20 files changed

+120
-59
lines changed

drivers/misc/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ if(CONFIG_BLK_RPMSG)
6565
endif()
6666

6767
if(CONFIG_BLK_RPMSG_SERVER)
68-
set_source_files_properties(
69-
rpmsgblk_server.c DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/..
70-
PROPERTIES INCLUDE_DIRECTORIES ${NUTTX_DIR}/fs/inode)
68+
target_include_directories(drivers PRIVATE ${NUTTX_DIR}/fs/inode
69+
${NUTTX_DIR}/fs)
70+
set_source_files_properties(rpmsgblk_server.c DIRECTORY
71+
${CMAKE_CURRENT_LIST_DIR}/..)
7172
list(APPEND SRCS rpmsgblk_server.c)
7273
endif()
7374

drivers/misc/Make.defs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ endif
7070
ifeq ($(CONFIG_BLK_RPMSG_SERVER),y)
7171
CSRCS += rpmsgblk_server.c
7272
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)fs$(DELIM)inode
73+
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)fs
7374
endif
7475

7576
ifneq ($(CONFIG_DEV_OPTEE_NONE),y)

fs/driver/fs_blockproxy.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <nuttx/mutex.h>
4343

4444
#include "driver.h"
45+
#include "fs_heap.h"
4546

4647
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && \
4748
!defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
@@ -112,7 +113,7 @@ static FAR char *unique_chardev(void)
112113
if (ret < 0)
113114
{
114115
DEBUGASSERT(ret == -ENOENT);
115-
return strdup(devbuf);
116+
return fs_heap_strdup(devbuf);
116117
}
117118

118119
/* It is in use, try again */
@@ -198,14 +199,14 @@ int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags)
198199

199200
/* Free the allocated character driver name. */
200201

201-
lib_free(chardev);
202+
fs_heap_free(chardev);
202203
return OK;
203204

204205
errout_with_bchdev:
205206
nx_unlink(chardev);
206207

207208
errout_with_chardev:
208-
lib_free(chardev);
209+
fs_heap_free(chardev);
209210
return ret;
210211
}
211212

fs/driver/fs_mtdproxy.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <nuttx/mutex.h>
3939

4040
#include "driver/driver.h"
41+
#include "fs_heap.h"
4142

4243
/****************************************************************************
4344
* Private Data
@@ -105,7 +106,7 @@ static FAR char *unique_blkdev(void)
105106
if (ret < 0)
106107
{
107108
DEBUGASSERT(ret == -ENOENT);
108-
return strdup(devbuf);
109+
return fs_heap_strdup(devbuf);
109110
}
110111

111112
/* It is in use, try again */
@@ -188,6 +189,6 @@ int mtd_proxy(FAR const char *mtddev, int mountflags,
188189
out_with_fltdev:
189190
nx_unlink(blkdev);
190191
out_with_blkdev:
191-
lib_free(blkdev);
192+
fs_heap_free(blkdev);
192193
return ret;
193194
}

fs/fs_heap.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,46 @@ void fs_heap_free(FAR void *mem)
7373
mm_free(g_fs_heap, mem);
7474
}
7575

76+
FAR char *fs_heap_strdup(FAR const char *s)
77+
{
78+
size_t len = strlen(s) + 1;
79+
FAR char *copy = fs_heap_malloc(len);
80+
if (copy != NULL)
81+
{
82+
memcpy(copy, s, len);
83+
}
84+
85+
return copy;
86+
}
87+
88+
int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...)
89+
{
90+
va_list ap;
91+
int len;
92+
93+
/* Calculates the length required to format the string */
94+
95+
va_start(ap, fmt);
96+
len = vsnprintf(NULL, 0, fmt, ap);
97+
va_end(ap);
98+
99+
if (len < 0)
100+
{
101+
*strp = NULL;
102+
return len;
103+
}
104+
105+
*strp = fs_heap_malloc(len + 1);
106+
if (*strp == NULL)
107+
{
108+
return -ENOMEM;
109+
}
110+
111+
va_start(ap, fmt);
112+
vsnprintf(*strp, len + 1, fmt, ap);
113+
va_end(ap);
114+
115+
return len;
116+
}
117+
76118
#endif

fs/fs_heap.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,36 @@
2626
****************************************************************************/
2727

2828
#include <nuttx/config.h>
29+
2930
#include <nuttx/compiler.h>
3031
#include <nuttx/kmalloc.h>
3132

33+
#include <stdio.h>
34+
#include <string.h>
35+
3236
/****************************************************************************
3337
* Public Function Prototypes
3438
****************************************************************************/
3539

3640
#if defined(CONFIG_FS_HEAPSIZE) && CONFIG_FS_HEAPSIZE > 0
3741
void fs_heap_initialize(void);
38-
FAR void *fs_heap_zalloc(size_t size);
39-
FAR void *fs_heap_malloc(size_t size);
42+
FAR void *fs_heap_zalloc(size_t size) malloc_like1(1);
43+
FAR void *fs_heap_malloc(size_t size) malloc_like1(1);
4044
size_t fs_heap_malloc_size(FAR void *mem);
41-
FAR void *fs_heap_realloc(FAR void *oldmem, size_t size);
45+
FAR void *fs_heap_realloc(FAR void *oldmem, size_t size) realloc_like(2);
4246
void fs_heap_free(FAR void *mem);
47+
FAR char *fs_heap_strdup(FAR const char *s) malloc_like;
48+
int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...)
49+
printf_like(2, 3);
4350
#else
4451
# define fs_heap_initialize()
4552
# define fs_heap_zalloc kmm_zalloc
4653
# define fs_heap_malloc kmm_malloc
4754
# define fs_heap_malloc_size kmm_malloc_size
4855
# define fs_heap_realloc kmm_realloc
4956
# define fs_heap_free kmm_free
57+
# define fs_heap_strdup strdup
58+
# define fs_heap_asprintf asprintf
5059
#endif
5160

5261
#endif

fs/hostfs/hostfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data,
10521052
* "fs=whatever", remote dir
10531053
*/
10541054

1055-
options = strdup(data);
1055+
options = fs_heap_strdup(data);
10561056
if (!options)
10571057
{
10581058
fs_heap_free(fs);
@@ -1070,7 +1070,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data,
10701070
ptr = strtok_r(NULL, ",", &saveptr);
10711071
}
10721072

1073-
lib_free(options);
1073+
fs_heap_free(options);
10741074

10751075
/* Take the lock for the mount */
10761076

fs/inode/fs_inodesearch.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <nuttx/fs/fs.h>
3535

3636
#include "inode/inode.h"
37+
#include "fs_heap.h"
3738

3839
/****************************************************************************
3940
* Private Function Prototypes
@@ -348,12 +349,12 @@ static int _inode_search(FAR struct inode_search_s *desc)
348349
{
349350
FAR char *buffer = NULL;
350351

351-
ret = asprintf(&buffer,
352-
"%s/%s", desc->relpath,
353-
name);
352+
ret = fs_heap_asprintf(&buffer, "%s/%s",
353+
desc->relpath,
354+
name);
354355
if (ret > 0)
355356
{
356-
lib_free(desc->buffer);
357+
fs_heap_free(desc->buffer);
357358
desc->buffer = buffer;
358359
relpath = buffer;
359360
ret = OK;
@@ -478,7 +479,8 @@ int inode_search(FAR struct inode_search_s *desc)
478479

479480
if (*desc->path != '/')
480481
{
481-
ret = asprintf(&desc->buffer, "%s/%s", _inode_getcwd(), desc->path);
482+
ret = fs_heap_asprintf(&desc->buffer, "%s/%s",
483+
_inode_getcwd(), desc->path);
482484
if (ret < 0)
483485
{
484486
return -ENOMEM;

fs/inode/inode.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <nuttx/fs/fs.h>
4040
#include <nuttx/lib/lib.h>
4141

42+
#include "fs_heap.h"
43+
4244
/****************************************************************************
4345
* Pre-processor Definitions
4446
****************************************************************************/
@@ -61,7 +63,7 @@
6163
{ \
6264
if ((d)->buffer != NULL) \
6365
{ \
64-
lib_free((d)->buffer); \
66+
fs_heap_free((d)->buffer); \
6567
(d)->buffer = NULL; \
6668
} \
6769
} \

fs/notify/inotify.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ inotify_alloc_watch_list(FAR const char *path)
746746
}
747747

748748
list_initialize(&list->watches);
749-
list->path = strdup(path);
749+
list->path = fs_heap_strdup(path);
750750
if (list->path == NULL)
751751
{
752752
fs_heap_free(list);
@@ -757,7 +757,7 @@ inotify_alloc_watch_list(FAR const char *path)
757757
item.data = list;
758758
if (hsearch_r(item, ENTER, &result, &g_inotify.hash) == 0)
759759
{
760-
lib_free(list->path);
760+
fs_heap_free(list->path);
761761
fs_heap_free(list);
762762
return NULL;
763763
}
@@ -1022,7 +1022,7 @@ static void notify_free_entry(FAR ENTRY *entry)
10221022
{
10231023
/* Key is alloced by lib_malloc, value is alloced by fs_heap_malloc */
10241024

1025-
lib_free(entry->key);
1025+
fs_heap_free(entry->key);
10261026
fs_heap_free(entry->data);
10271027
}
10281028

@@ -1150,7 +1150,7 @@ int inotify_add_watch(int fd, FAR const char *pathname, uint32_t mask)
11501150

11511151
out_free:
11521152
fs_putfilep(filep);
1153-
lib_free(abspath);
1153+
fs_heap_free(abspath);
11541154
if (ret < 0)
11551155
{
11561156
set_errno(-ret);

0 commit comments

Comments
 (0)