Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/libc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ menu "POSIX layer and C standard library"

config RT_USING_LIBC
bool "Enable libc APIs from toolchain"
default y
select RT_USING_HEAP
default n

if RT_USING_LIBC
config RT_LIBC_USING_TIME
Expand Down
36 changes: 32 additions & 4 deletions components/libc/compilers/armlibc/mem_std.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,68 @@
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* 2014-08-03 bernard Add file header.
* Date Author Notes
* 2014-08-03 bernard Add file header
* 2021-11-13 Meco Man implement no-heap warning
*/

#include <rtthread.h>
#include <stddef.h>

#ifdef RT_USING_HEAP
#ifndef RT_USING_HEAP
#define DBG_TAG "armlibc.mem"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#define _NO_HEAP_ERROR() do{LOG_E("Please enable RT_USING_HEAP");\
RT_ASSERT(0);\
}while(0)
#endif /* RT_USING_HEAP */

#ifdef __CC_ARM
/* avoid the heap and heap-using library functions supplied by arm */
#pragma import(__use_no_heap)
#endif
#endif /* __CC_ARM */

void *malloc(size_t n)
{
#ifdef RT_USING_HEAP
return rt_malloc(n);
#else
_NO_HEAP_ERROR();
return RT_NULL;
#endif
}
RTM_EXPORT(malloc);

void *realloc(void *rmem, size_t newsize)
{
#ifdef RT_USING_HEAP
return rt_realloc(rmem, newsize);
#else
_NO_HEAP_ERROR();
return RT_NULL;
#endif
}
RTM_EXPORT(realloc);

void *calloc(size_t nelem, size_t elsize)
{
#ifdef RT_USING_HEAP
return rt_calloc(nelem, elsize);
#else
_NO_HEAP_ERROR();
return RT_NULL;
#endif
}
RTM_EXPORT(calloc);

void free(void *rmem)
{
#ifdef RT_USING_HEAP
rt_free(rmem);
#else
_NO_HEAP_ERROR();
#endif
}
RTM_EXPORT(free);
#endif
69 changes: 38 additions & 31 deletions components/libc/compilers/armlibc/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,78 +144,85 @@ int _sys_close(FILEHANDLE fh)
*/
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
{
#ifdef RT_USING_POSIX_STDIO
#ifdef RT_USING_POSIX
int size;

if (fh == STDIN)
{
#ifdef RT_USING_POSIX_STDIO
if (libc_stdio_get_console() < 0)
{
LOG_W("Do not invoke standard output before initializing libc");
return 0;
return 0; /* error, but keep going */
}
size = read(STDIN_FILENO, buf, len);
return len - size;
return 0; /* success */
#else
return 0; /* error */
#endif
}
else if ((fh == STDOUT) || (fh == STDERR))
else if (fh == STDOUT || fh == STDERR)
{
return 0; /* error */
}

size = read(fh, buf, len);
if (size >= 0)
return len - size;
else
return 0; /* error */
{
size = read(fh, buf, len);
if (size >= 0)
return len - size; /* success */
else
return 0; /* error */
}
#else
return 0; /* error */
#endif /* RT_USING_POSIX_STDIO */
#endif /* RT_USING_POSIX */
}

/*
* Write to a file. Returns 0 on success, negative on error, and
* the number of characters _not_ written on partial success.
* `mode' exists for historical reasons and must be ignored.
* The return value is either:
* A positive number representing the number of characters not written
* (so any nonzero return value denotes a failure of some sort).
* A negative number indicating an error.
*/
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
#ifdef RT_USING_POSIX
int size;
#endif /* RT_USING_POSIX */

if ((fh == STDOUT) || (fh == STDERR))
if (fh == STDOUT || fh == STDERR)
{
#ifdef RT_USING_POSIX_STDIO
if (libc_stdio_get_console() < 0)
{
LOG_W("Do not invoke standard input before initializing libc");
return 0;
}
size = write(STDOUT_FILENO, buf, len);
return len - size;
#elif defined(RT_USING_CONSOLE)
if (rt_console_get_device())
#ifdef RT_USING_CONSOLE
rt_device_t console;
console = rt_console_get_device();
if (console)
{
rt_device_write(rt_console_get_device(), -1, buf, len);
rt_device_write(console, -1, buf, len);
}

return 0; /* success */
#else
return 0; /* error */
#endif /* RT_USING_POSIX_STDIO */
#endif /* RT_USING_CONSOLE */
}
else if (fh == STDIN)
{
return 0; /* error */
}

#ifdef RT_USING_POSIX
size = write(fh, buf, len);
if (size >= 0)
return len - size;
else
return 0; /* error */
{
#ifdef RT_USING_POSIX
size = write(fh, buf, len);
if (size >= 0)
return 0; /* success */
else
return 0; /* error */
#else
return 0;
return 0; /* error */
#endif /* RT_USING_POSIX */
}
}

/*
Expand Down
8 changes: 7 additions & 1 deletion components/libc/compilers/dlib/syscall_close.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
#include <yfuns.h>
#include <LowLevelIOInterface.h>
#include <unistd.h>

/*
* The "__close" function should close the file corresponding to
* "handle". It should return 0 on success and nonzero on failure.
*/

#pragma module_name = "?__close"

int __close(int handle)
{
if (handle == _LLIO_STDOUT ||
Expand Down
17 changes: 16 additions & 1 deletion components/libc/compilers/dlib/syscall_lseek.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@
* 2015-01-28 Bernard first version
*/
#include <rtthread.h>
#include <yfuns.h>
#include <LowLevelIOInterface.h>
#include <unistd.h>

/*
* The "__lseek" function makes the next file operation (__read or
* __write) act on a new location. The parameter "whence" specifies
* how the "offset" parameter should be interpreted according to the
* following table:
*
* 0 (=SEEK_SET) - Goto location "offset".
* 1 (=SEEK_CUR) - Go "offset" bytes from the current location.
* 2 (=SEEK_END) - Go to "offset" bytes from the end.
*
* This function should return the current file position, or -1 on
* failure.
*/

#pragma module_name = "?__lseek"

long __lseek(int handle, long offset, int whence)
{
if (handle == _LLIO_STDOUT ||
Expand Down
38 changes: 33 additions & 5 deletions components/libc/compilers/dlib/syscall_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,55 @@
* Change Logs:
* Date Author Notes
* 2015-01-28 Bernard first version
* 2021-11-13 Meco Man implement no-heap warning
*/
#include <rtthread.h>
#include <stddef.h>

#ifdef RT_USING_HEAP
void *malloc(rt_size_t n)
#ifndef RT_USING_HEAP
#define DBG_TAG "dlib.syscall_mem"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define _NO_HEAP_ERROR() do{LOG_E("Please enable RT_USING_HEAP");\
RT_ASSERT(0);\
}while(0)
#endif /* RT_USING_HEAP */

void *malloc(size_t n)
{
#ifdef RT_USING_HEAP
return rt_malloc(n);
#else
_NO_HEAP_ERROR();
return RT_NULL;
#endif
}

void *realloc(void *rmem, rt_size_t newsize)
void *realloc(void *rmem, size_t newsize)
{
#ifdef RT_USING_HEAP
return rt_realloc(rmem, newsize);
#else
_NO_HEAP_ERROR();
return RT_NULL;
#endif
}

void *calloc(rt_size_t nelem, rt_size_t elsize)
void *calloc(size_t nelem, size_t elsize)
{
#ifdef RT_USING_HEAP
return rt_calloc(nelem, elsize);
#else
_NO_HEAP_ERROR();
return RT_NULL;
#endif
}

void free(void *rmem)
{
#ifdef RT_USING_HEAP
rt_free(rmem);
}
#else
_NO_HEAP_ERROR();
#endif
}
7 changes: 6 additions & 1 deletion components/libc/compilers/dlib/syscall_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
*/

#include <rtthread.h>
#include <yfuns.h>
#include <LowLevelIOInterface.h>
#include <fcntl.h>

/*
* The "__open" function opens the file named "filename" as specified
* by "mode".
*/

#pragma module_name = "?__open"

int __open(const char *filename, int mode)
Expand Down
26 changes: 21 additions & 5 deletions components/libc/compilers/dlib/syscall_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

#include <rtthread.h>
#include <yfuns.h>
#include <LowLevelIOInterface.h>
#include <unistd.h>
#ifdef RT_USING_POSIX_STDIO
#include "libc.h"
Expand All @@ -19,28 +19,44 @@
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

/*
* The "__read" function reads a number of bytes, at most "size" into
* the memory area pointed to by "buffer". It returns the number of
* bytes read, 0 at the end of the file, or _LLIO_ERROR if failure
* occurs.
*
* The template implementation below assumes that the application
* provides the function "MyLowLevelGetchar". It should return a
* character value, or -1 on failure.
*/

#pragma module_name = "?__read"

size_t __read(int handle, unsigned char *buf, size_t len)
{
#ifdef RT_USING_POSIX_STDIO
#ifdef RT_USING_POSIX
int size;

if (handle == _LLIO_STDIN)
{
#ifdef RT_USING_POSIX_STDIO
if (libc_stdio_get_console() < 0)
{
LOG_W("Do not invoke standard input before initializing libc");
return 0;
return 0; /* error, but keep going */
}
return read(STDIN_FILENO, buf, len);
return read(STDIN_FILENO, buf, len); /* return the length of the data read */
#else
return _LLIO_ERROR;
#endif /* RT_USING_POSIX_STDIO */
}
else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
{
return _LLIO_ERROR;
}

size = read(handle, buf, len);
return size;
return size; /* return the length of the data read */
#else
return _LLIO_ERROR;
#endif /* RT_USING_POSIX */
Expand Down
Loading