summaryrefslogtreecommitdiff
path: root/py/stream.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-01-21 19:14:25 +0000
committerDamien George <damien.p.george@gmail.com>2015-01-21 23:17:27 +0000
commit0b9ee86133a2a0524691c6cdac209dbfcb3bf116 (patch)
treee17f1f8c26b2d92991a8481ba5d2fc0649a8c6ce /py/stream.c
parent2e526ff1a15cfe50aa46fe5611d3160b2a854f49 (diff)
py: Add mp_obj_new_str_from_vstr, and use it where relevant.
This patch allows to reuse vstr memory when creating str/bytes object. This improves memory usage. Also saves code ROM: 128 bytes on stmhal, 92 bytes on bare-arm, and 88 bytes on unix x64.
Diffstat (limited to 'py/stream.c')
-rw-r--r--py/stream.c20
1 files changed, 9 insertions, 11 deletions
diff --git a/py/stream.c b/py/stream.c
index c57b7981a..b7d4a9000 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -156,9 +156,7 @@ STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) {
}
}
- mp_obj_t ret = mp_obj_new_str_of_type(&mp_type_str, (byte*)vstr.buf, vstr.len);
- vstr_clear(&vstr);
- return ret;
+ return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
}
#endif
@@ -251,8 +249,9 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
}
mp_uint_t total_size = 0;
- vstr_t *vstr = vstr_new_size(DEFAULT_BUFFER_SIZE);
- char *p = vstr_str(vstr);
+ vstr_t vstr;
+ vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
+ char *p = vstr.buf;
mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
while (true) {
int error;
@@ -278,7 +277,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
p += out_sz;
} else {
current_read = DEFAULT_BUFFER_SIZE;
- p = vstr_extend(vstr, current_read);
+ p = vstr_extend(&vstr, current_read);
if (p == NULL) {
// TODO
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
@@ -286,9 +285,9 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
}
}
- mp_obj_t s = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, total_size);
- vstr_free(vstr);
- return s;
+ vstr.len = total_size;
+ vstr.buf[vstr.len] = '\0'; // XXX is there enough space?
+ return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr);
}
// Unbuffered, inefficient implementation of readline() for raw I/O files.
@@ -348,8 +347,7 @@ done:
break;
}
}
- // TODO need a string creation API that doesn't copy the given data
- mp_obj_t ret = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, vstr->len);
+ mp_obj_t ret = mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), vstr);
vstr_free(vstr);
return ret;
}