py/vstr: Combine vstr_new_size with vstr_new since they are rarely used.

Now there is just one function to allocate a new vstr, namely vstr_new
(in addition to vstr_init etc).  The caller of this function should know
what initial size to allocate for the buffer, or at least have some policy
or config option, instead of leaving it to a default (as it was before).
This commit is contained in:
Damien George 2016-10-14 16:46:34 +11:00
parent ed878275b0
commit 824f5c5a32
6 changed files with 6 additions and 19 deletions

View File

@ -148,7 +148,7 @@ STATIC int pyexec_raw_repl_process_char(int c);
STATIC int pyexec_friendly_repl_process_char(int c);
void pyexec_event_repl_init(void) {
MP_STATE_VM(repl_line) = vstr_new_size(32);
MP_STATE_VM(repl_line) = vstr_new(32);
repl.cont_line = false;
readline_init(MP_STATE_VM(repl_line), ">>> ");
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {

View File

@ -151,8 +151,7 @@ void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
struct _mp_print_t;
void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print);
void vstr_clear(vstr_t *vstr);
vstr_t *vstr_new(void);
vstr_t *vstr_new_size(size_t alloc);
vstr_t *vstr_new(size_t alloc);
void vstr_free(vstr_t *vstr);
static inline void vstr_reset(vstr_t *vstr) { vstr->len = 0; }
static inline char *vstr_str(vstr_t *vstr) { return vstr->buf; }

View File

@ -150,7 +150,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio
STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
o->base.type = type;
o->vstr = vstr_new();
o->vstr = vstr_new(16);
o->pos = 0;
return o;
}

View File

@ -74,20 +74,8 @@ void vstr_clear(vstr_t *vstr) {
vstr->buf = NULL;
}
vstr_t *vstr_new(void) {
vstr_t *vstr_new(size_t alloc) {
vstr_t *vstr = m_new_obj(vstr_t);
if (vstr == NULL) {
return NULL;
}
vstr_init(vstr, 16);
return vstr;
}
vstr_t *vstr_new_size(size_t alloc) {
vstr_t *vstr = m_new_obj(vstr_t);
if (vstr == NULL) {
return NULL;
}
vstr_init(vstr, alloc);
return vstr;
}

View File

@ -317,7 +317,7 @@ soft_reset:
pyexec_frozen_module("main.py");
#else
{
vstr_t *vstr = vstr_new();
vstr_t *vstr = vstr_new(16);
vstr_add_str(vstr, "/");
if (pyb_config_main == MP_OBJ_NULL) {
vstr_add_str(vstr, "main.py");

View File

@ -34,7 +34,7 @@ STATIC mp_obj_t extra_coverage(void) {
// vstr
{
mp_printf(&mp_plat_print, "# vstr\n");
vstr_t *vstr = vstr_new_size(16);
vstr_t *vstr = vstr_new(16);
vstr_hint_size(vstr, 32);
vstr_add_str(vstr, "ts");
vstr_ins_byte(vstr, 1, 'e');