kopia lustrzana https://github.com/micropython/micropython
py/persistentcode: Support loading from VfsMap.
Signed-off-by: Damien George <damien@micropython.org>pull/8381/head
rodzic
d15c1e0e17
commit
c34bfed356
|
@ -55,6 +55,7 @@
|
|||
// At the moment the VFS protocol just has import_stat, but could be extended to other methods
|
||||
typedef struct _mp_vfs_proto_t {
|
||||
mp_import_stat_t (*import_stat)(void *self, const char *path);
|
||||
size_t (*memmap)(void *self, const char *path, const void **ptr_out);
|
||||
} mp_vfs_proto_t;
|
||||
|
||||
typedef struct _mp_vfs_blockdev_t {
|
||||
|
|
|
@ -83,6 +83,26 @@ static intptr_t mp_reader_vfs_ioctl(void *data, uintptr_t request, uintptr_t arg
|
|||
}
|
||||
|
||||
void mp_reader_new_file(mp_reader_t *reader, qstr filename) {
|
||||
#if MICROPY_VFS_MAP
|
||||
const char *path_out;
|
||||
mp_vfs_mount_t *vfs = mp_vfs_lookup_path(qstr_str(filename), &path_out);
|
||||
if (!(vfs == MP_VFS_NONE || vfs == MP_VFS_ROOT)) {
|
||||
// If the mounted object has the VFS protocol, call its memmap helper.
|
||||
const mp_obj_type_t *type = mp_obj_get_type(vfs->obj);
|
||||
if (MP_OBJ_TYPE_HAS_SLOT(type, protocol)) {
|
||||
const mp_vfs_proto_t *proto = MP_OBJ_TYPE_GET_SLOT(type, protocol);
|
||||
if (proto->memmap != NULL) {
|
||||
const void *data;
|
||||
size_t size = proto->memmap(MP_OBJ_TO_PTR(vfs->obj), path_out, &data);
|
||||
if (data != NULL) {
|
||||
mp_reader_new_mem(reader, data, size, (size_t)-1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
mp_obj_t args[2] = {
|
||||
MP_OBJ_NEW_QSTR(filename),
|
||||
MP_OBJ_NEW_QSTR(MP_QSTR_rb),
|
||||
|
@ -91,6 +111,15 @@ void mp_reader_new_file(mp_reader_t *reader, qstr filename) {
|
|||
|
||||
const mp_stream_p_t *stream_p = mp_get_stream(file);
|
||||
int errcode = 0;
|
||||
|
||||
#if 0 && MICROPY_VFS_MAP
|
||||
// Check if the stream can initialising a reader itself.
|
||||
mp_uint_t reader_ret = stream_p->ioctl(file, MP_STREAM_INIT_READER, (uintptr_t)reader, &errcode);
|
||||
if (reader_ret == 0) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
mp_uint_t bufsize = stream_p->ioctl(file, MP_STREAM_GET_BUFFER_SIZE, 0, &errcode);
|
||||
if (bufsize == MP_STREAM_ERROR || bufsize == 0) {
|
||||
// bufsize == 0 is included here to support mpremote v1.21 and older where mount file ioctl
|
||||
|
@ -104,6 +133,7 @@ void mp_reader_new_file(mp_reader_t *reader, qstr filename) {
|
|||
rf->file = file;
|
||||
rf->bufsize = bufsize;
|
||||
rf->buflen = mp_stream_rw(rf->file, rf->buf, rf->bufsize, &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
|
||||
|
||||
if (errcode != 0) {
|
||||
mp_raise_OSError(errcode);
|
||||
}
|
||||
|
|
|
@ -160,6 +160,18 @@ static size_t read_uint(mp_reader_t *reader) {
|
|||
return unum;
|
||||
}
|
||||
|
||||
#if MICROPY_VFS_MAP
|
||||
static inline const uint8_t *map_try_read_bytes(mp_reader_t *reader, size_t len) {
|
||||
mp_reader_ioctl_memmap_t memmap;
|
||||
memmap.len = len;
|
||||
intptr_t ret = reader->ioctl(reader->data, MP_READER_MEMMAP, (uintptr_t)&memmap);
|
||||
if (ret < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return memmap.ptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
static qstr load_qstr(mp_reader_t *reader) {
|
||||
size_t len = read_uint(reader);
|
||||
if (len & 1) {
|
||||
|
@ -167,6 +179,12 @@ static qstr load_qstr(mp_reader_t *reader) {
|
|||
return len >> 1;
|
||||
}
|
||||
len >>= 1;
|
||||
#if MICROPY_VFS_MAP
|
||||
const uint8_t *memmap = map_try_read_bytes(reader, len + 1);
|
||||
if (memmap != NULL) {
|
||||
return qstr_from_strn_static((const char *)memmap, len);
|
||||
}
|
||||
#endif
|
||||
char *str = m_new(char, len);
|
||||
read_bytes(reader, (byte *)str, len);
|
||||
read_byte(reader); // read and discard null terminator
|
||||
|
@ -175,6 +193,22 @@ static qstr load_qstr(mp_reader_t *reader) {
|
|||
return qst;
|
||||
}
|
||||
|
||||
#if MICROPY_VFS_MAP
|
||||
static mp_obj_t mp_obj_new_str_static(const mp_obj_type_t *type, const byte *data, size_t len) {
|
||||
if (type == &mp_type_str) {
|
||||
qstr q = qstr_find_strn((const char *)data, len);
|
||||
if (q != MP_QSTRnull) {
|
||||
return MP_OBJ_NEW_QSTR(q);
|
||||
}
|
||||
}
|
||||
mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
|
||||
o->base.type = type;
|
||||
o->len = len;
|
||||
o->hash = qstr_compute_hash(data, len);
|
||||
o->data = data;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
}
|
||||
#endif
|
||||
static mp_obj_t load_obj(mp_reader_t *reader) {
|
||||
byte obj_type = read_byte(reader);
|
||||
#if MICROPY_EMIT_MACHINE_CODE
|
||||
|
@ -192,6 +226,7 @@ static mp_obj_t load_obj(mp_reader_t *reader) {
|
|||
return MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj);
|
||||
} else {
|
||||
size_t len = read_uint(reader);
|
||||
|
||||
if (len == 0 && obj_type == MP_PERSISTENT_OBJ_BYTES) {
|
||||
read_byte(reader); // skip null terminator
|
||||
return mp_const_empty_bytes;
|
||||
|
@ -202,11 +237,25 @@ static mp_obj_t load_obj(mp_reader_t *reader) {
|
|||
}
|
||||
return MP_OBJ_FROM_PTR(tuple);
|
||||
}
|
||||
|
||||
|
||||
const uint8_t *memmap = NULL;
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, len);
|
||||
read_bytes(reader, (byte *)vstr.buf, len);
|
||||
#if MICROPY_VFS_MAP
|
||||
memmap = map_try_read_bytes(reader, len);
|
||||
#endif
|
||||
if (memmap == NULL) {
|
||||
vstr_init_len(&vstr, len);
|
||||
read_bytes(reader, (byte *)vstr.buf, len);
|
||||
}
|
||||
if (obj_type == MP_PERSISTENT_OBJ_STR || obj_type == MP_PERSISTENT_OBJ_BYTES) {
|
||||
read_byte(reader); // skip null terminator
|
||||
#if MICROPY_VFS_MAP
|
||||
if (memmap != NULL) {
|
||||
const mp_obj_type_t *t = obj_type == MP_PERSISTENT_OBJ_STR ? &mp_type_str : &mp_type_bytes;
|
||||
return mp_obj_new_str_static(t, memmap, len);
|
||||
}
|
||||
#endif
|
||||
if (obj_type == MP_PERSISTENT_OBJ_STR) {
|
||||
return mp_obj_new_str_from_utf8_vstr(&vstr);
|
||||
} else {
|
||||
|
@ -243,10 +292,17 @@ static mp_raw_code_t *load_raw_code(mp_reader_t *reader, mp_module_context_t *co
|
|||
#endif
|
||||
|
||||
if (kind == MP_CODE_BYTECODE) {
|
||||
// Allocate memory for the bytecode
|
||||
fun_data = m_new(uint8_t, fun_data_len);
|
||||
// Load bytecode
|
||||
read_bytes(reader, fun_data, fun_data_len);
|
||||
#if MICROPY_VFS_MAP
|
||||
// Try to reference memory-mapped data for the bytecode.
|
||||
fun_data = (uint8_t *)map_try_read_bytes(reader, fun_data_len);
|
||||
#endif
|
||||
|
||||
if (fun_data == NULL) {
|
||||
// Allocate memory for the bytecode.
|
||||
fun_data = m_new(uint8_t, fun_data_len);
|
||||
// Load bytecode.
|
||||
read_bytes(reader, fun_data, fun_data_len);
|
||||
}
|
||||
|
||||
#if MICROPY_EMIT_MACHINE_CODE
|
||||
} else {
|
||||
|
|
Ładowanie…
Reference in New Issue