Skip to content

Commit 09be031

Browse files
committed
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.
1 parent 439acdd commit 09be031

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

extmod/vfs_fat_diskio.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "py/mphal.h"
3737

3838
#include "py/runtime.h"
39+
#include "py/binary.h"
40+
#include "py/objarray.h"
3941
#include "lib/oofatfs/ff.h"
4042
#include "lib/oofatfs/diskio.h"
4143
#include "extmod/vfs_fat.h"
@@ -126,8 +128,9 @@ DRESULT disk_read (
126128
return RES_ERROR;
127129
}
128130
} else {
131+
mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, count * SECSIZE(&vfs->fatfs), buff};
129132
vfs->readblocks[2] = MP_OBJ_NEW_SMALL_INT(sector);
130-
vfs->readblocks[3] = mp_obj_new_bytearray_by_ref(count * SECSIZE(&vfs->fatfs), buff);
133+
vfs->readblocks[3] = MP_OBJ_FROM_PTR(&ar);
131134
mp_call_method_n_kw(2, 0, vfs->readblocks);
132135
// TODO handle error return
133136
}
@@ -162,8 +165,9 @@ DRESULT disk_write (
162165
return RES_ERROR;
163166
}
164167
} else {
168+
mp_obj_array_t ar = {{&mp_type_bytearray}, BYTEARRAY_TYPECODE, 0, count * SECSIZE(&vfs->fatfs), (void*)buff};
165169
vfs->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(sector);
166-
vfs->writeblocks[3] = mp_obj_new_bytearray_by_ref(count * SECSIZE(&vfs->fatfs), (void*)buff);
170+
vfs->writeblocks[3] = MP_OBJ_FROM_PTR(&ar);
167171
mp_call_method_n_kw(2, 0, vfs->writeblocks);
168172
// TODO handle error return
169173
}

0 commit comments

Comments
 (0)