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.
This commit is contained in:
Damien George
2015-01-21 19:14:25 +00:00
parent 2e526ff1a1
commit 0b9ee86133
8 changed files with 82 additions and 109 deletions

View File

@@ -136,26 +136,6 @@ char *vstr_extend(vstr_t *vstr, size_t size) {
return p;
}
// Shrink vstr to be given size
bool vstr_set_size(vstr_t *vstr, size_t size) {
if (vstr->fixed_buf) {
return false;
}
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, size);
if (new_buf == NULL) {
vstr->had_error = true;
return false;
}
vstr->buf = new_buf;
vstr->alloc = vstr->len = size;
return true;
}
// Shrink vstr allocation to its actual length
bool vstr_shrink(vstr_t *vstr) {
return vstr_set_size(vstr, vstr->len);
}
STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
if (vstr->len + size + 1 > vstr->alloc) {
if (vstr->fixed_buf) {