Skip to content

Commit e9995bd

Browse files
committed
moduzlib: Align out buffer to block size; shrink when decompression done.
1 parent 2324f3e commit e9995bd

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

extmod/moduzlib.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ STATIC mp_obj_t mod_uzlib_decompress(mp_uint_t n_args, const mp_obj_t *args) {
6363
TINF_DATA *decomp = m_new_obj(TINF_DATA);
6464
DEBUG_printf("sizeof(TINF_DATA)=" UINT_FMT "\n", sizeof(*decomp));
6565

66-
decomp->destStart = m_new(byte, bufinfo.len);
67-
decomp->destSize = bufinfo.len;
66+
decomp->destSize = (bufinfo.len + 15) & ~15;
67+
decomp->destStart = m_new(byte, decomp->destSize);
68+
DEBUG_printf("uzlib: Initial out buffer: " UINT_FMT " bytes\n", decomp->destSize);
6869
decomp->destGrow = mod_uzlib_grow_buf;
6970
decomp->source = bufinfo.buf;
7071

@@ -78,7 +79,10 @@ STATIC mp_obj_t mod_uzlib_decompress(mp_uint_t n_args, const mp_obj_t *args) {
7879
nlr_raise(mp_obj_new_exception_arg1(&mp_type_ValueError, MP_OBJ_NEW_SMALL_INT(st)));
7980
}
8081

81-
mp_obj_t res = mp_obj_new_bytearray_by_ref(decomp->dest - decomp->destStart, decomp->destStart);
82+
mp_uint_t final_sz = decomp->dest - decomp->destStart;
83+
DEBUG_printf("uzlib: Resizing from " UINT_FMT " to final size: " UINT_FMT " bytes\n", decomp->destSize, final_sz);
84+
decomp->destStart = (byte*)m_renew(byte, decomp->destStart, decomp->destSize, final_sz);
85+
mp_obj_t res = mp_obj_new_bytearray_by_ref(final_sz, decomp->destStart);
8286
m_del_obj(TINF_DATA, decomp);
8387
return res;
8488
}

0 commit comments

Comments
 (0)