From 439542f70c4546568dca3f2539d503aa7a6ec05b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 21 Jan 2014 00:19:19 +0200 Subject: [PATCH] sequence.c: Start to refactor sequence operations for reuse among types. --- py/obj.h | 3 +++ py/objlist.c | 9 ++------- py/py.mk | 1 + py/sequence.c | 25 +++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 py/sequence.c diff --git a/py/obj.h b/py/obj.h index d7154a743..c2ce32588 100644 --- a/py/obj.h +++ b/py/obj.h @@ -376,3 +376,6 @@ typedef struct _mp_obj_classmethod_t { mp_obj_base_t base; mp_obj_t fun; } mp_obj_classmethod_t; + +// sequence helpers +void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest); diff --git a/py/objlist.c b/py/objlist.c index 0ad7b6879..804384222 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -153,13 +153,8 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { return NULL; } int n = MP_OBJ_SMALL_INT_VALUE(rhs); - int len = o->len; - mp_obj_list_t *s = list_new(len * n); - mp_obj_t *dest = s->items; - for (int i = 0; i < n; i++) { - memcpy(dest, o->items, sizeof(mp_obj_t) * len); - dest += len; - } + mp_obj_list_t *s = list_new(o->len * n); + mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items); return s; } case RT_COMPARE_OP_EQUAL: diff --git a/py/py.mk b/py/py.mk index ce5169f77..6ab1ee305 100644 --- a/py/py.mk +++ b/py/py.mk @@ -97,6 +97,7 @@ PY_O_BASENAME = \ objstr.o \ objtuple.o \ objtype.o \ + sequence.o \ stream.o \ builtin.o \ builtinimport.o \ diff --git a/py/sequence.c b/py/sequence.c new file mode 100644 index 000000000..af3c61af6 --- /dev/null +++ b/py/sequence.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +#include "nlr.h" +#include "misc.h" +#include "mpconfig.h" +#include "mpqstr.h" +#include "obj.h" +#include "map.h" +#include "runtime0.h" +#include "runtime.h" + +// Helpers for sequence types + +// Implements backend of sequence * integer operation. Assumes elements are +// memory-adjacent in sequence. +void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest) { + for (int i = 0; i < times; i++) { + uint copy_sz = item_sz * len; + memcpy(dest, items, copy_sz); + dest = (char*)dest + copy_sz; + } +}