diff options
author | Luc Van Oostenryck <lucvoo@kernel.org> | 2024-01-20 01:24:12 +0100 |
---|---|---|
committer | Luc Van Oostenryck <lucvoo@kernel.org> | 2024-01-20 01:25:33 +0100 |
commit | d5a73d316e38f533d3ec61879149a1c33fac51a3 (patch) | |
tree | e76da38b72e37757fd9a12753f1fcb2e122305a3 | |
parent | d7edb6c7b7c62db1a328fd88f7888324293b005c (diff) | |
download | sparse-dev-d5a73d316e38f533d3ec61879149a1c33fac51a3.tar.gz |
llvm: do not duplicate strings and use their length in struct string
In 2 places, we duplicate the storage for a string (with strdup) and we
also calculate its length via strlen(). Both operation are unneeded
as the length is already calculated in the struct string and the pointer
to the string data can be safely reused since Sparse will not modify or free it.
Signed-off-by: Luc Van Oostenryck <lucvoo@kernel.org>
-rw-r--r-- | sparse-llvm.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/sparse-llvm.c b/sparse-llvm.c index 9ceb19a9..fc0399b4 100644 --- a/sparse-llvm.c +++ b/sparse-llvm.c @@ -294,13 +294,14 @@ static LLVMValueRef get_sym_value(LLVMModuleRef module, struct symbol *sym) switch (expr->type) { case EXPR_STRING: { const char *s = expr->string->data; + size_t len = expr->string->length; LLVMValueRef indices[] = { LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt64Type(), 0, 0) }; LLVMValueRef data; - data = LLVMAddGlobal(module, LLVMArrayType(LLVMInt8Type(), strlen(s) + 1), ".str"); + data = LLVMAddGlobal(module, LLVMArrayType(LLVMInt8Type(), len), ".str"); LLVMSetLinkage(data, LLVMPrivateLinkage); LLVMSetGlobalConstant(data, 1); - LLVMSetInitializer(data, LLVMConstString(strdup(s), strlen(s) + 1, true)); + LLVMSetInitializer(data, LLVMConstString(s, len, true)); result = LLVMConstGEP(data, indices, ARRAY_SIZE(indices)); return result; @@ -1212,8 +1213,9 @@ static LLVMValueRef output_data(LLVMModuleRef module, struct symbol *sym) } case EXPR_STRING: { const char *s = initializer->string->data; + size_t len = initializer->string->length; - initial_value = LLVMConstString(strdup(s), strlen(s) + 1, true); + initial_value = LLVMConstString(s, len, true); break; } default: |