libc: Replace all malloc/free to lib_malloc/lib_free

since libc can be built and used in kernel space,
we must call kmm_malloc and kmm_free in this case.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao
2020-07-26 22:43:43 +08:00
committed by Abdelatif Guettouche
parent d2f75467b5
commit 1cb1fb427d
11 changed files with 69 additions and 51 deletions
+18 -18
View File
@@ -44,6 +44,8 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include "libc.h"
/* The scandir() function is not appropriate for use within the kernel in its /* The scandir() function is not appropriate for use within the kernel in its
* current form because it uses user space memory allocators and modifies * current form because it uses user space memory allocators and modifies
* the errno value. * the errno value.
@@ -59,18 +61,18 @@
* Name: scandir * Name: scandir
* *
* Description: * Description:
* The scandir() function scans the directory dirp, calling filter() on each * The scandir() function scans the directory dirp, calling filter() on
* directory entry. Entries for which filter() returns nonzero are stored * each directory entry. Entries for which filter() returns nonzero are
* in strings allocated via malloc(), sorted using qsort() with comparison * stored in strings allocated via malloc(), sorted using qsort() with
* function compar(), and collected in array namelist which is allocated via * comparison function compar(), and collected in array namelist which is
* malloc(). If filter is NULL, all entries are selected. * allocated via malloc(). If filter is NULL, all entries are selected.
* *
* Input Parameters: * Input Parameters:
* path - Pathname of the directory to scan * path - Pathname of the directory to scan
* namelist - An array of pointers to directory entries, which is allocated * namelist - An array of pointers to directory entries, which is allocated
* by scandir via malloc. Each directory entry is allocated via * by scandir via malloc. Each directory entry is allocated via
* malloc as well. The caller is responsible to free said * malloc as well. The caller is responsible to free said
objects. * objects.
* filter - Directory entries for which filter returns zero are not * filter - Directory entries for which filter returns zero are not
* included in the namelist. If filter is NULL, all entries are * included in the namelist. If filter is NULL, all entries are
* included. * included.
@@ -149,7 +151,7 @@ int scandir(FAR const char *path, FAR struct dirent ***namelist,
listsize *= 2; listsize *= 2;
} }
newlist = realloc(list, listsize * sizeof(*list)); newlist = lib_realloc(list, listsize * sizeof(*list));
if (!newlist) if (!newlist)
{ {
@@ -163,14 +165,12 @@ int scandir(FAR const char *path, FAR struct dirent ***namelist,
list = newlist; list = newlist;
} }
/* Allocate a new directory entry, but restrict its heap size to what is /* Allocate a new directory entry, but restrict its heap size to what
* really required given the directories' path name. * is really required given the directories' path name.
*/ */
dsize = (size_t)(&d->d_name[strlen(d->d_name) + 1] - (char *)d); dsize = (size_t)(&d->d_name[strlen(d->d_name) + 1] - (char *)d);
dnew = lib_malloc(dsize);
dnew = malloc(dsize);
if (!dnew) if (!dnew)
{ {
/* malloc failed and set errno. This will tell follow up code that /* malloc failed and set errno. This will tell follow up code that
@@ -197,14 +197,14 @@ int scandir(FAR const char *path, FAR struct dirent ***namelist,
if (get_errno() == 0) if (get_errno() == 0)
{ {
/* If the caller provided a comparison function, use it to sort the list /* If the caller provided a comparison function, use it to sort the
* of directory entries. * list of directory entries.
*/ */
if (compar) if (compar)
{ {
qsort(list, cnt, sizeof(*list), typedef int (*compar_fn_t)(FAR const void *, FAR const void *);
(CODE int (*)(FAR const void *, FAR const void *))compar); qsort(list, cnt, sizeof(*list), (compar_fn_t)compar);
} }
/* Set the output parameters. */ /* Set the output parameters. */
@@ -220,10 +220,10 @@ int scandir(FAR const char *path, FAR struct dirent ***namelist,
for (i = 0; i < cnt; i++) for (i = 0; i < cnt; i++)
{ {
free(list[i]); lib_free(list[i]);
} }
free(list); lib_free(list);
result = -1; result = -1;
} }
+4 -2
View File
@@ -290,10 +290,12 @@ static inline FAR void *dlinsert(FAR const char *filename)
return NULL; return NULL;
} }
/* Then install the file using the basename of the file as the module name. */ /* Then install the file using the basename of the file as the module
* name.
*/
handle = insmod(filename, basename(name)); handle = insmod(filename, basename(name));
free(name); lib_free(name);
return handle; return handle;
} }
#else /* if defined(CONFIG_BUILD_KERNEL) */ #else /* if defined(CONFIG_BUILD_KERNEL) */
+5 -4
View File
@@ -45,6 +45,7 @@
#include <grp.h> #include <grp.h>
#include "grp/lib_grp.h" #include "grp/lib_grp.h"
#include "libc.h"
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
@@ -85,7 +86,7 @@ FAR struct group *getgrbuf(gid_t gid, FAR const char *name,
buflen = sizeof(FAR char **) + strlen(name) + 1 + strlen(passwd) + 1; buflen = sizeof(FAR char **) + strlen(name) + 1 + strlen(passwd) + 1;
newbuf = (FAR char *)realloc(g_buf, buflen); newbuf = (FAR char *)lib_realloc(g_buf, buflen);
if (!newbuf) if (!newbuf)
{ {
@@ -97,7 +98,7 @@ FAR struct group *getgrbuf(gid_t gid, FAR const char *name,
if (!g_grp) if (!g_grp)
{ {
g_grp = (FAR struct group *)malloc(sizeof(struct group)); g_grp = (FAR struct group *)lib_malloc(sizeof(struct group));
} }
if (!g_grp) if (!g_grp)
@@ -116,8 +117,8 @@ FAR struct group *getgrbuf(gid_t gid, FAR const char *name,
return result; return result;
error: error:
free(g_grp); lib_free(g_grp);
free(g_buf); lib_free(g_buf);
g_grp = NULL; g_grp = NULL;
g_buf = NULL; g_buf = NULL;
set_errno(err); set_errno(err);
+4 -2
View File
@@ -42,6 +42,8 @@
#include <nuttx/streams.h> #include <nuttx/streams.h>
#include "libc.h"
#ifdef CONFIG_LIB_HEX2BIN #ifdef CONFIG_LIB_HEX2BIN
/**************************************************************************** /****************************************************************************
@@ -422,7 +424,7 @@ int hex2bin(FAR struct lib_instream_s *instream,
/* Allocate buffer memory */ /* Allocate buffer memory */
alloc = (FAR uint8_t *)malloc(LINE_ALLOC + BIN_ALLOC); alloc = (FAR uint8_t *)lib_malloc(LINE_ALLOC + BIN_ALLOC);
if (alloc == NULL) if (alloc == NULL)
{ {
lerr("ERROR: Failed to allocate memory\n"); lerr("ERROR: Failed to allocate memory\n");
@@ -703,7 +705,7 @@ errout_with_einval:
errout_with_buffers: errout_with_buffers:
exit_with_buffers: exit_with_buffers:
free(alloc); lib_free(alloc);
return ret; return ret;
} }
+7 -5
View File
@@ -45,6 +45,7 @@
#include <pwd.h> #include <pwd.h>
#include "pwd/lib_pwd.h" #include "pwd/lib_pwd.h"
#include "libc.h"
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
@@ -87,7 +88,7 @@ FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name,
buflen = strlen(name) + 1 + strlen(dir) + 1 + strlen(shell) + 1; buflen = strlen(name) + 1 + strlen(dir) + 1 + strlen(shell) + 1;
newbuf = (FAR char *)realloc(g_buf, buflen); newbuf = (FAR char *)lib_realloc(g_buf, buflen);
if (!newbuf) if (!newbuf)
{ {
@@ -99,7 +100,7 @@ FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name,
if (!g_pwd) if (!g_pwd)
{ {
g_pwd = (FAR struct passwd *)malloc(sizeof(struct passwd)); g_pwd = (FAR struct passwd *)lib_malloc(sizeof(struct passwd));
} }
if (!g_pwd) if (!g_pwd)
@@ -108,7 +109,8 @@ FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name,
goto error; goto error;
} }
err = getpwbuf_r(uid, gid, name, dir, shell, g_pwd, g_buf, buflen, &result); err = getpwbuf_r(uid, gid, name, dir, shell,
g_pwd, g_buf, buflen, &result);
if (err) if (err)
{ {
@@ -118,8 +120,8 @@ FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name,
return result; return result;
error: error:
free(g_pwd); lib_free(g_pwd);
free(g_buf); lib_free(g_buf);
g_pwd = NULL; g_pwd = NULL;
g_buf = NULL; g_buf = NULL;
set_errno(err); set_errno(err);
+3 -1
View File
@@ -43,6 +43,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include "libc.h"
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
@@ -97,7 +99,7 @@ FAR char *tempnam(FAR const char *dir, FAR const char *pfx)
return path; return path;
} }
free(path); lib_free(path);
} }
set_errno(ENOMEM); set_errno(ENOMEM);
+4 -5
View File
@@ -105,7 +105,7 @@ int vasprintf(FAR char **ptr, FAR const IPTR char *fmt, va_list ap)
* for the null terminator. * for the null terminator.
*/ */
buf = (FAR char *)malloc(nulloutstream.nput + 1); buf = (FAR char *)lib_malloc(nulloutstream.nput + 1);
if (!buf) if (!buf)
{ {
va_end(ap); va_end(ap);
@@ -125,16 +125,15 @@ int vasprintf(FAR char **ptr, FAR const IPTR char *fmt, va_list ap)
/* Then let lib_vsprintf do it's real thing */ /* Then let lib_vsprintf do it's real thing */
nbytes = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public,
#ifdef va_copy #ifdef va_copy
nbytes = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public,
fmt, ap2); fmt, ap2);
va_end(ap2);
#else #else
nbytes = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public,
fmt, ap); fmt, ap);
#endif #endif
#ifdef va_copy
va_end(ap2);
#endif
va_end(ap); va_end(ap);
/* Return a pointer to the string to the caller. NOTE: the memstream put() /* Return a pointer to the string to the caller. NOTE: the memstream put()
+4 -2
View File
@@ -30,6 +30,8 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "libc.h"
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@@ -62,7 +64,7 @@ FAR char *realpath(FAR const char *path, FAR char *resolved)
if (resolved == NULL) if (resolved == NULL)
{ {
fres = resolved = malloc(PATH_MAX); fres = resolved = lib_malloc(PATH_MAX);
if (resolved == NULL) if (resolved == NULL)
{ {
return NULL; return NULL;
@@ -230,6 +232,6 @@ loop:
goto loop; goto loop;
out: out:
free(fres); lib_free(fres);
return NULL; return NULL;
} }
+8 -6
View File
@@ -60,6 +60,8 @@
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include "libc.h"
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
@@ -544,7 +546,7 @@ static int tzload(FAR const char *name,
int doaccess; int doaccess;
union local_storage *lsp; union local_storage *lsp;
lsp = malloc(sizeof *lsp); lsp = lib_malloc(sizeof *lsp);
if (!lsp) if (!lsp)
{ {
return -1; return -1;
@@ -897,11 +899,11 @@ static int tzload(FAR const char *name,
} }
sp->defaulttype = i; sp->defaulttype = i;
free(up); lib_free(up);
return 0; return 0;
oops: oops:
free(up); lib_free(up);
return -1; return -1;
} }
@@ -1646,7 +1648,7 @@ static void tzsetwall(void)
if (lclptr == NULL) if (lclptr == NULL)
{ {
lclptr = malloc(sizeof *lclptr); lclptr = lib_malloc(sizeof *lclptr);
if (lclptr == NULL) if (lclptr == NULL)
{ {
settzname(); /* all we can do */ settzname(); /* all we can do */
@@ -1791,7 +1793,7 @@ static struct tm *gmtsub(FAR const time_t * const timep,
{ {
if (!g_gmt_isset) if (!g_gmt_isset)
{ {
gmtptr = malloc(sizeof *gmtptr); gmtptr = lib_malloc(sizeof *gmtptr);
g_gmt_isset = gmtptr != NULL; g_gmt_isset = gmtptr != NULL;
if (g_gmt_isset) if (g_gmt_isset)
{ {
@@ -2510,7 +2512,7 @@ void tzset(void)
if (lclptr == NULL) if (lclptr == NULL)
{ {
lclptr = malloc(sizeof *lclptr); lclptr = lib_malloc(sizeof *lclptr);
if (lclptr == NULL) if (lclptr == NULL)
{ {
settzname(); /* all we can do */ settzname(); /* all we can do */
+8 -4
View File
@@ -44,11 +44,14 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include "libc.h"
#ifdef CONFIG_LIBC_EXECFUNCS #ifdef CONFIG_LIBC_EXECFUNCS
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
/* This is an artificial limit to detect error conditions where an argv[] /* This is an artificial limit to detect error conditions where an argv[]
* list is not properly terminated. * list is not properly terminated.
*/ */
@@ -96,11 +99,12 @@
* The non-standard binfmt function 'exec()' needs to have (1) a symbol * The non-standard binfmt function 'exec()' needs to have (1) a symbol
* table that provides the list of symbols exported by the base code, and * table that provides the list of symbols exported by the base code, and
* (2) the number of symbols in that table. This information is currently * (2) the number of symbols in that table. This information is currently
* provided to 'exec()' from 'exec[l|v]()' via NuttX configuration settings: * provided to 'exec()' from 'exec[l|v]()' via NuttX configuration setting:
* *
* CONFIG_LIBC_EXECFUNCS : Enable exec[l|v] support * CONFIG_LIBC_EXECFUNCS : Enable exec[l|v] support
* CONFIG_EXECFUNCS_SYMTAB_ARRAY : Symbol table name used by exec[l|v] * CONFIG_EXECFUNCS_SYMTAB_ARRAY : Symbol table name used by exec[l|v]
* CONFIG_EXECFUNCS_NSYMBOLS_VAR : Variable holding number of symbols in the table * CONFIG_EXECFUNCS_NSYMBOLS_VAR : Variable holding number of symbols in
* the table
* *
* As a result of the above, the current implementations of 'execl()' and * As a result of the above, the current implementations of 'execl()' and
* 'execv()' suffer from some incompatibilities that may or may not be * 'execv()' suffer from some incompatibilities that may or may not be
@@ -166,7 +170,7 @@ int execl(FAR const char *path, ...)
if (nargs > 0) if (nargs > 0)
{ {
argv = (FAR char **)malloc((nargs + 1) * sizeof(FAR char *)); argv = (FAR char **)lib_malloc((nargs + 1) * sizeof(FAR char *));
if (argv == (FAR char **)NULL) if (argv == (FAR char **)NULL)
{ {
set_errno(ENOMEM); set_errno(ENOMEM);
@@ -193,7 +197,7 @@ int execl(FAR const char *path, ...)
if (argv) if (argv)
{ {
free(argv); lib_free(argv);
} }
return ret; return ret;
+4 -2
View File
@@ -56,6 +56,8 @@
#include <nuttx/fs/userfs.h> #include <nuttx/fs/userfs.h>
#include <nuttx/semaphore.h> #include <nuttx/semaphore.h>
#include "libc.h"
/**************************************************************************** /****************************************************************************
* Private Types * Private Types
****************************************************************************/ ****************************************************************************/
@@ -939,7 +941,7 @@ int userfs_run(FAR const char *mountpt,
*/ */
iolen = USERFS_REQ_MAXSIZE + mxwrite; iolen = USERFS_REQ_MAXSIZE + mxwrite;
info = (FAR struct userfs_info_s *)zalloc(SIZEOF_USERFS_INFO_S(iolen)); info = lib_zalloc(SIZEOF_USERFS_INFO_S(iolen));
if (info == NULL) if (info == NULL)
{ {
ferr("ERROR: Failed to allocate state structure\n"); ferr("ERROR: Failed to allocate state structure\n");
@@ -1165,6 +1167,6 @@ errout_with_sockfd:
/* Free the IO Buffer */ /* Free the IO Buffer */
errout_with_info: errout_with_info:
free(info); lib_free(info);
return ret; return ret;
} }