ocaml: Use caml_alloc_initialized_string instead of memcpy.

See this commit in libguestfs-common:
398dc56a6c
This commit is contained in:
Richard W.M. Jones
2020-02-06 10:17:35 +00:00
parent 378b49152d
commit 9f3148c791
5 changed files with 42 additions and 9 deletions

2
common

Submodule common updated: 08c4504d7a...ea10827b4c

View File

@@ -509,12 +509,11 @@ copy_table (char * const * argv)
| name, FString -> | name, FString ->
pr " v = caml_copy_string (%s->%s);\n" typ name pr " v = caml_copy_string (%s->%s);\n" typ name
| name, FBuffer -> | name, FBuffer ->
pr " v = caml_alloc_string (%s->%s_len);\n" typ name; pr " v = caml_alloc_initialized_string (%s->%s_len, %s->%s);\n"
pr " memcpy (String_val (v), %s->%s, %s->%s_len);\n"
typ name typ name typ name typ name
| name, FUUID -> | name, FUUID ->
pr " v = caml_alloc_string (32);\n"; pr " v = caml_alloc_initialized_string (32, %s->%s);\n"
pr " memcpy (String_val (v), %s->%s, 32);\n" typ name typ name
| name, (FBytes|FInt64|FUInt64) -> | name, (FBytes|FInt64|FUInt64) ->
pr " v = caml_copy_int64 (%s->%s);\n" typ name pr " v = caml_copy_int64 (%s->%s);\n" typ name
| name, (FInt32|FUInt32) -> | name, (FInt32|FUInt32) ->
@@ -762,8 +761,7 @@ copy_table (char * const * argv)
pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n"; pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
pr " free (r);\n"; pr " free (r);\n";
| RBufferOut _ -> | RBufferOut _ ->
pr " rv = caml_alloc_string (size);\n"; pr " rv = caml_alloc_initialized_string (size, r);\n";
pr " memcpy (String_val (rv), r, size);\n";
pr " free (r);\n" pr " free (r);\n"
); );

View File

@@ -213,6 +213,24 @@ EOF
AM_CONDITIONAL([HAVE_BYTES_COMPAT_ML], AM_CONDITIONAL([HAVE_BYTES_COMPAT_ML],
[test "x$have_Bytes_module" = "xno"]) [test "x$have_Bytes_module" = "xno"])
dnl Check if OCaml has caml_alloc_initialized_string (added 2017).
AS_IF([test "x$OCAMLC" != "xno" && test "x$OCAMLFIND" != "xno" && \
test "x$enable_ocaml" = "xyes"],[
AC_MSG_CHECKING([for caml_alloc_initialized_string])
cat >conftest.c <<'EOF'
#include <caml/alloc.h>
int main () { char *p = (void *) caml_alloc_initialized_string; return 0; }
EOF
AS_IF([$OCAMLC conftest.c >&AS_MESSAGE_LOG_FD 2>&1],[
AC_MSG_RESULT([yes])
AC_DEFINE([HAVE_CAML_ALLOC_INITIALIZED_STRING],[1],
[caml_alloc_initialized_string found at compile time.])
],[
AC_MSG_RESULT([no])
])
rm -f conftest.c conftest.o
])
dnl Flags we want to pass to every OCaml compiler call. dnl Flags we want to pass to every OCaml compiler call.
OCAML_WARN_ERROR="-warn-error CDEFLMPSUVYZX+52-3" OCAML_WARN_ERROR="-warn-error CDEFLMPSUVYZX+52-3"
AC_SUBST([OCAML_WARN_ERROR]) AC_SUBST([OCAML_WARN_ERROR])

View File

@@ -360,8 +360,7 @@ event_callback_wrapper_locked (guestfs_h *g,
ehv = Val_int (event_handle); ehv = Val_int (event_handle);
bufv = caml_alloc_string (buf_len); bufv = caml_alloc_initialized_string (buf_len, buf);
memcpy (String_val (bufv), buf, buf_len);
arrayv = caml_alloc (array_len, 0); arrayv = caml_alloc (array_len, 0);
for (i = 0; i < array_len; ++i) { for (i = 0; i < array_len; ++i) {

View File

@@ -19,6 +19,24 @@
#ifndef GUESTFS_OCAML_C_H #ifndef GUESTFS_OCAML_C_H
#define GUESTFS_OCAML_C_H #define GUESTFS_OCAML_C_H
#include "config.h"
#include <caml/alloc.h>
#include <caml/mlvalues.h>
/* Replacement if caml_alloc_initialized_string is missing, added
* to OCaml runtime in 2017.
*/
#ifndef HAVE_CAML_ALLOC_INITIALIZED_STRING
static inline value
caml_alloc_initialized_string (mlsize_t len, const char *p)
{
value sv = caml_alloc_string (len);
memcpy ((char *) String_val (sv), p, len);
return sv;
}
#endif
#define Guestfs_val(v) (*((guestfs_h **)Data_custom_val(v))) #define Guestfs_val(v) (*((guestfs_h **)Data_custom_val(v)))
extern void guestfs_int_ocaml_raise_error (guestfs_h *g, const char *func) extern void guestfs_int_ocaml_raise_error (guestfs_h *g, const char *func)
Noreturn; Noreturn;