summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2018-09-16 13:41:30 +0300
committerDamien George <damien.p.george@gmail.com>2018-12-20 17:40:48 +1100
commita261d8b615298a7ade5c2aebf50e97d089c56667 (patch)
treead3ace3e3e0a53203c9490028139e8cab828704c
parent39eef270831323b77445e0458c9357d38e23658a (diff)
downloadmicropython-a261d8b615298a7ade5c2aebf50e97d089c56667.tar.gz
micropython-a261d8b615298a7ade5c2aebf50e97d089c56667.zip
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.
-rw-r--r--py/objarray.c19
-rw-r--r--py/objarray.h7
2 files changed, 18 insertions, 8 deletions
diff --git a/py/objarray.c b/py/objarray.c
index bdef34135e..6f857a9eda 100644
--- a/py/objarray.c
+++ b/py/objarray.c
@@ -50,11 +50,14 @@
// Note that we don't handle the case where the original buffer might change
// size due to a resize of the original parent object.
-// make (& TYPECODE_MASK) a null operation if memorview not enabled
#if MICROPY_PY_BUILTINS_MEMORYVIEW
#define TYPECODE_MASK (0x7f)
+#define memview_offset free
#else
+// make (& TYPECODE_MASK) a null operation if memorview not enabled
#define TYPECODE_MASK (~(size_t)0)
+// memview_offset should not be accessed if memoryview is not enabled,
+// so not defined to catch errors
#endif
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) {
mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
self->base.type = &mp_type_memoryview;
self->typecode = typecode;
- self->free = 0;
+ self->memview_offset = 0;
self->len = nitems;
self->items = items;
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
src_items = src_slice->items;
#if MICROPY_PY_BUILTINS_MEMORYVIEW
if (MP_OBJ_IS_TYPE(value, &mp_type_memoryview)) {
- src_items = (uint8_t*)src_items + (src_slice->free * item_sz);
+ src_items = (uint8_t*)src_items + (src_slice->memview_offset * item_sz);
}
#endif
} 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
if (len_adj != 0) {
goto compat_error;
}
- dest_items += o->free * item_sz;
+ dest_items += o->memview_offset * item_sz;
}
#endif
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
} else if (o->base.type == &mp_type_memoryview) {
res = m_new_obj(mp_obj_array_t);
*res = *o;
- res->free += slice.start;
+ res->memview_offset += slice.start;
res->len = slice.stop - slice.start;
#endif
} 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
size_t index = mp_get_index(o->base.type, o->len, index_in, false);
#if MICROPY_PY_BUILTINS_MEMORYVIEW
if (o->base.type == &mp_type_memoryview) {
- index += o->free;
+ index += o->memview_offset;
if (value != MP_OBJ_SENTINEL && !(o->typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) {
// store to read-only memoryview
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
// read-only memoryview
return 1;
}
- bufinfo->buf = (uint8_t*)bufinfo->buf + (size_t)o->free * sz;
+ bufinfo->buf = (uint8_t*)bufinfo->buf + (size_t)o->memview_offset * sz;
}
#else
(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
o->cur = 0;
#if MICROPY_PY_BUILTINS_MEMORYVIEW
if (array->base.type == &mp_type_memoryview) {
- o->offset = array->free;
+ o->offset = array->memview_offset;
}
#endif
return MP_OBJ_FROM_PTR(o);
diff --git a/py/objarray.h b/py/objarray.h
index 0dad705711..2fb6e2c915 100644
--- a/py/objarray.h
+++ b/py/objarray.h
@@ -32,11 +32,18 @@
// Used only for memoryview types, set in "typecode" to indicate a writable memoryview
#define MP_OBJ_ARRAY_TYPECODE_FLAG_RW (0x80)
+// This structure is used for all of bytearray, array.array, memoryview
+// objects. Note that memoryview has different meaning for some fields,
+// see comment at the beginning of objarray.c.
typedef struct _mp_obj_array_t {
mp_obj_base_t base;
size_t typecode : 8;
// free is number of unused elements after len used elements
// alloc size = len + free
+ // But for memoryview, 'free' is reused as offset (in elements) into the
+ // parent object. (Union is not used to not go into a complication of
+ // union-of-bitfields with different toolchains). See comments in
+ // objarray.c.
size_t free : (8 * sizeof(size_t) - 8);
size_t len; // in elements
void *items;