diff options
| author | Radomir Dopieralski <openstack@sheep.art.pl> | 2021-02-21 16:25:17 +0100 |
|---|---|---|
| committer | Radomir Dopieralski <openstack@sheep.art.pl> | 2021-02-27 20:52:38 +0100 |
| commit | e505c59ed838095ccb2bd996fc77bd4aeada2d80 (patch) | |
| tree | 9e724f7edfa6413da8ac259757a6ad624b7ffad6 | |
| parent | 121c6bcc9b20d7419f2654680215fd8b4bd455c4 (diff) | |
Separate mp_obj_list_pop so it can be used outside of objlist.c
| -rw-r--r-- | py/objlist.c | 12 | ||||
| -rw-r--r-- | py/objlist.h | 1 | ||||
| -rw-r--r-- | shared-module/displayio/Group.c | 25 |
3 files changed, 11 insertions, 27 deletions
diff --git a/py/objlist.c b/py/objlist.c index 51ec920be..309e0e542 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -270,13 +270,10 @@ STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) { return mp_const_none; // return None, as per CPython } -STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) { - mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list)); - mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list); +mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) { if (self->len == 0) { mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list); } - size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false); mp_obj_t ret = self->items[index]; self->len -= 1; memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t)); @@ -289,6 +286,13 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) { return ret; } +STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) { + mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list)); + mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list); + size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false); + return mp_obj_list_pop(self, index); +} + STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, mp_obj_t binop_less_result) { MP_STACK_CHECK(); while (head < tail) { diff --git a/py/objlist.h b/py/objlist.h index f02030557..cec23e06c 100644 --- a/py/objlist.h +++ b/py/objlist.h @@ -36,5 +36,6 @@ typedef struct _mp_obj_list_t { } mp_obj_list_t; void mp_obj_list_init(mp_obj_list_t *o, size_t n); +mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index); #endif // MICROPY_INCLUDED_PY_OBJLIST_H diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c index 48f869489..97bb86c9a 100644 --- a/shared-module/displayio/Group.c +++ b/shared-module/displayio/Group.c @@ -27,33 +27,13 @@ #include "shared-bindings/displayio/Group.h" #include "py/runtime.h" +#include "py/objlist.h" #include "shared-bindings/displayio/TileGrid.h" #if CIRCUITPY_VECTORIO #include "shared-bindings/vectorio/VectorShape.h" #endif -#include <string.h> -#define LIST_MIN_ALLOC 4 - -STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) { - mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list)); - mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list); - if (self->len == 0) { - mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list); - } - size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false); - mp_obj_t ret = self->items[index]; - self->len -= 1; - memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t)); - // Clear stale pointer from slot which just got freed to prevent GC issues - self->items[self->len] = MP_OBJ_NULL; - if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) { - self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2); - self->alloc /= 2; - } - return ret; -} void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y) { mp_obj_list_t *members = mp_obj_new_list(0, NULL); @@ -343,8 +323,7 @@ void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) { _remove_layer(self, index); - mp_obj_t args[] = {self->members, MP_OBJ_NEW_SMALL_INT(index)}; - return list_pop(2, args); + return mp_obj_list_pop(self->members, index); } mp_int_t common_hal_displayio_group_index(displayio_group_t* self, mp_obj_t layer) { |
