[llvm] 2c82ea1 - [OCaml] Code simplification using string allocation functions
Josh Berdine via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 5 02:59:02 PDT 2021
Author: Josh Berdine
Date: 2021-04-05T10:55:07+01:00
New Revision: 2c82ea1849dc77eedeedb59a73c870717229ed37
URL: https://github.com/llvm/llvm-project/commit/2c82ea1849dc77eedeedb59a73c870717229ed37
DIFF: https://github.com/llvm/llvm-project/commit/2c82ea1849dc77eedeedb59a73c870717229ed37.diff
LOG: [OCaml] Code simplification using string allocation functions
Using the `cstr_to_string` function that allocates and initializes an
OCaml `string` value enables simplifications in several cases. This
change also has the effect of avoiding calling `memcpy` on NULL
pointers even if only 0 bytes are to be copied.
Differential Revision: https://reviews.llvm.org/D99474
Added:
Modified:
llvm/bindings/ocaml/llvm/llvm_ocaml.c
Removed:
################################################################################
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index b6d1435674019..e6b4b4d730921 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -279,18 +279,14 @@ CAMLprim value llvm_is_string_attr(LLVMAttributeRef A) {
CAMLprim value llvm_get_string_attr_kind(LLVMAttributeRef A) {
unsigned Length;
const char *String = LLVMGetStringAttributeKind(A, &Length);
- value Result = caml_alloc_string(Length);
- memcpy((char *)String_val(Result), String, Length);
- return Result;
+ return cstr_to_string (String, Length);
}
/* llattribute -> string */
CAMLprim value llvm_get_string_attr_value(LLVMAttributeRef A) {
unsigned Length;
const char *String = LLVMGetStringAttributeValue(A, &Length);
- value Result = caml_alloc_string(Length);
- memcpy((char *)String_val(Result), String, Length);
- return Result;
+ return cstr_to_string (String, Length);
}
/*===-- Modules -----------------------------------------------------------===*/
@@ -2512,11 +2508,9 @@ CAMLprim LLVMMemoryBufferRef llvm_memorybuffer_of_string(value Name, value Strin
/* llmemorybuffer -> string */
CAMLprim value llvm_memorybuffer_as_string(LLVMMemoryBufferRef MemBuf) {
- value String = caml_alloc_string(LLVMGetBufferSize(MemBuf));
- memcpy((char *)String_val(String), LLVMGetBufferStart(MemBuf),
- LLVMGetBufferSize(MemBuf));
-
- return String;
+ size_t BufferSize = LLVMGetBufferSize(MemBuf);
+ const char *BufferStart = LLVMGetBufferStart(MemBuf);
+ return cstr_to_string(BufferStart, BufferSize);
}
/* llmemorybuffer -> unit */
More information about the llvm-commits
mailing list