Skip to content

Commit fb2a578

Browse files
jimmodpgeorge
authored andcommitted
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 <[email protected]>
1 parent ca51d63 commit fb2a578

File tree

14 files changed

+19
-24
lines changed

14 files changed

+19
-24
lines changed

examples/natmod/framebuf/framebuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
2121
mp_type_framebuf.base.type = (void*)&mp_type_type;
2222
mp_type_framebuf.name = MP_QSTR_FrameBuffer;
2323
mp_type_framebuf.make_new = framebuf_make_new;
24-
mp_type_framebuf.buffer_p.get_buffer = framebuf_get_buffer;
24+
mp_type_framebuf.buffer = framebuf_get_buffer;
2525
framebuf_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_fill), MP_OBJ_FROM_PTR(&framebuf_fill_obj) };
2626
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) };
2727
framebuf_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_pixel), MP_OBJ_FROM_PTR(&framebuf_pixel_obj) };

extmod/modbluetooth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ const mp_obj_type_t mp_type_bluetooth_uuid = {
248248
.binary_op = bluetooth_uuid_binary_op,
249249
.locals_dict = NULL,
250250
.print = bluetooth_uuid_print,
251-
.buffer_p = { .get_buffer = bluetooth_uuid_get_buffer },
251+
.buffer = bluetooth_uuid_get_buffer,
252252
};
253253

254254
// ----------------------------------------------------------------------------

extmod/modframebuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ STATIC const mp_obj_type_t mp_type_framebuf = {
833833
{ &mp_type_type },
834834
.name = MP_QSTR_FrameBuffer,
835835
.make_new = framebuf_make_new,
836-
.buffer_p = { .get_buffer = framebuf_get_buffer },
836+
.buffer = framebuf_get_buffer,
837837
.locals_dict = (mp_obj_dict_t *)&framebuf_locals_dict,
838838
};
839839
#endif

extmod/moductypes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ STATIC const mp_obj_type_t uctypes_struct_type = {
642642
.attr = uctypes_struct_attr,
643643
.subscr = uctypes_struct_subscr,
644644
.unary_op = uctypes_struct_unary_op,
645-
.buffer_p = { .get_buffer = uctypes_get_buffer },
645+
.buffer = uctypes_get_buffer,
646646
};
647647

648648
STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = {

ports/nrf/boards/microbit/modules/iters.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ const mp_obj_type_t microbit_repeat_iterator_type = {
5555
.subscr = NULL,
5656
.getiter = mp_identity_getiter,
5757
.iternext = microbit_repeat_iter_next,
58-
.buffer_p = {NULL},
5958
MP_OBJ_NULL
6059
};
6160

ports/nrf/boards/microbit/modules/microbitdisplay.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@ const mp_obj_type_t microbit_display_type = {
554554
.subscr = NULL,
555555
.getiter = NULL,
556556
.iternext = NULL,
557-
.buffer_p = {NULL},
558557
.locals_dict = (mp_obj_dict_t*)&microbit_display_locals_dict,
559558
};
560559

ports/nrf/boards/microbit/modules/microbitimage.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,6 @@ const mp_obj_type_t microbit_image_type = {
690690
.subscr = NULL,
691691
.getiter = NULL,
692692
.iternext = NULL,
693-
.buffer_p = {NULL},
694693
.locals_dict = (mp_obj_dict_t*)&microbit_image_locals_dict,
695694
};
696695

ports/unix/modffi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,10 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
505505
} else if (mp_obj_is_str(a)) {
506506
const char *s = mp_obj_str_get_str(a);
507507
values[i].ffi = (ffi_arg)(intptr_t)s;
508-
} else if (((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type->buffer_p.get_buffer != NULL) {
508+
} else if (((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type->buffer != NULL) {
509509
mp_obj_base_t *o = (mp_obj_base_t *)MP_OBJ_TO_PTR(a);
510510
mp_buffer_info_t bufinfo;
511-
int ret = o->type->buffer_p.get_buffer(MP_OBJ_FROM_PTR(o), &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ?
511+
int ret = o->type->buffer(MP_OBJ_FROM_PTR(o), &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ?
512512
if (ret != 0) {
513513
goto error;
514514
}

py/obj.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,10 @@ mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
579579

580580
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
581581
const mp_obj_type_t *type = mp_obj_get_type(obj);
582-
if (type->buffer_p.get_buffer == NULL) {
582+
if (type->buffer == NULL) {
583583
return false;
584584
}
585-
int ret = type->buffer_p.get_buffer(obj, bufinfo, flags);
585+
int ret = type->buffer(obj, bufinfo, flags);
586586
if (ret != 0) {
587587
return false;
588588
}

py/obj.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,7 @@ typedef struct _mp_buffer_info_t {
557557
#define MP_BUFFER_READ (1)
558558
#define MP_BUFFER_WRITE (2)
559559
#define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
560-
typedef struct _mp_buffer_p_t {
561-
mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
562-
} mp_buffer_p_t;
560+
typedef mp_int_t (*mp_buffer_fun_t)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
563561
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
564562
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
565563

@@ -618,7 +616,7 @@ struct _mp_obj_type_t {
618616
mp_fun_1_t iternext;
619617

620618
// Implements the buffer protocol if supported by this type.
621-
mp_buffer_p_t buffer_p;
619+
mp_buffer_fun_t buffer;
622620

623621
// One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
624622
const void *protocol;

py/objarray.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ const mp_obj_type_t mp_type_array = {
580580
.unary_op = array_unary_op,
581581
.binary_op = array_binary_op,
582582
.subscr = array_subscr,
583-
.buffer_p = { .get_buffer = array_get_buffer },
583+
.buffer_p = array_get_buffer,
584584
.locals_dict = (mp_obj_dict_t *)&mp_obj_array_locals_dict,
585585
};
586586
#endif
@@ -596,7 +596,7 @@ const mp_obj_type_t mp_type_bytearray = {
596596
.unary_op = array_unary_op,
597597
.binary_op = array_binary_op,
598598
.subscr = array_subscr,
599-
.buffer_p = { .get_buffer = array_get_buffer },
599+
.buffer = array_get_buffer,
600600
.locals_dict = (mp_obj_dict_t *)&mp_obj_bytearray_locals_dict,
601601
};
602602
#endif
@@ -617,7 +617,7 @@ const mp_obj_type_t mp_type_memoryview = {
617617
.locals_dict = (mp_obj_dict_t *)&mp_obj_memoryview_locals_dict,
618618
#endif
619619
.subscr = array_subscr,
620-
.buffer_p = { .get_buffer = array_get_buffer },
620+
.buffer = array_get_buffer,
621621
};
622622
#endif
623623

py/objstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,7 +2151,7 @@ const mp_obj_type_t mp_type_str = {
21512151
.binary_op = mp_obj_str_binary_op,
21522152
.subscr = bytes_subscr,
21532153
.getiter = mp_obj_new_str_iterator,
2154-
.buffer_p = { .get_buffer = mp_obj_str_get_buffer },
2154+
.buffer = mp_obj_str_get_buffer,
21552155
.locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict,
21562156
};
21572157
#endif // !MICROPY_PY_BUILTINS_STR_UNICODE
@@ -2165,7 +2165,7 @@ const mp_obj_type_t mp_type_bytes = {
21652165
.binary_op = mp_obj_str_binary_op,
21662166
.subscr = bytes_subscr,
21672167
.getiter = mp_obj_new_bytes_iterator,
2168-
.buffer_p = { .get_buffer = mp_obj_str_get_buffer },
2168+
.buffer = mp_obj_str_get_buffer,
21692169
.locals_dict = (mp_obj_dict_t *)&mp_obj_bytes_locals_dict,
21702170
};
21712171

py/objstrunicode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ const mp_obj_type_t mp_type_str = {
238238
.binary_op = mp_obj_str_binary_op,
239239
.subscr = str_subscr,
240240
.getiter = mp_obj_new_str_iterator,
241-
.buffer_p = { .get_buffer = mp_obj_str_get_buffer },
241+
.buffer = mp_obj_str_get_buffer,
242242
.locals_dict = (mp_obj_dict_t *)&mp_obj_str_locals_dict,
243243
};
244244

py/objtype.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,14 +913,14 @@ STATIC mp_int_t instance_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo,
913913
struct class_lookup_data lookup = {
914914
.obj = self,
915915
.attr = MP_QSTR_, // don't actually look for a method
916-
.meth_offset = offsetof(mp_obj_type_t, buffer_p.get_buffer),
916+
.meth_offset = offsetof(mp_obj_type_t, buffer),
917917
.dest = member,
918918
.is_type = false,
919919
};
920920
mp_obj_class_lookup(&lookup, self->base.type);
921921
if (member[0] == MP_OBJ_SENTINEL) {
922922
const mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]);
923-
return type->buffer_p.get_buffer(self->subobj[0], bufinfo, flags);
923+
return type->buffer(self->subobj[0], bufinfo, flags);
924924
} else {
925925
return 1; // object does not support buffer protocol
926926
}
@@ -1160,7 +1160,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
11601160
o->subscr = instance_subscr;
11611161
o->getiter = mp_obj_instance_getiter;
11621162
// o->iternext = ; not implemented
1163-
o->buffer_p.get_buffer = instance_get_buffer;
1163+
o->buffer = instance_get_buffer;
11641164

11651165
if (bases_len > 0) {
11661166
// Inherit protocol from a base class. This allows to define an

0 commit comments

Comments
 (0)