Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Data_custom_val()
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: IKEDA Katsumi <ikeda@m...>
Subject: [Caml-list] Data_custom_val()
Hi,

I use OCaml 3.06 on Linux.

I want to call C malloc() function from OCaml.
I use alloc_custom() function in C code.

Now, I confuse with Data_custom_val() and Field().

In OCaml source code, I find the following definition.

[form ./byterun/mlvalues.h]
|  #define Data_custom_val(v) ((void *) &Field((v), 1))

How to use Data_custom_val as lvalue ?

My codes are :

---------------- main.ml ----------------
(* main.ml *)
let count = ref 0;;

let main () =
  print_string "main start\n"; flush stdout;
  for i = 1 to 100 do
    for j = 1 to 10 do
      let r = Foo.record() in
(*      print_string "count = "; print_int !count; print_newline(); flush stdout;
      incr count;*)
      Foo.print(r);
    done;
    ignore(Sys.command "ps auxw | grep '[m]ain'")
  done
;;

main()
--------------------------------

---------------- foo.mli ----------------
(* foo.mli *)
type record
val record: unit -> record
val print: record -> unit
--------------------------------

---------------- foo.ml ----------------
(* foo.ml *)

type record

external record: unit -> record = "ocaml_foo_record"
external print: record -> unit = "ocaml_foo_print"
--------------------------------

---------------- stubs.c ----------------
#include <stdio.h>
#include <stdlib.h>

#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/callback.h>
#include <caml/fail.h>
#include <caml/custom.h>


static void custom_finalize(value rec)
{
  CAMLparam1(rec);
  fprintf(stderr, "custom_finalize() is called.\n");
#if 0
  free((void *)Field(rec,1));
#else
  free(Data_custom_val(rec));
#endif
  CAMLreturn0;
}

struct custom_operations ops = {
  "ops",
  custom_finalize,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default,
};

CAMLprim value ocaml_foo_record(value unit)
{
  CAMLparam1(unit);
  value block;
  block = alloc_custom(&ops, sizeof(char *), sizeof(char *), 100);
#if 0
  Field(block, 1) = (value)malloc(1024*1024);
#else
 *((char *)Data_custom_val(block)) = *((char *)malloc(1024*1024));
#endif
  CAMLreturn(block);
}

void ocaml_foo_print(value rec)
{
  CAMLparam1(rec);
#if 0
  fprintf(stderr, "rec = %p\n", Field(rec, 1));
#else
  fprintf(stderr, "rec = %p\n", Data_custom_val(rec));
#endif
  CAMLreturn0;
}
--------------------------------

---------------- Makefile ----------------
all: main

main: stubs.o foo.cmo main.ml
	ocamlc -custom -o main stubs.o foo.cmo main.ml 

foo.cmo: foo.cmi foo.ml
	ocamlc -c foo.ml

foo.cmi: foo.mli
	ocamlc -c foo.mli

stubs.o: stubs.c
	ocamlc -c stubs.c

clean:
	rm -f main *.cmi *.cmo *.o *~ core
--------------------------------

Running the above program, Segmentation fault (core dumped)
is occurred in custom_finalize().

Please teach me how to use Data_custom_val().

Thanks.

-- 
IKEDA Katsumi  <ikeda@msi.co.jp>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners