From ac275965f2e2d3ff730d06a8962ccaaea4f027ce Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sun, 28 Feb 2021 11:09:48 +0800 Subject: [PATCH] =?UTF-8?q?[libc][newlib]=20=E5=AF=B9syscall=E4=B8=ADmallo?= =?UTF-8?q?c=E7=9B=B8=E5=85=B3=E6=A1=A9=E5=87=BD=E6=95=B0=E5=81=9A?= =?UTF-8?q?=E5=87=BA=E7=BC=96=E8=AF=91=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/libc/compilers/newlib/minilib.c | 53 ++++++++++++++++++++- components/libc/compilers/newlib/syscalls.c | 18 +++---- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/components/libc/compilers/newlib/minilib.c b/components/libc/compilers/newlib/minilib.c index 2d07eabe5a..92877bd6c3 100644 --- a/components/libc/compilers/newlib/minilib.c +++ b/components/libc/compilers/newlib/minilib.c @@ -11,8 +11,59 @@ #include #include -void * _sbrk_r(struct _reent *ptr, ptrdiff_t incr) +#ifdef RT_USING_HEAP /* Memory routine */ +void * +_malloc_r (struct _reent *ptr, size_t size) +{ + void* result; + + result = (void*)rt_malloc (size); + if (result == RT_NULL) + { + ptr->_errno = ENOMEM; + } + + return result; +} + +void * +_realloc_r (struct _reent *ptr, void *old, size_t newlen) +{ + void* result; + + result = (void*)rt_realloc (old, newlen); + if (result == RT_NULL) + { + ptr->_errno = ENOMEM; + } + + return result; +} + +void *_calloc_r (struct _reent *ptr, size_t size, size_t len) +{ + void* result; + + result = (void*)rt_calloc (size, len); + if (result == RT_NULL) + { + ptr->_errno = ENOMEM; + } + + return result; +} + +void +_free_r (struct _reent *ptr, void *addr) +{ + rt_free (addr); +} + +#else +void * +_sbrk_r(struct _reent *ptr, ptrdiff_t incr) { /* no use this routine to get memory */ return RT_NULL; } +#endif /*RT_USING_HEAP*/ diff --git a/components/libc/compilers/newlib/syscalls.c b/components/libc/compilers/newlib/syscalls.c index 949d8a61de..444e3960c1 100644 --- a/components/libc/compilers/newlib/syscalls.c +++ b/components/libc/compilers/newlib/syscalls.c @@ -189,13 +189,6 @@ _rename_r(struct _reent *ptr, const char *old, const char *new) #endif } -void * -_sbrk_r(struct _reent *ptr, ptrdiff_t incr) -{ - /* no use this routine to get memory */ - return RT_NULL; -} - int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat) { @@ -258,7 +251,7 @@ _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) #endif } -/* Memory routine */ +#ifdef RT_USING_HEAP /* Memory routine */ void * _malloc_r (struct _reent *ptr, size_t size) { @@ -306,6 +299,15 @@ _free_r (struct _reent *ptr, void *addr) rt_free (addr); } +#else +void * +_sbrk_r(struct _reent *ptr, ptrdiff_t incr) +{ + /* no use this routine to get memory */ + return RT_NULL; +} +#endif /*RT_USING_HEAP*/ + /* for exit() and abort() */ __attribute__ ((noreturn)) void _exit (int status) -- GitLab