Skip to content

Commit c811655

Browse files
committed
【添加】ctrl + d 实现软中断
1 parent 5c0ee72 commit c811655

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

port/mpy_main.c

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@
2727
#include <stdint.h>
2828
#include <stdio.h>
2929
#include <string.h>
30-
3130
#include <rtthread.h>
32-
3331
#ifdef RT_USING_DFS
3432
#include <dfs_posix.h>
3533
#endif
36-
3734
#include <py/compile.h>
3835
#include <py/runtime.h>
3936
#include <py/repl.h>
@@ -66,13 +63,87 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) {
6663
}
6764
#endif
6865

66+
#ifdef RT_USING_DFS
67+
static int mp_sys_resource_bak(struct dfs_fdtable **table_bak)
68+
{
69+
struct dfs_fdtable *fd_table;
70+
struct dfs_fdtable *fd_table_bak;
71+
struct dfs_fd **fds;
72+
73+
fd_table = dfs_fdtable_get();
74+
if (!fd_table)
75+
{
76+
return RT_FALSE;
77+
}
78+
79+
fd_table_bak = (struct dfs_fdtable *)rt_malloc(sizeof(struct dfs_fdtable));
80+
if (!fd_table_bak)
81+
{
82+
goto _exit_tab;
83+
}
84+
85+
fds = (struct dfs_fd **)rt_malloc((int)fd_table->maxfd * sizeof(struct dfs_fd *));
86+
if (!fds)
87+
{
88+
goto _exit_fds;
89+
}
90+
else
91+
{
92+
rt_memcpy(fds, fd_table->fds, (int)fd_table->maxfd * sizeof(struct dfs_fd *));
93+
fd_table_bak->maxfd = (int)fd_table->maxfd;
94+
fd_table_bak->fds = fds;
95+
}
96+
97+
*table_bak = fd_table_bak;
98+
99+
return RT_TRUE;
100+
101+
_exit_fds:
102+
rt_free(fd_table_bak);
103+
104+
_exit_tab:
105+
return RT_FALSE;
106+
}
107+
108+
static void mp_sys_resource_gc(struct dfs_fdtable *fd_table_bak)
109+
{
110+
struct dfs_fdtable *fd_table;
111+
struct dfs_fd *fd;
112+
113+
if (!fd_table_bak) return;
114+
115+
fd_table = dfs_fdtable_get();
116+
117+
for(int i = 0; i < fd_table->maxfd; i++)
118+
{
119+
if (fd_table->fds[i] != RT_NULL)
120+
{
121+
if ((i < fd_table_bak->maxfd && fd_table_bak->fds[i] == RT_NULL) || (i >= fd_table_bak->maxfd))
122+
{
123+
fd = fd_table->fds[i];
124+
dfs_file_close(fd);
125+
fd_put(fd);
126+
}
127+
}
128+
}
129+
130+
rt_free(fd_table_bak->fds);
131+
rt_free(fd_table_bak);
132+
}
133+
#endif
134+
69135
static void *stack_top = RT_NULL;
70136
static char *heap = RT_NULL;
71137

72138
void mpy_main(const char *filename) {
73139
int stack_dummy;
74140
int stack_size_check;
75141
stack_top = (void *)&stack_dummy;
142+
143+
#ifdef RT_USING_DFS
144+
struct dfs_fdtable *fd_table_bak = NULL;
145+
mp_sys_resource_bak(&fd_table_bak);
146+
#endif
76147

77148
mp_getchar_init();
78149
mp_putsn_init();
@@ -167,6 +238,10 @@ void mpy_main(const char *filename) {
167238

168239
mp_putsn_deinit();
169240
mp_getchar_deinit();
241+
242+
#ifdef RT_USING_DFS
243+
mp_sys_resource_gc(fd_table_bak);
244+
#endif
170245
}
171246

172247
#if !MICROPY_PY_MODUOS_FILE

0 commit comments

Comments
 (0)