* [Caml-list] Data_custom_val()
@ 2002-10-03 4:59 IKEDA Katsumi
2002-10-03 9:56 ` Nicolas Cannasse
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: IKEDA Katsumi @ 2002-10-03 4:59 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Data_custom_val()
2002-10-03 4:59 [Caml-list] Data_custom_val() IKEDA Katsumi
@ 2002-10-03 9:56 ` Nicolas Cannasse
2002-10-03 10:02 ` Nicolas Cannasse
[not found] ` <15771.65438.985931.420887@karryall.dnsalias.org>
2 siblings, 0 replies; 4+ messages in thread
From: Nicolas Cannasse @ 2002-10-03 9:56 UTC (permalink / raw)
To: caml-list, IKEDA Katsumi
>
> How to use Data_custom_val as lvalue ?
>
Try using the following macro :
#define LData_custom_val(v) (*(value**)Data_custom_val(v))
Nicolas Cannasse
-------------------
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Data_custom_val()
2002-10-03 4:59 [Caml-list] Data_custom_val() IKEDA Katsumi
2002-10-03 9:56 ` Nicolas Cannasse
@ 2002-10-03 10:02 ` Nicolas Cannasse
[not found] ` <15771.65438.985931.420887@karryall.dnsalias.org>
2 siblings, 0 replies; 4+ messages in thread
From: Nicolas Cannasse @ 2002-10-03 10:02 UTC (permalink / raw)
To: caml-list, IKEDA Katsumi
> 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));
Sorry I replied a little too fast.
Looks like this last line is using one more indirection.
*((char **)Data_custom_val(block)) = (char *)malloc(1024*1024);
Looks better.
Nicolas Cannasse
-------------------
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
^ permalink raw reply [flat|nested] 4+ messages in thread
[parent not found: <15771.65438.985931.420887@karryall.dnsalias.org>]
end of thread, other threads:[~2002-10-04 1:39 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-03 4:59 [Caml-list] Data_custom_val() IKEDA Katsumi
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox