[RFC PATCH 26/28] cli: lil: Allocate len even when str is NULL in alloc_value_len

Sean Anderson
2021-07-01T06:21:12Z

This allows us to reserve some space ahead of time, avoiding another
alloc/copy/free.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

common/cli_lil.c | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/common/cli_lil.c b/common/cli_lil.c
index 153c34791b..42659920b5 100644
--- a/common/cli_lil.c
+++ b/common/cli_lil.c
@@ -459,21 +459,19 @@ static struct lil_value *alloc_value_len(const char *str, size_t len)
if (!val)
return NULL;
value_to_symbol(val)->type = LIL_SYMBOL_VALUE;
+ ensure_capacity(val, len + 1);
+ if (!val->d) {
+ release_to_pool(val);
+ return NULL;
+ }

if (str) {
val->l = len;
- ensure_capacity(val, len + 1);
- if (!val->d) {
- release_to_pool(val);
- return NULL;
- }
memcpy(val->d, str, len);
val->d[len] = 0;
} else {
val->l = 0;
- ensure_capacity(val, 1);
- if (val->d)
- val->d[0] = '\0';
+ val->d[0] = '\0';
}
return val;
}
--
2.32.0