Skip to content

Commit 5da0d29

Browse files
committed
py/vstr: Remove vstr.had_error flag and inline basic vstr functions.
The vstr.had_error flag was a relic from the very early days which assumed that the malloc functions (eg m_new, m_renew) returned NULL if they failed to allocate. But that's no longer the case: these functions will raise an exception if they fail. Since it was impossible for had_error to be set, this patch introduces no change in behaviour. An alternative option would be to change the malloc calls to the _maybe variants, which return NULL instead of raising, but then a lot of code will need to explicitly check if the vstr had an error and raise if it did. The code-size savings for this patch are, in bytes: bare-arm:188, minimal:456, unix(NDEBUG,x86-64):368, stmhal:228, esp8266:360.
1 parent adaf0d8 commit 5da0d29

3 files changed

Lines changed: 8 additions & 58 deletions

File tree

py/lexer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,8 @@ mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_
723723
vstr_init(&lex->vstr, 32);
724724

725725
// check for memory allocation error
726-
if (lex->indent_level == NULL || vstr_had_error(&lex->vstr)) {
726+
// note: vstr_init above may fail on malloc, but so may mp_lexer_next_token_into below
727+
if (lex->indent_level == NULL) {
727728
mp_lexer_free(lex);
728729
return NULL;
729730
}

py/misc.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ typedef struct _vstr_t {
139139
size_t alloc;
140140
size_t len;
141141
char *buf;
142-
bool had_error : 1;
143142
bool fixed_buf : 1;
144143
} vstr_t;
145144

@@ -155,10 +154,9 @@ void vstr_clear(vstr_t *vstr);
155154
vstr_t *vstr_new(void);
156155
vstr_t *vstr_new_size(size_t alloc);
157156
void vstr_free(vstr_t *vstr);
158-
void vstr_reset(vstr_t *vstr);
159-
bool vstr_had_error(vstr_t *vstr);
160-
char *vstr_str(vstr_t *vstr);
161-
size_t vstr_len(vstr_t *vstr);
157+
static inline void vstr_reset(vstr_t *vstr) { vstr->len = 0; }
158+
static inline char *vstr_str(vstr_t *vstr) { return vstr->buf; }
159+
static inline size_t vstr_len(vstr_t *vstr) { return vstr->len; }
162160
void vstr_hint_size(vstr_t *vstr, size_t size);
163161
char *vstr_extend(vstr_t *vstr, size_t size);
164162
char *vstr_add_len(vstr_t *vstr, size_t len);

py/vstr.c

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ void vstr_init(vstr_t *vstr, size_t alloc) {
4444
vstr->alloc = alloc;
4545
vstr->len = 0;
4646
vstr->buf = m_new(char, vstr->alloc);
47-
if (vstr->buf == NULL) {
48-
vstr->had_error = true;
49-
return;
50-
}
51-
vstr->had_error = false;
5247
vstr->fixed_buf = false;
5348
}
5449

@@ -63,7 +58,6 @@ void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
6358
vstr->alloc = alloc;
6459
vstr->len = 0;
6560
vstr->buf = buf;
66-
vstr->had_error = false;
6761
vstr->fixed_buf = true;
6862
}
6963

@@ -107,39 +101,12 @@ void vstr_free(vstr_t *vstr) {
107101
}
108102
}
109103

110-
void vstr_reset(vstr_t *vstr) {
111-
vstr->len = 0;
112-
vstr->had_error = false;
113-
}
114-
115-
bool vstr_had_error(vstr_t *vstr) {
116-
return vstr->had_error;
117-
}
118-
119-
char *vstr_str(vstr_t *vstr) {
120-
if (vstr->had_error) {
121-
return NULL;
122-
}
123-
return vstr->buf;
124-
}
125-
126-
size_t vstr_len(vstr_t *vstr) {
127-
if (vstr->had_error) {
128-
return 0;
129-
}
130-
return vstr->len;
131-
}
132-
133104
// Extend vstr strictly by requested size, return pointer to newly added chunk.
134105
char *vstr_extend(vstr_t *vstr, size_t size) {
135106
if (vstr->fixed_buf) {
136107
return NULL;
137108
}
138109
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
139-
if (new_buf == NULL) {
140-
vstr->had_error = true;
141-
return NULL;
142-
}
143110
char *p = new_buf + vstr->alloc;
144111
vstr->alloc += size;
145112
vstr->buf = new_buf;
@@ -153,25 +120,18 @@ STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
153120
}
154121
size_t new_alloc = ROUND_ALLOC((vstr->len + size) + 16);
155122
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
156-
if (new_buf == NULL) {
157-
vstr->had_error = true;
158-
return false;
159-
}
160123
vstr->alloc = new_alloc;
161124
vstr->buf = new_buf;
162125
}
163126
return true;
164127
}
165128

166129
void vstr_hint_size(vstr_t *vstr, size_t size) {
167-
// it's not an error if we fail to allocate for the size hint
168-
bool er = vstr->had_error;
169130
vstr_ensure_extra(vstr, size);
170-
vstr->had_error = er;
171131
}
172132

173133
char *vstr_add_len(vstr_t *vstr, size_t len) {
174-
if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
134+
if (!vstr_ensure_extra(vstr, len)) {
175135
return NULL;
176136
}
177137
char *buf = vstr->buf + vstr->len;
@@ -181,9 +141,6 @@ char *vstr_add_len(vstr_t *vstr, size_t len) {
181141

182142
// Doesn't increase len, just makes sure there is a null byte at the end
183143
char *vstr_null_terminated_str(vstr_t *vstr) {
184-
if (vstr->had_error) {
185-
return NULL;
186-
}
187144
// If there's no more room, add single byte
188145
if (vstr->alloc == vstr->len) {
189146
if (vstr_extend(vstr, 1) == NULL) {
@@ -248,7 +205,7 @@ void vstr_add_str(vstr_t *vstr, const char *str) {
248205
}
249206

250207
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
251-
if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
208+
if (!vstr_ensure_extra(vstr, len)) {
252209
// if buf is fixed, we got here because there isn't enough room left
253210
// so just try to copy as much as we can, with room for a possible null byte
254211
if (vstr->fixed_buf && vstr->len < vstr->alloc) {
@@ -263,9 +220,6 @@ void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
263220
}
264221

265222
STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
266-
if (vstr->had_error) {
267-
return NULL;
268-
}
269223
size_t l = vstr->len;
270224
if (byte_pos > l) {
271225
byte_pos = l;
@@ -303,9 +257,6 @@ void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
303257
}
304258

305259
void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
306-
if (vstr->had_error) {
307-
return;
308-
}
309260
if (len > vstr->len) {
310261
vstr->len = 0;
311262
} else {
@@ -314,7 +265,7 @@ void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
314265
}
315266

316267
void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
317-
if (vstr->had_error || byte_pos >= vstr->len) {
268+
if (byte_pos >= vstr->len) {
318269
return;
319270
} else if (byte_pos + bytes_to_cut >= vstr->len) {
320271
vstr->len = byte_pos;

0 commit comments

Comments
 (0)