aboutsummaryrefslogtreecommitdiff
path: root/py/vstr.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-09-19 11:17:02 +1000
committerDamien George <damien.p.george@gmail.com>2016-09-19 12:28:55 +1000
commit5da0d29d3cefa6a3cac52e0db96e9ede820d6a51 (patch)
treecf39b02347b40016088c907cd2e1b3aaada8b653 /py/vstr.c
parentadaf0d865cd6c81fb352751566460506392ed55f (diff)
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.
Diffstat (limited to 'py/vstr.c')
-rw-r--r--py/vstr.c55
1 files changed, 3 insertions, 52 deletions
diff --git a/py/vstr.c b/py/vstr.c
index cf10f8471..5096475f1 100644
--- a/py/vstr.c
+++ b/py/vstr.c
@@ -44,11 +44,6 @@ void vstr_init(vstr_t *vstr, size_t alloc) {
vstr->alloc = alloc;
vstr->len = 0;
vstr->buf = m_new(char, vstr->alloc);
- if (vstr->buf == NULL) {
- vstr->had_error = true;
- return;
- }
- vstr->had_error = false;
vstr->fixed_buf = false;
}
@@ -63,7 +58,6 @@ void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
vstr->alloc = alloc;
vstr->len = 0;
vstr->buf = buf;
- vstr->had_error = false;
vstr->fixed_buf = true;
}
@@ -107,39 +101,12 @@ void vstr_free(vstr_t *vstr) {
}
}
-void vstr_reset(vstr_t *vstr) {
- vstr->len = 0;
- vstr->had_error = false;
-}
-
-bool vstr_had_error(vstr_t *vstr) {
- return vstr->had_error;
-}
-
-char *vstr_str(vstr_t *vstr) {
- if (vstr->had_error) {
- return NULL;
- }
- return vstr->buf;
-}
-
-size_t vstr_len(vstr_t *vstr) {
- if (vstr->had_error) {
- return 0;
- }
- return vstr->len;
-}
-
// Extend vstr strictly by requested size, return pointer to newly added chunk.
char *vstr_extend(vstr_t *vstr, size_t size) {
if (vstr->fixed_buf) {
return NULL;
}
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
- if (new_buf == NULL) {
- vstr->had_error = true;
- return NULL;
- }
char *p = new_buf + vstr->alloc;
vstr->alloc += size;
vstr->buf = new_buf;
@@ -153,10 +120,6 @@ STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
}
size_t new_alloc = ROUND_ALLOC((vstr->len + size) + 16);
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
- if (new_buf == NULL) {
- vstr->had_error = true;
- return false;
- }
vstr->alloc = new_alloc;
vstr->buf = new_buf;
}
@@ -164,14 +127,11 @@ STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
}
void vstr_hint_size(vstr_t *vstr, size_t size) {
- // it's not an error if we fail to allocate for the size hint
- bool er = vstr->had_error;
vstr_ensure_extra(vstr, size);
- vstr->had_error = er;
}
char *vstr_add_len(vstr_t *vstr, size_t len) {
- if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
+ if (!vstr_ensure_extra(vstr, len)) {
return NULL;
}
char *buf = vstr->buf + vstr->len;
@@ -181,9 +141,6 @@ char *vstr_add_len(vstr_t *vstr, size_t len) {
// Doesn't increase len, just makes sure there is a null byte at the end
char *vstr_null_terminated_str(vstr_t *vstr) {
- if (vstr->had_error) {
- return NULL;
- }
// If there's no more room, add single byte
if (vstr->alloc == vstr->len) {
if (vstr_extend(vstr, 1) == NULL) {
@@ -248,7 +205,7 @@ void vstr_add_str(vstr_t *vstr, const char *str) {
}
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
- if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
+ if (!vstr_ensure_extra(vstr, len)) {
// if buf is fixed, we got here because there isn't enough room left
// so just try to copy as much as we can, with room for a possible null byte
if (vstr->fixed_buf && vstr->len < vstr->alloc) {
@@ -263,9 +220,6 @@ copy:
}
STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
- if (vstr->had_error) {
- return NULL;
- }
size_t l = vstr->len;
if (byte_pos > l) {
byte_pos = l;
@@ -303,9 +257,6 @@ void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
}
void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
- if (vstr->had_error) {
- return;
- }
if (len > vstr->len) {
vstr->len = 0;
} else {
@@ -314,7 +265,7 @@ void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
}
void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
- if (vstr->had_error || byte_pos >= vstr->len) {
+ if (byte_pos >= vstr->len) {
return;
} else if (byte_pos + bytes_to_cut >= vstr->len) {
vstr->len = byte_pos;