libufdt: avoid to strdup() if possible
Memory allocation is slow in some platform. Uses stack to avoid
malloc/free if possible.
Also removed dto_strdup(). Doesn't need to implement it anymore.
Bug: 32969430
Test: ./tests/run_tests.sh
Change-Id: I68eae8fcf05e722089879ba3e95c55b136bb31ed
diff --git a/ufdt_overlay.c b/ufdt_overlay.c
index 1c6b15e..7ffb8c1 100644
--- a/ufdt_overlay.c
+++ b/ufdt_overlay.c
@@ -131,12 +131,18 @@
char *path, *prop_ptr, *offset_ptr, *end_ptr;
int prop_offset, prop_len;
const char *prop_data;
+ char path_buf[1024];
+ char *path_mem = NULL;
- /*
- * TODO(akaineko): Keep track of substring lengths so we don't have to
- * dto_malloc a copy and split it up.
- */
- path = dto_strdup(fixup);
+ size_t fixup_len = strlen(fixup) + 1;
+ if (fixup_len > sizeof(path_buf)) {
+ path_mem = dto_malloc(fixup_len);
+ path = path_mem;
+ } else {
+ path = path_buf;
+ }
+ dto_memcpy(path, fixup, fixup_len);
+
prop_ptr = dto_strchr(path, ':');
if (prop_ptr == NULL) {
dto_error("Missing property part in '%s'\n", path);
@@ -182,11 +188,11 @@
goto fail;
}
- dto_free(path);
+ if (path_mem) dto_free(path_mem);
return (char *)prop_data + prop_offset;
fail:
- dto_free(path);
+ if (path_mem) dto_free(path_mem);
return NULL;
}