diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-21 22:48:37 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-21 23:18:02 +0000 |
commit | 05005f679e00241e15a87751d89327f2c4630cb6 (patch) | |
tree | 50319ab1dee5af8016e95e3c80a3b28346cdca21 /stmhal/moduos.c | |
parent | 0b9ee86133a2a0524691c6cdac209dbfcb3bf116 (diff) |
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/moduos.c')
-rw-r--r-- | stmhal/moduos.c | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/stmhal/moduos.c b/stmhal/moduos.c index 8706f4981..9f8de8046 100644 --- a/stmhal/moduos.c +++ b/stmhal/moduos.c @@ -322,12 +322,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync); /// random number generator. STATIC mp_obj_t os_urandom(mp_obj_t num) { mp_int_t n = mp_obj_get_int(num); - byte *data; - mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, n, &data); + vstr_t vstr; + vstr_init_len(&vstr, n); for (int i = 0; i < n; i++) { - data[i] = rng_get(); + vstr.buf[i] = rng_get(); } - return mp_obj_str_builder_end(o); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); #endif |