summaryrefslogtreecommitdiffstatshomepage
path: root/py/objlist.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-27 23:16:20 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-27 23:16:20 +0000
commitc7aa9fcae571a442a50c90409396788dd0b28c24 (patch)
tree8959e333f1d9183fbfb781ed974a17f1e27d71c7 /py/objlist.c
parent4e8dc8c41b6f68269571fe218f7292fc86cf3ddb (diff)
parent9b00dad7bb0125a3459ca4f9c939c7510bd2f77f (diff)
downloadmicropython-c7aa9fcae571a442a50c90409396788dd0b28c24.tar.gz
micropython-c7aa9fcae571a442a50c90409396788dd0b28c24.zip
Merge branch 'master' of github.com:micropython/micropython
Diffstat (limited to 'py/objlist.c')
-rw-r--r--py/objlist.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/py/objlist.c b/py/objlist.c
index 1e8930eeea..fb68e2c566 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -23,6 +23,9 @@ static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
static mp_obj_list_t *list_new(uint n);
static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
+// TODO: Move to mpconfig.h
+#define LIST_MIN_ALLOC 4
+
/******************************************************************************/
/* list */
@@ -189,6 +192,7 @@ mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_list_t *self = self_in;
if (self->len >= self->alloc) {
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
+ assert(self->items);
self->alloc *= 2;
}
self->items[self->len++] = arg;
@@ -223,7 +227,7 @@ static mp_obj_t list_pop(uint n_args, const mp_obj_t *args) {
mp_obj_t ret = self->items[index];
self->len -= 1;
memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
- if (self->alloc > 2 * self->len) {
+ 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;
}
@@ -275,8 +279,8 @@ static mp_obj_t list_clear(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
mp_obj_list_t *self = self_in;
self->len = 0;
- self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
- self->alloc = 4;
+ self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
+ self->alloc = LIST_MIN_ALLOC;
return mp_const_none;
}
@@ -412,7 +416,7 @@ const mp_obj_type_t list_type = {
static mp_obj_list_t *list_new(uint n) {
mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
o->base.type = &list_type;
- o->alloc = n < 4 ? 4 : n;
+ o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
o->len = n;
o->items = m_new(mp_obj_t, o->alloc);
return o;