When is CAMLparamX required?

I am writing an interface to the C library using external declarations in OCaml. I used ctypes for testing, but that concerned 100% of the overhead for quick calls (measured using the core_bench micro test).

The functions are as follows:

/* external _create_var : float -> int -> int -> int -> _npnum = "ocaml_tnp_number_create_var" ;; */
value ocaml_tnp_number_create_var(value v, value nr, value p, value o) {
  //CAMLparam4(v, nr, p, o);
  const int params = Int_val(p);
  const int order = Int_val(o);
  const int number = Int_val(nr);
  const double value = Double_val(v);
  return CTYPES_FROM_PTR(tnp_number_create_variable(value, number, params, order));
}
/* external _delete : _npnum -> unit = "ocaml_tnp_number_delete" ;; */
value ocaml_tnp_number_delete(value num) {
  //CAMLparam1(num);
  struct tnp_number* n = CTYPES_TO_PTR(num);
  tnp_number_delete(n);
  return Val_unit;
}

I borrowed CTYPES_ * macros, so I basically move pointers around as Int64 values.

#define CTYPES_FROM_PTR(P) caml_copy_int64((intptr_t)P)
#define CTYPES_TO_PTR(I64) ((void *)Int64_val(I64))
#define CTYPES_PTR_PLUS(I64, I) caml_copy_int64(Int64_val(I64) + I)

AFAIK, these values ​​are presented in the form of boxes that are marked as "custom", which the GC should not be ignored.

Do macros need to be uncommented CAMLparamXto notify the GC of my use or is it legal to omit them?

+4
source share
1 answer

byterun/memory.h CAMLparamN value.

+3

Source: https://habr.com/ru/post/1528065/


All Articles