Skip to content

Commit

Permalink
fs:replase all asprintf / strdup in fs with fs_heap_xxx
Browse files Browse the repository at this point in the history
Signed-off-by: chenrun1 <[email protected]>
  • Loading branch information
crafcat7 committed Oct 12, 2024
1 parent a565d3b commit 4834f10
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 51 deletions.
7 changes: 4 additions & 3 deletions fs/driver/fs_blockproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <nuttx/mutex.h>

#include "driver.h"
#include "fs_heap.h"

#if !defined(CONFIG_DISABLE_MOUNTPOINT) && \
!defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
Expand Down Expand Up @@ -112,7 +113,7 @@ static FAR char *unique_chardev(void)
if (ret < 0)
{
DEBUGASSERT(ret == -ENOENT);
return strdup(devbuf);
return fs_heap_strdup(devbuf);
}

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

/* Free the allocated character driver name. */

lib_free(chardev);
fs_heap_free(chardev);
return OK;

errout_with_bchdev:
nx_unlink(chardev);

errout_with_chardev:
lib_free(chardev);
fs_heap_free(chardev);
return ret;
}

Expand Down
5 changes: 3 additions & 2 deletions fs/driver/fs_mtdproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <nuttx/mutex.h>

#include "driver/driver.h"
#include "fs_heap.h"

/****************************************************************************
* Private Data
Expand Down Expand Up @@ -105,7 +106,7 @@ static FAR char *unique_blkdev(void)
if (ret < 0)
{
DEBUGASSERT(ret == -ENOENT);
return strdup(devbuf);
return fs_heap_strdup(devbuf);
}

/* It is in use, try again */
Expand Down Expand Up @@ -188,6 +189,6 @@ int mtd_proxy(FAR const char *mtddev, int mountflags,
out_with_fltdev:
nx_unlink(blkdev);
out_with_blkdev:
lib_free(blkdev);
fs_heap_free(blkdev);
return ret;
}
42 changes: 42 additions & 0 deletions fs/fs_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,46 @@ void fs_heap_free(FAR void *mem)
mm_free(g_fs_heap, mem);
}

FAR char *fs_heap_strdup(FAR const char *s)
{
size_t len = strlen(s) + 1;
FAR char *copy = fs_heap_malloc(len);
if (copy != NULL)
{
memcpy(copy, s, len);
}

return copy;
}

int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...)
{
va_list ap;
int len;

/* Calculates the length required to format the string */

va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);

if (len < 0)
{
*strp = NULL;
return len;
}

*strp = fs_heap_malloc(len + 1);
if (*strp == NULL)
{
return ERROR;
}

va_start(ap, fmt);
vsnprintf(*strp, len + 1, fmt, ap);
va_end(ap);

return len;
}

#endif
4 changes: 4 additions & 0 deletions fs/fs_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ FAR void *fs_heap_malloc(size_t size);
size_t fs_heap_malloc_size(FAR void *mem);
FAR void *fs_heap_realloc(FAR void *oldmem, size_t size);
void fs_heap_free(FAR void *mem);
FAR char *fs_heap_strdup(FAR const char *s);
int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...);
#else
# define fs_heap_initialize()
# define fs_heap_zalloc kmm_zalloc
# define fs_heap_malloc kmm_malloc
# define fs_heap_malloc_size kmm_malloc_size
# define fs_heap_realloc kmm_realloc
# define fs_heap_free kmm_free
# define fs_heap_strdup nx_strdup
# define fs_heap_asprintf nx_asprintf
#endif

#endif
4 changes: 2 additions & 2 deletions fs/hostfs/hostfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data,
* "fs=whatever", remote dir
*/

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

lib_free(options);
fs_heap_free(options);

/* Take the lock for the mount */

Expand Down
7 changes: 4 additions & 3 deletions fs/inode/fs_inodesearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <nuttx/fs/fs.h>

#include "inode/inode.h"
#include "fs_heap.h"

/****************************************************************************
* Private Function Prototypes
Expand Down Expand Up @@ -348,12 +349,12 @@ static int _inode_search(FAR struct inode_search_s *desc)
{
FAR char *buffer = NULL;

ret = asprintf(&buffer,
ret = fs_heap_asprintf(&buffer,
"%s/%s", desc->relpath,
name);
if (ret > 0)
{
lib_free(desc->buffer);
fs_heap_free(desc->buffer);
desc->buffer = buffer;
relpath = buffer;
ret = OK;
Expand Down Expand Up @@ -478,7 +479,7 @@ int inode_search(FAR struct inode_search_s *desc)

if (*desc->path != '/')
{
ret = asprintf(&desc->buffer, "%s/%s", _inode_getcwd(), desc->path);
ret = fs_heap_asprintf(&desc->buffer, "%s/%s", _inode_getcwd(), desc->path);
if (ret < 0)
{
return -ENOMEM;
Expand Down
5 changes: 4 additions & 1 deletion fs/inode/inode.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include <nuttx/fs/fs.h>
#include <nuttx/lib/lib.h>

#include "../fs_heap.h"
#include "fs_heap.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
Expand All @@ -61,7 +64,7 @@
{ \
if ((d)->buffer != NULL) \
{ \
lib_free((d)->buffer); \
fs_heap_free((d)->buffer); \
(d)->buffer = NULL; \
} \
} \
Expand Down
8 changes: 4 additions & 4 deletions fs/notify/inotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ inotify_alloc_watch_list(FAR const char *path)
}

list_initialize(&list->watches);
list->path = strdup(path);
list->path = fs_heap_strdup(path);
if (list->path == NULL)
{
fs_heap_free(list);
Expand All @@ -757,7 +757,7 @@ inotify_alloc_watch_list(FAR const char *path)
item.data = list;
if (hsearch_r(item, ENTER, &result, &g_inotify.hash) == 0)
{
lib_free(list->path);
fs_heap_free(list->path);
fs_heap_free(list);
return NULL;
}
Expand Down Expand Up @@ -1022,7 +1022,7 @@ static void notify_free_entry(FAR ENTRY *entry)
{
/* Key is alloced by lib_malloc, value is alloced by fs_heap_malloc */

lib_free(entry->key);
fs_heap_free(entry->key);
fs_heap_free(entry->data);
}

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

out_free:
fs_putfilep(filep);
lib_free(abspath);
fs_heap_free(abspath);
if (ret < 0)
{
set_errno(-ret);
Expand Down
2 changes: 1 addition & 1 deletion fs/nxffs/nxffs_inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void nxffs_freeentry(FAR struct nxffs_entry_s *entry)
{
if (entry->name)
{
lib_free(entry->name);
fs_heap_free(entry->name);
entry->name = NULL;
}
}
Expand Down
4 changes: 2 additions & 2 deletions fs/nxffs/nxffs_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,

/* Save a copy of the inode name. */

wrfile->ofile.entry.name = strdup(name);
wrfile->ofile.entry.name = fs_heap_strdup(name);
if (!wrfile->ofile.entry.name)
{
ret = -ENOMEM;
Expand Down Expand Up @@ -655,7 +655,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,
return OK;

errout_with_name:
lib_free(wrfile->ofile.entry.name);
fs_heap_free(wrfile->ofile.entry.name);
errout_with_ofile:
#ifndef CONFIG_NXFFS_PREALLOCATED
fs_heap_free(wrfile);
Expand Down
3 changes: 2 additions & 1 deletion fs/nxffs/nxffs_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <nuttx/kmalloc.h>

#include "nxffs.h"
#include "fs_heap.h"

/****************************************************************************
* Private Types
Expand Down Expand Up @@ -1089,7 +1090,7 @@ nxffs_setupwriter(FAR struct nxffs_volume_s *volume,
/* Initialize for the packing operation. */

memset(&pack->dest, 0, sizeof(struct nxffs_packstream_s));
pack->dest.entry.name = strdup(wrfile->ofile.entry.name);
pack->dest.entry.name = fs_heap_strdup(wrfile->ofile.entry.name);
pack->dest.entry.utc = wrfile->ofile.entry.utc;
pack->dest.entry.datlen = wrfile->ofile.entry.datlen;

Expand Down
4 changes: 2 additions & 2 deletions fs/rpmsgfs/rpmsgfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data,
* "timeout=xx", connect timeout, unit (ms)
*/

options = strdup(data);
options = fs_heap_strdup(data);
if (!options)
{
fs_heap_free(fs);
Expand Down Expand Up @@ -1106,7 +1106,7 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data,
}

ret = rpmsgfs_client_bind(&fs->handle, cpuname);
lib_free(options);
fs_heap_free(options);
if (ret < 0)
{
fs_heap_free(fs);
Expand Down
4 changes: 2 additions & 2 deletions fs/tmpfs/fs_tmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ static int tmpfs_remove_dirent(FAR struct tmpfs_directory_s *tdo,

if (tdo->tdo_entry[index].tde_name != NULL)
{
lib_free(tdo->tdo_entry[index].tde_name);
fs_heap_free(tdo->tdo_entry[index].tde_name);
}

/* Remove by replacing this entry with the final directory entry */
Expand Down Expand Up @@ -1216,7 +1216,7 @@ static int tmpfs_free_callout(FAR struct tmpfs_directory_s *tdo,

if (tdo->tdo_entry[index].tde_name != NULL)
{
lib_free(tdo->tdo_entry[index].tde_name);
fs_heap_free(tdo->tdo_entry[index].tde_name);
}

/* Remove by replacing this entry with the final directory entry */
Expand Down
Loading

0 comments on commit 4834f10

Please sign in to comment.