all: Simplify buffer protocol to just a "get buffer" callback.

The buffer protocol type only has a single member, and this existing layout
creates problems for the upcoming split/slot-index mp_obj_type_t layout
optimisations.

If we need to make the buffer protocol more sophisticated in the future
either we can rely on the mp_obj_type_t optimisations to just add
additional slots to mp_obj_type_t or re-visit the buffer protocol then.

This change is a no-op in terms of generated code.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared 2021-07-15 14:31:06 +10:00 committed by Damien George
parent ca51d63c37
commit fb2a57800a
14 changed files with 19 additions and 24 deletions

View File

@ -21,7 +21,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
mp_type_framebuf.base.type = (void*)&mp_type_type;
mp_type_framebuf.name = MP_QSTR_FrameBuffer;
mp_type_framebuf.make_new = framebuf_make_new;
mp_type_framebuf.buffer_p.get_buffer = framebuf_get_buffer;
mp_type_framebuf.buffer = framebuf_get_buffer;
framebuf_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_fill), MP_OBJ_FROM_PTR(&framebuf_fill_obj) };
framebuf_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_fill_rect), MP_OBJ_FROM_PTR(&framebuf_fill_rect_obj) };
framebuf_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_pixel), MP_OBJ_FROM_PTR(&framebuf_pixel_obj) };

View File

@ -248,7 +248,7 @@ const mp_obj_type_t mp_type_bluetooth_uuid = {
.binary_op = bluetooth_uuid_binary_op,
.locals_dict = NULL,
.print = bluetooth_uuid_print,
.buffer_p = { .get_buffer = bluetooth_uuid_get_buffer },
.buffer = bluetooth_uuid_get_buffer,
};
// ----------------------------------------------------------------------------

View File

@ -833,7 +833,7 @@ STATIC const mp_obj_type_t mp_type_framebuf = {
{ &mp_type_type },
.name = MP_QSTR_FrameBuffer,
.make_new = framebuf_make_new,
.buffer_p = { .get_buffer = framebuf_get_buffer },
.buffer = framebuf_get_buffer,
.locals_dict = (mp_obj_dict_t *)&framebuf_locals_dict,
};
#endif

View File

@ -642,7 +642,7 @@ STATIC const mp_obj_type_t uctypes_struct_type = {
.attr = uctypes_struct_attr,
.subscr = uctypes_struct_subscr,
.unary_op = uctypes_struct_unary_op,
.buffer_p = { .get_buffer = uctypes_get_buffer },
.buffer = uctypes_get_buffer,
};
STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = {

View File

@ -55,7 +55,6 @@ const mp_obj_type_t microbit_repeat_iterator_type = {
.subscr = NULL,
.getiter = mp_identity_getiter,
.iternext = microbit_repeat_iter_next,
.buffer_p = {NULL},
MP_OBJ_NULL
};

View File

@ -554,7 +554,6 @@ const mp_obj_type_t microbit_display_type = {
.subscr = NULL,
.getiter = NULL,
.iternext = NULL,
.buffer_p = {NULL},
.locals_dict = (mp_obj_dict_t*)&microbit_display_locals_dict,
};

View File

@ -690,7 +690,6 @@ const mp_obj_type_t microbit_image_type = {
.subscr = NULL,
.getiter = NULL,
.iternext = NULL,
.buffer_p = {NULL},
.locals_dict = (mp_obj_dict_t*)&microbit_image_locals_dict,
};

View File

@ -505,10 +505,10 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
} else if (mp_obj_is_str(a)) {
const char *s = mp_obj_str_get_str(a);
values[i].ffi = (ffi_arg)(intptr_t)s;
} else if (((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type->buffer_p.get_buffer != NULL) {
} else if (((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type->buffer != NULL) {
mp_obj_base_t *o = (mp_obj_base_t *)MP_OBJ_TO_PTR(a);
mp_buffer_info_t bufinfo;
int ret = o->type->buffer_p.get_buffer(MP_OBJ_FROM_PTR(o), &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ?
int ret = o->type->buffer(MP_OBJ_FROM_PTR(o), &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ?
if (ret != 0) {
goto error;
}

View File

@ -579,10 +579,10 @@ mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
const mp_obj_type_t *type = mp_obj_get_type(obj);
if (type->buffer_p.get_buffer == NULL) {
if (type->buffer == NULL) {
return false;
}
int ret = type->buffer_p.get_buffer(obj, bufinfo, flags);
int ret = type->buffer(obj, bufinfo, flags);
if (ret != 0) {
return false;
}

View File

@ -557,9 +557,7 @@ typedef struct _mp_buffer_info_t {
#define MP_BUFFER_READ (1)
#define MP_BUFFER_WRITE (2)
#define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
typedef struct _mp_buffer_p_t {
mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
} mp_buffer_p_t;
typedef mp_int_t (*mp_buffer_fun_t)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
@ -618,7 +616,7 @@ struct _mp_obj_type_t {
mp_fun_1_t iternext;
// Implements the buffer protocol if supported by this type.
mp_buffer_p_t buffer_p;
mp_buffer_fun_t buffer;
// One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
const void *protocol;

View File

@ -580,7 +580,7 @@ const mp_obj_type_t mp_type_array = {
.unary_op = array_unary_op,
.binary_op = array_binary_op,
.subscr = array_subscr,
.buffer_p = { .get_buffer = array_get_buffer },
.buffer_p = array_get_buffer,
.locals_dict = (mp_obj_dict_t *)&mp_obj_array_locals_dict,
};
#endif
@ -596,7 +596,7 @@ const mp_obj_type_t mp_type_bytearray = {
.unary_op = array_unary_op,
.binary_op = array_binary_op,
.subscr = array_subscr,
.buffer_p = { .get_buffer = array_get_buffer },
.buffer = array_get_buffer,
.locals_dict = (mp_obj_dict_t *)&mp_obj_bytearray_locals_dict,
};
#endif
@ -617,7 +617,7 @@ const mp_obj_type_t mp_type_memoryview = {
.locals_dict = (mp_obj_dict_t *)&mp_obj_memoryview_locals_dict,
#endif
.subscr = array_subscr,
.buffer_p = { .get_buffer = array_get_buffer },
.buffer = array_get_buffer,
};
#endif

View File

@ -2151,7 +2151,7 @@ const mp_obj_type_t mp_type_str = {
.binary_op = mp_obj_str_binary_op,
.subscr = bytes_subscr,
.getiter = mp_obj_new_str_iterator,
.buffer_p = { .get_buffer = mp_obj_str_get_buffer },
.buffer = mp_obj_str_get_buffer,
.locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict,
};
#endif // !MICROPY_PY_BUILTINS_STR_UNICODE
@ -2165,7 +2165,7 @@ const mp_obj_type_t mp_type_bytes = {
.binary_op = mp_obj_str_binary_op,
.subscr = bytes_subscr,
.getiter = mp_obj_new_bytes_iterator,
.buffer_p = { .get_buffer = mp_obj_str_get_buffer },
.buffer = mp_obj_str_get_buffer,
.locals_dict = (mp_obj_dict_t *)&mp_obj_bytes_locals_dict,
};

View File

@ -238,7 +238,7 @@ const mp_obj_type_t mp_type_str = {
.binary_op = mp_obj_str_binary_op,
.subscr = str_subscr,
.getiter = mp_obj_new_str_iterator,
.buffer_p = { .get_buffer = mp_obj_str_get_buffer },
.buffer = mp_obj_str_get_buffer,
.locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict,
};

View File

@ -913,14 +913,14 @@ STATIC mp_int_t instance_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo,
struct class_lookup_data lookup = {
.obj = self,
.attr = MP_QSTR_, // don't actually look for a method
.meth_offset = offsetof(mp_obj_type_t, buffer_p.get_buffer),
.meth_offset = offsetof(mp_obj_type_t, buffer),
.dest = member,
.is_type = false,
};
mp_obj_class_lookup(&lookup, self->base.type);
if (member[0] == MP_OBJ_SENTINEL) {
const mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
return type->buffer_p.get_buffer(self->subobj[0], bufinfo, flags);
return type->buffer(self->subobj[0], bufinfo, flags);
} else {
return 1; // object does not support buffer protocol
}
@ -1160,7 +1160,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
o->subscr = instance_subscr;
o->getiter = mp_obj_instance_getiter;
// o->iternext = ; not implemented
o->buffer_p.get_buffer = instance_get_buffer;
o->buffer = instance_get_buffer;
if (bases_len > 0) {
// Inherit protocol from a base class. This allows to define an