Skip to content

Commit 824f5c5

Browse files
committed
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).
1 parent ed87827 commit 824f5c5

File tree

6 files changed

+6
-19
lines changed

6 files changed

+6
-19
lines changed

lib/utils/pyexec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ STATIC int pyexec_raw_repl_process_char(int c);
148148
STATIC int pyexec_friendly_repl_process_char(int c);
149149

150150
void pyexec_event_repl_init(void) {
151-
MP_STATE_VM(repl_line) = vstr_new_size(32);
151+
MP_STATE_VM(repl_line) = vstr_new(32);
152152
repl.cont_line = false;
153153
readline_init(MP_STATE_VM(repl_line), ">>> ");
154154
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {

py/misc.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
151151
struct _mp_print_t;
152152
void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print);
153153
void vstr_clear(vstr_t *vstr);
154-
vstr_t *vstr_new(void);
155-
vstr_t *vstr_new_size(size_t alloc);
154+
vstr_t *vstr_new(size_t alloc);
156155
void vstr_free(vstr_t *vstr);
157156
static inline void vstr_reset(vstr_t *vstr) { vstr->len = 0; }
158157
static inline char *vstr_str(vstr_t *vstr) { return vstr->buf; }

py/objstringio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio
150150
STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) {
151151
mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t);
152152
o->base.type = type;
153-
o->vstr = vstr_new();
153+
o->vstr = vstr_new(16);
154154
o->pos = 0;
155155
return o;
156156
}

py/vstr.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,8 @@ void vstr_clear(vstr_t *vstr) {
7474
vstr->buf = NULL;
7575
}
7676

77-
vstr_t *vstr_new(void) {
77+
vstr_t *vstr_new(size_t alloc) {
7878
vstr_t *vstr = m_new_obj(vstr_t);
79-
if (vstr == NULL) {
80-
return NULL;
81-
}
82-
vstr_init(vstr, 16);
83-
return vstr;
84-
}
85-
86-
vstr_t *vstr_new_size(size_t alloc) {
87-
vstr_t *vstr = m_new_obj(vstr_t);
88-
if (vstr == NULL) {
89-
return NULL;
90-
}
9179
vstr_init(vstr, alloc);
9280
return vstr;
9381
}

teensy/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ int main(void) {
317317
pyexec_frozen_module("main.py");
318318
#else
319319
{
320-
vstr_t *vstr = vstr_new();
320+
vstr_t *vstr = vstr_new(16);
321321
vstr_add_str(vstr, "/");
322322
if (pyb_config_main == MP_OBJ_NULL) {
323323
vstr_add_str(vstr, "main.py");

unix/coverage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ STATIC mp_obj_t extra_coverage(void) {
3434
// vstr
3535
{
3636
mp_printf(&mp_plat_print, "# vstr\n");
37-
vstr_t *vstr = vstr_new_size(16);
37+
vstr_t *vstr = vstr_new(16);
3838
vstr_hint_size(vstr, 32);
3939
vstr_add_str(vstr, "ts");
4040
vstr_ins_byte(vstr, 1, 'e');

0 commit comments

Comments
 (0)