Skip to content

Commit 5e9dd4b

Browse files
committed
extmod/vfs_reader: Add support for opening a memory-mappable file.
If the file can be memory mapped (because it responds to the buffer protocol) then return a memory-reader that directly references the ROM data of the file. Signed-off-by: Damien George <[email protected]>
1 parent f870e8d commit 5e9dd4b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Diff for: extmod/vfs_reader.c

+12
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ void mp_reader_new_file(mp_reader_t *reader, qstr filename) {
8585

8686
const mp_stream_p_t *stream_p = mp_get_stream(file);
8787
int errcode = 0;
88+
89+
#if MICROPY_VFS_ROM
90+
// Check if the stream can be memory mapped.
91+
mp_buffer_info_t bufinfo;
92+
if (mp_get_buffer(file, &bufinfo, MP_BUFFER_READ)) {
93+
mp_reader_new_mem(reader, bufinfo.buf, bufinfo.len, MP_READER_IS_ROM);
94+
return;
95+
}
96+
#endif
97+
98+
// Determine how big the input buffer should be, if the stream requests a certain size or not.
8899
mp_uint_t bufsize = stream_p->ioctl(file, MP_STREAM_GET_BUFFER_SIZE, 0, &errcode);
89100
if (bufsize == MP_STREAM_ERROR || bufsize == 0) {
90101
// bufsize == 0 is included here to support mpremote v1.21 and older where mount file ioctl
@@ -94,6 +105,7 @@ void mp_reader_new_file(mp_reader_t *reader, qstr filename) {
94105
bufsize = MIN(MICROPY_READER_VFS_MAX_BUFFER_SIZE, MAX(MICROPY_READER_VFS_MIN_BUFFER_SIZE, bufsize));
95106
}
96107

108+
// Create the reader.
97109
mp_reader_vfs_t *rf = m_new_obj_var(mp_reader_vfs_t, buf, byte, bufsize);
98110
rf->file = file;
99111
rf->bufsize = bufsize;

0 commit comments

Comments
 (0)