summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/bufhelper.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-01-21 22:48:37 +0000
committerDamien George <damien.p.george@gmail.com>2015-01-21 23:18:02 +0000
commit05005f679e00241e15a87751d89327f2c4630cb6 (patch)
tree50319ab1dee5af8016e95e3c80a3b28346cdca21 /stmhal/bufhelper.c
parent0b9ee86133a2a0524691c6cdac209dbfcb3bf116 (diff)
downloadmicropython-05005f679e00241e15a87751d89327f2c4630cb6.tar.gz
micropython-05005f679e00241e15a87751d89327f2c4630cb6.zip
py: Remove mp_obj_str_builder and use vstr instead.
With this patch str/bytes construction is streamlined. Always use a vstr to build a str/bytes object. If the size is known beforehand then use vstr_init_len to allocate only required memory. Otherwise use vstr_init and the vstr will grow as needed. Then use mp_obj_new_str_from_vstr to create a str/bytes object using the vstr memory. Saves code ROM: 68 bytes on stmhal, 108 bytes on bare-arm, and 336 bytes on unix x64.
Diffstat (limited to 'stmhal/bufhelper.c')
-rw-r--r--stmhal/bufhelper.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/stmhal/bufhelper.c b/stmhal/bufhelper.c
index 57aef5a91e..ca76e9496d 100644
--- a/stmhal/bufhelper.c
+++ b/stmhal/bufhelper.c
@@ -38,15 +38,17 @@ void pyb_buf_get_for_send(mp_obj_t o, mp_buffer_info_t *bufinfo, byte *tmp_data)
}
}
-mp_obj_t pyb_buf_get_for_recv(mp_obj_t o, mp_buffer_info_t *bufinfo) {
+mp_obj_t pyb_buf_get_for_recv(mp_obj_t o, vstr_t *vstr) {
if (MP_OBJ_IS_INT(o)) {
// allocate a new bytearray of given length
- bufinfo->len = mp_obj_get_int(o);
- bufinfo->typecode = 'B';
- return mp_obj_str_builder_start(&mp_type_bytes, bufinfo->len, (byte**)&bufinfo->buf);
+ vstr_init_len(vstr, mp_obj_get_int(o));
+ return MP_OBJ_NULL;
} else {
// get the existing buffer
- mp_get_buffer_raise(o, bufinfo, MP_BUFFER_WRITE);
- return MP_OBJ_NULL;
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(o, &bufinfo, MP_BUFFER_WRITE);
+ vstr->buf = bufinfo.buf;
+ vstr->len = bufinfo.len;
+ return o;
}
}