Skip to content

Commit a261d8b

Browse files
pfalcondpgeorge
authored andcommitted
py/objarray: Introduce "memview_offset" alias for "free" field of object
Both mp_type_array and mp_type_memoryview use the same object structure, mp_obj_array_t, but for the case of memoryview, some fields, e.g. "free", have different meaning. As the "free" field is also a bitfield, assume that (anonymous) union can't be used here (for the concerns of possible compatibility issues with wide array of toolchains), and just add a field alias using a #define. As it's a define, it should be a selective identifier, so use verbose "memview_offset" to avoid any clashes.
1 parent 39eef27 commit a261d8b

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

py/objarray.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@
5050
// Note that we don't handle the case where the original buffer might change
5151
// size due to a resize of the original parent object.
5252

53-
// make (& TYPECODE_MASK) a null operation if memorview not enabled
5453
#if MICROPY_PY_BUILTINS_MEMORYVIEW
5554
#define TYPECODE_MASK (0x7f)
55+
#define memview_offset free
5656
#else
57+
// make (& TYPECODE_MASK) a null operation if memorview not enabled
5758
#define TYPECODE_MASK (~(size_t)0)
59+
// memview_offset should not be accessed if memoryview is not enabled,
60+
// so not defined to catch errors
5861
#endif
5962

6063
STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf);
@@ -200,7 +203,7 @@ mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items) {
200203
mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
201204
self->base.type = &mp_type_memoryview;
202205
self->typecode = typecode;
203-
self->free = 0;
206+
self->memview_offset = 0;
204207
self->len = nitems;
205208
self->items = items;
206209
return MP_OBJ_FROM_PTR(self);
@@ -396,7 +399,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
396399
src_items = src_slice->items;
397400
#if MICROPY_PY_BUILTINS_MEMORYVIEW
398401
if (MP_OBJ_IS_TYPE(value, &mp_type_memoryview)) {
399-
src_items = (uint8_t*)src_items + (src_slice->free * item_sz);
402+
src_items = (uint8_t*)src_items + (src_slice->memview_offset * item_sz);
400403
}
401404
#endif
402405
} else if (MP_OBJ_IS_TYPE(value, &mp_type_bytes)) {
@@ -423,7 +426,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
423426
if (len_adj != 0) {
424427
goto compat_error;
425428
}
426-
dest_items += o->free * item_sz;
429+
dest_items += o->memview_offset * item_sz;
427430
}
428431
#endif
429432
if (len_adj > 0) {
@@ -459,7 +462,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
459462
} else if (o->base.type == &mp_type_memoryview) {
460463
res = m_new_obj(mp_obj_array_t);
461464
*res = *o;
462-
res->free += slice.start;
465+
res->memview_offset += slice.start;
463466
res->len = slice.stop - slice.start;
464467
#endif
465468
} else {
@@ -472,7 +475,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
472475
size_t index = mp_get_index(o->base.type, o->len, index_in, false);
473476
#if MICROPY_PY_BUILTINS_MEMORYVIEW
474477
if (o->base.type == &mp_type_memoryview) {
475-
index += o->free;
478+
index += o->memview_offset;
476479
if (value != MP_OBJ_SENTINEL && !(o->typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) {
477480
// store to read-only memoryview
478481
return MP_OBJ_NULL;
@@ -503,7 +506,7 @@ STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_ui
503506
// read-only memoryview
504507
return 1;
505508
}
506-
bufinfo->buf = (uint8_t*)bufinfo->buf + (size_t)o->free * sz;
509+
bufinfo->buf = (uint8_t*)bufinfo->buf + (size_t)o->memview_offset * sz;
507510
}
508511
#else
509512
(void)flags;
@@ -624,7 +627,7 @@ STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_bu
624627
o->cur = 0;
625628
#if MICROPY_PY_BUILTINS_MEMORYVIEW
626629
if (array->base.type == &mp_type_memoryview) {
627-
o->offset = array->free;
630+
o->offset = array->memview_offset;
628631
}
629632
#endif
630633
return MP_OBJ_FROM_PTR(o);

py/objarray.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@
3232
// Used only for memoryview types, set in "typecode" to indicate a writable memoryview
3333
#define MP_OBJ_ARRAY_TYPECODE_FLAG_RW (0x80)
3434

35+
// This structure is used for all of bytearray, array.array, memoryview
36+
// objects. Note that memoryview has different meaning for some fields,
37+
// see comment at the beginning of objarray.c.
3538
typedef struct _mp_obj_array_t {
3639
mp_obj_base_t base;
3740
size_t typecode : 8;
3841
// free is number of unused elements after len used elements
3942
// alloc size = len + free
43+
// But for memoryview, 'free' is reused as offset (in elements) into the
44+
// parent object. (Union is not used to not go into a complication of
45+
// union-of-bitfields with different toolchains). See comments in
46+
// objarray.c.
4047
size_t free : (8 * sizeof(size_t) - 8);
4148
size_t len; // in elements
4249
void *items;

0 commit comments

Comments
 (0)