Skip to content

Commit 0bcab28

Browse files
authored
Merge pull request #5268 from mysterywolf/SYSCALL
[libc]重新梳理read write桩函数实现
2 parents ef05ecf + 4fe9388 commit 0bcab28

File tree

11 files changed

+233
-86
lines changed

11 files changed

+233
-86
lines changed

components/libc/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ menu "POSIX layer and C standard library"
22

33
config RT_USING_LIBC
44
bool "Enable libc APIs from toolchain"
5-
default y
5+
select RT_USING_HEAP
6+
default n
67

78
if RT_USING_LIBC
89
config RT_LIBC_USING_TIME

components/libc/compilers/armlibc/mem_std.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,68 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
7-
* 2014-08-03 bernard Add file header.
7+
* Date Author Notes
8+
* 2014-08-03 bernard Add file header
9+
* 2021-11-13 Meco Man implement no-heap warning
810
*/
911

1012
#include <rtthread.h>
1113
#include <stddef.h>
1214

13-
#ifdef RT_USING_HEAP
15+
#ifndef RT_USING_HEAP
16+
#define DBG_TAG "armlibc.mem"
17+
#define DBG_LVL DBG_INFO
18+
#include <rtdbg.h>
19+
20+
#define _NO_HEAP_ERROR() do{LOG_E("Please enable RT_USING_HEAP");\
21+
RT_ASSERT(0);\
22+
}while(0)
23+
#endif /* RT_USING_HEAP */
1424

1525
#ifdef __CC_ARM
1626
/* avoid the heap and heap-using library functions supplied by arm */
1727
#pragma import(__use_no_heap)
18-
#endif
28+
#endif /* __CC_ARM */
1929

2030
void *malloc(size_t n)
2131
{
32+
#ifdef RT_USING_HEAP
2233
return rt_malloc(n);
34+
#else
35+
_NO_HEAP_ERROR();
36+
return RT_NULL;
37+
#endif
2338
}
2439
RTM_EXPORT(malloc);
2540

2641
void *realloc(void *rmem, size_t newsize)
2742
{
43+
#ifdef RT_USING_HEAP
2844
return rt_realloc(rmem, newsize);
45+
#else
46+
_NO_HEAP_ERROR();
47+
return RT_NULL;
48+
#endif
2949
}
3050
RTM_EXPORT(realloc);
3151

3252
void *calloc(size_t nelem, size_t elsize)
3353
{
54+
#ifdef RT_USING_HEAP
3455
return rt_calloc(nelem, elsize);
56+
#else
57+
_NO_HEAP_ERROR();
58+
return RT_NULL;
59+
#endif
3560
}
3661
RTM_EXPORT(calloc);
3762

3863
void free(void *rmem)
3964
{
65+
#ifdef RT_USING_HEAP
4066
rt_free(rmem);
67+
#else
68+
_NO_HEAP_ERROR();
69+
#endif
4170
}
4271
RTM_EXPORT(free);
43-
#endif

components/libc/compilers/armlibc/syscalls.c

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -144,78 +144,85 @@ int _sys_close(FILEHANDLE fh)
144144
*/
145145
int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode)
146146
{
147-
#ifdef RT_USING_POSIX_STDIO
147+
#ifdef RT_USING_POSIX
148148
int size;
149149

150150
if (fh == STDIN)
151151
{
152+
#ifdef RT_USING_POSIX_STDIO
152153
if (libc_stdio_get_console() < 0)
153154
{
154155
LOG_W("Do not invoke standard output before initializing libc");
155-
return 0;
156+
return 0; /* error, but keep going */
156157
}
157158
size = read(STDIN_FILENO, buf, len);
158-
return len - size;
159+
return 0; /* success */
160+
#else
161+
return 0; /* error */
162+
#endif
159163
}
160-
else if ((fh == STDOUT) || (fh == STDERR))
164+
else if (fh == STDOUT || fh == STDERR)
161165
{
162166
return 0; /* error */
163167
}
164-
165-
size = read(fh, buf, len);
166-
if (size >= 0)
167-
return len - size;
168168
else
169-
return 0; /* error */
169+
{
170+
size = read(fh, buf, len);
171+
if (size >= 0)
172+
return len - size; /* success */
173+
else
174+
return 0; /* error */
175+
}
170176
#else
171177
return 0; /* error */
172-
#endif /* RT_USING_POSIX_STDIO */
178+
#endif /* RT_USING_POSIX */
173179
}
174180

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

186-
if ((fh == STDOUT) || (fh == STDERR))
196+
if (fh == STDOUT || fh == STDERR)
187197
{
188-
#ifdef RT_USING_POSIX_STDIO
189-
if (libc_stdio_get_console() < 0)
190-
{
191-
LOG_W("Do not invoke standard input before initializing libc");
192-
return 0;
193-
}
194-
size = write(STDOUT_FILENO, buf, len);
195-
return len - size;
196-
#elif defined(RT_USING_CONSOLE)
197-
if (rt_console_get_device())
198+
#ifdef RT_USING_CONSOLE
199+
rt_device_t console;
200+
console = rt_console_get_device();
201+
if (console)
198202
{
199-
rt_device_write(rt_console_get_device(), -1, buf, len);
203+
rt_device_write(console, -1, buf, len);
200204
}
201-
205+
return 0; /* success */
206+
#else
202207
return 0; /* error */
203-
#endif /* RT_USING_POSIX_STDIO */
208+
#endif /* RT_USING_CONSOLE */
204209
}
205210
else if (fh == STDIN)
206211
{
207212
return 0; /* error */
208213
}
209-
210-
#ifdef RT_USING_POSIX
211-
size = write(fh, buf, len);
212-
if (size >= 0)
213-
return len - size;
214214
else
215-
return 0; /* error */
215+
{
216+
#ifdef RT_USING_POSIX
217+
size = write(fh, buf, len);
218+
if (size >= 0)
219+
return 0; /* success */
220+
else
221+
return 0; /* error */
216222
#else
217-
return 0;
223+
return 0; /* error */
218224
#endif /* RT_USING_POSIX */
225+
}
219226
}
220227

221228
/*

components/libc/compilers/dlib/syscall_close.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
* 2015-01-28 Bernard first version
99
*/
1010
#include <rtthread.h>
11-
#include <yfuns.h>
11+
#include <LowLevelIOInterface.h>
1212
#include <unistd.h>
1313

14+
/*
15+
* The "__close" function should close the file corresponding to
16+
* "handle". It should return 0 on success and nonzero on failure.
17+
*/
18+
1419
#pragma module_name = "?__close"
20+
1521
int __close(int handle)
1622
{
1723
if (handle == _LLIO_STDOUT ||

components/libc/compilers/dlib/syscall_lseek.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,25 @@
88
* 2015-01-28 Bernard first version
99
*/
1010
#include <rtthread.h>
11-
#include <yfuns.h>
11+
#include <LowLevelIOInterface.h>
1212
#include <unistd.h>
1313

14+
/*
15+
* The "__lseek" function makes the next file operation (__read or
16+
* __write) act on a new location. The parameter "whence" specifies
17+
* how the "offset" parameter should be interpreted according to the
18+
* following table:
19+
*
20+
* 0 (=SEEK_SET) - Goto location "offset".
21+
* 1 (=SEEK_CUR) - Go "offset" bytes from the current location.
22+
* 2 (=SEEK_END) - Go to "offset" bytes from the end.
23+
*
24+
* This function should return the current file position, or -1 on
25+
* failure.
26+
*/
27+
1428
#pragma module_name = "?__lseek"
29+
1530
long __lseek(int handle, long offset, int whence)
1631
{
1732
if (handle == _LLIO_STDOUT ||

components/libc/compilers/dlib/syscall_mem.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,55 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2015-01-28 Bernard first version
9+
* 2021-11-13 Meco Man implement no-heap warning
910
*/
1011
#include <rtthread.h>
12+
#include <stddef.h>
1113

12-
#ifdef RT_USING_HEAP
13-
void *malloc(rt_size_t n)
14+
#ifndef RT_USING_HEAP
15+
#define DBG_TAG "dlib.syscall_mem"
16+
#define DBG_LVL DBG_INFO
17+
#include <rtdbg.h>
18+
#define _NO_HEAP_ERROR() do{LOG_E("Please enable RT_USING_HEAP");\
19+
RT_ASSERT(0);\
20+
}while(0)
21+
#endif /* RT_USING_HEAP */
22+
23+
void *malloc(size_t n)
1424
{
25+
#ifdef RT_USING_HEAP
1526
return rt_malloc(n);
27+
#else
28+
_NO_HEAP_ERROR();
29+
return RT_NULL;
30+
#endif
1631
}
1732

18-
void *realloc(void *rmem, rt_size_t newsize)
33+
void *realloc(void *rmem, size_t newsize)
1934
{
35+
#ifdef RT_USING_HEAP
2036
return rt_realloc(rmem, newsize);
37+
#else
38+
_NO_HEAP_ERROR();
39+
return RT_NULL;
40+
#endif
2141
}
2242

23-
void *calloc(rt_size_t nelem, rt_size_t elsize)
43+
void *calloc(size_t nelem, size_t elsize)
2444
{
45+
#ifdef RT_USING_HEAP
2546
return rt_calloc(nelem, elsize);
47+
#else
48+
_NO_HEAP_ERROR();
49+
return RT_NULL;
50+
#endif
2651
}
2752

2853
void free(void *rmem)
2954
{
55+
#ifdef RT_USING_HEAP
3056
rt_free(rmem);
31-
}
57+
#else
58+
_NO_HEAP_ERROR();
3259
#endif
60+
}

components/libc/compilers/dlib/syscall_open.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
*/
1010

1111
#include <rtthread.h>
12-
#include <yfuns.h>
12+
#include <LowLevelIOInterface.h>
1313
#include <fcntl.h>
1414

15+
/*
16+
* The "__open" function opens the file named "filename" as specified
17+
* by "mode".
18+
*/
19+
1520
#pragma module_name = "?__open"
1621

1722
int __open(const char *filename, int mode)

components/libc/compilers/dlib/syscall_read.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
#include <rtthread.h>
12-
#include <yfuns.h>
12+
#include <LowLevelIOInterface.h>
1313
#include <unistd.h>
1414
#ifdef RT_USING_POSIX_STDIO
1515
#include "libc.h"
@@ -19,28 +19,44 @@
1919
#define DBG_LVL DBG_INFO
2020
#include <rtdbg.h>
2121

22+
/*
23+
* The "__read" function reads a number of bytes, at most "size" into
24+
* the memory area pointed to by "buffer". It returns the number of
25+
* bytes read, 0 at the end of the file, or _LLIO_ERROR if failure
26+
* occurs.
27+
*
28+
* The template implementation below assumes that the application
29+
* provides the function "MyLowLevelGetchar". It should return a
30+
* character value, or -1 on failure.
31+
*/
32+
2233
#pragma module_name = "?__read"
34+
2335
size_t __read(int handle, unsigned char *buf, size_t len)
2436
{
25-
#ifdef RT_USING_POSIX_STDIO
37+
#ifdef RT_USING_POSIX
2638
int size;
2739

2840
if (handle == _LLIO_STDIN)
2941
{
42+
#ifdef RT_USING_POSIX_STDIO
3043
if (libc_stdio_get_console() < 0)
3144
{
3245
LOG_W("Do not invoke standard input before initializing libc");
33-
return 0;
46+
return 0; /* error, but keep going */
3447
}
35-
return read(STDIN_FILENO, buf, len);
48+
return read(STDIN_FILENO, buf, len); /* return the length of the data read */
49+
#else
50+
return _LLIO_ERROR;
51+
#endif /* RT_USING_POSIX_STDIO */
3652
}
3753
else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR))
3854
{
3955
return _LLIO_ERROR;
4056
}
4157

4258
size = read(handle, buf, len);
43-
return size;
59+
return size; /* return the length of the data read */
4460
#else
4561
return _LLIO_ERROR;
4662
#endif /* RT_USING_POSIX */

0 commit comments

Comments
 (0)