|
27 | 27 | #include <stdint.h> |
28 | 28 | #include <stdio.h> |
29 | 29 | #include <string.h> |
30 | | - |
31 | 30 | #include <rtthread.h> |
32 | | - |
33 | 31 | #ifdef RT_USING_DFS |
34 | 32 | #include <dfs_posix.h> |
35 | 33 | #endif |
36 | | - |
37 | 34 | #include <py/compile.h> |
38 | 35 | #include <py/runtime.h> |
39 | 36 | #include <py/repl.h> |
@@ -66,13 +63,87 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) { |
66 | 63 | } |
67 | 64 | #endif |
68 | 65 |
|
| 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 | + |
69 | 135 | static void *stack_top = RT_NULL; |
70 | 136 | static char *heap = RT_NULL; |
71 | 137 |
|
72 | 138 | void mpy_main(const char *filename) { |
73 | 139 | int stack_dummy; |
74 | 140 | int stack_size_check; |
75 | 141 | 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 |
76 | 147 |
|
77 | 148 | mp_getchar_init(); |
78 | 149 | mp_putsn_init(); |
@@ -167,6 +238,10 @@ void mpy_main(const char *filename) { |
167 | 238 |
|
168 | 239 | mp_putsn_deinit(); |
169 | 240 | mp_getchar_deinit(); |
| 241 | + |
| 242 | +#ifdef RT_USING_DFS |
| 243 | + mp_sys_resource_gc(fd_table_bak); |
| 244 | +#endif |
170 | 245 | } |
171 | 246 |
|
172 | 247 | #if !MICROPY_PY_MODUOS_FILE |
|
0 commit comments