caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: IKEDA Katsumi <ikeda@msi.co.jp>
To: caml-list@inria.fr
Subject: [Caml-list] Data_custom_val()
Date: Thu, 03 Oct 2002 13:59:22 +0900	[thread overview]
Message-ID: <20021003135922G.ikeda@msi.co.jp> (raw)

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


             reply	other threads:[~2002-10-03  4:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-03  4:59 IKEDA Katsumi [this message]
2002-10-03  9:56 ` Nicolas Cannasse
2002-10-03 10:02 ` Nicolas Cannasse
     [not found] ` <15771.65438.985931.420887@karryall.dnsalias.org>
2002-10-04  1:39   ` IKEDA Katsumi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20021003135922G.ikeda@msi.co.jp \
    --to=ikeda@msi.co.jp \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).