Skip to content

Commit a4c8a1f

Browse files
committed
extmod/moduos_dupterm: Reuse dupterm_arr_obj for write operations.
Instead of allocating new array object header again and again, causing memory fragmentation.
1 parent ec7fe92 commit a4c8a1f

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

extmod/moduos_dupterm.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "py/nlr.h"
3232
#include "py/runtime.h"
3333
#include "py/objtuple.h"
34+
#include "py/objarray.h"
3435
#include "py/stream.h"
3536

3637
#if MICROPY_PY_OS_DUPTERM
@@ -51,8 +52,16 @@ void mp_uos_dupterm_tx_strn(const char *str, size_t len) {
5152
if (nlr_push(&nlr) == 0) {
5253
mp_obj_t write_m[3];
5354
mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m);
54-
write_m[2] = mp_obj_new_bytearray_by_ref(len, (char*)str);
55+
56+
mp_obj_array_t *arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj));
57+
void *org_items = arr->items;
58+
arr->items = (void*)str;
59+
arr->len = len;
60+
write_m[2] = MP_STATE_PORT(dupterm_arr_obj);
5561
mp_call_method_n_kw(1, 0, write_m);
62+
arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj));
63+
arr->items = org_items;
64+
arr->len = 1;
5665
nlr_pop();
5766
} else {
5867
mp_uos_deactivate("dupterm: Exception in write() method, deactivating: ", nlr.ret_val);

0 commit comments

Comments
 (0)