diff options
author | Damien George <damien.p.george@gmail.com> | 2018-02-28 15:11:20 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-02-28 15:11:20 +1100 |
commit | 09be031e047f0e7ba77620c29c5abbdbd5e3d055 (patch) | |
tree | e59d9d3045d9f96e26ce5ed827aaf6b18a87fef7 | |
parent | 439acddc6052f112e8c2e57a6d50c7569344a6bf (diff) |
extmod/vfs_fat_diskio: Use a C-stack-allocated bytearray for block buf.
This patch eliminates heap allocation in the VFS FAT disk IO layer, when
calling the underlying readblocks/writeblocks methods. The bytearray
object that is passed to these methods is now allocated on the C stack
rather than the heap (it's only 4 words big).
This means that these methods should not retain a pointer to the buffer
object that is passed in, but this was already a restriction because the
original heap-allocated bytearray had its buffer passed by reference.
-rw-r--r-- | extmod/vfs_fat_diskio.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index ff23c6b0c..712038f3e 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -36,6 +36,8 @@ #include "py/mphal.h" #include "py/runtime.h" +#include "py/binary.h" +#include "py/objarray.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" #include "extmod/vfs_fat.h" @@ -126,8 +128,9 @@ DRESULT disk_read ( return RES_ERROR; } } else { + mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, count * SECSIZE(&vfs->fatfs), buff}; vfs->readblocks[2] = MP_OBJ_NEW_SMALL_INT(sector); - vfs->readblocks[3] = mp_obj_new_bytearray_by_ref(count * SECSIZE(&vfs->fatfs), buff); + vfs->readblocks[3] = MP_OBJ_FROM_PTR(&ar); mp_call_method_n_kw(2, 0, vfs->readblocks); // TODO handle error return } @@ -162,8 +165,9 @@ DRESULT disk_write ( return RES_ERROR; } } else { + mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, count * SECSIZE(&vfs->fatfs), (void*)buff}; vfs->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(sector); - vfs->writeblocks[3] = mp_obj_new_bytearray_by_ref(count * SECSIZE(&vfs->fatfs), (void*)buff); + vfs->writeblocks[3] = MP_OBJ_FROM_PTR(&ar); mp_call_method_n_kw(2, 0, vfs->writeblocks); // TODO handle error return } |