caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] OCaml 3.08 and Bus Errors
@ 2004-08-02  7:43 David McClain
  2004-08-06 12:09 ` Damien Doligez
  0 siblings, 1 reply; 2+ messages in thread
From: David McClain @ 2004-08-02  7:43 UTC (permalink / raw)
  To: caml

... in going back over my sources from 5 years ago, it appears that at one
time I was trying (perhaps too hard) to outsmart the internals of OCaml. I
regret having to do this, but bear with me one moment...

I was avoiding declaring CAMLparamN() of input arguments in many cases
because I was attempting to gain a unified access to both of OCaml native
Arrays of double, and my own FastArray implementation which predates the
BigArray stuff. The only time it appeared necessary to declare an input
parameter is when some OCaml allocation could occur, and where we need to
track the possible relocation of those parameters as a result of a triggered
GC cycle.

But if I were completely finished accessing these parameters by the time an
OCaml allocation occurred, then I could care less where these parameters had
possibly been relocated. The caller of my C routines still have live
pointers to these parameters and so they form valid memory roots for GC.
Hence, GC does not need my CAMLparam's to find the system roots, it merely
offers through these macros to allow me to find them again should I need to
after some GC may have relocated them.

In my case with my FastArrays I dare not put them in these CAMLparam macros
because the arena pointer to the FastArray doubles array is not a valid
OCaml structure address, and so I don't want GC pounding on them. These
addresses are external to the OCaml heap, and so I guess OCaml would ignore
them anyway. I was playing fast and loose on purpose here because native
OCaml arrays of doubles do pass the address of their own arena. My C
routines have uniform access to the arena of both kinds of arrays.

I realized there is risk involved in using raw external pointers, especially
as the OCaml heap grows and eventually occupies the same region these
pointers once did. But that can't happen until these pointers are
invalidated and reclaimed anyway.

In looking at BigArray as a possible alternative to my FastArrays, I see
that the address passed for such arguments is much like a Custom block and
reaching the arena requires a special offsetting access from the passed
argument address. Hence these cannot be used interchangeably with normal
arrays of double.

I like the idea of moving back away from taking advantage of special
internal knowledge, since that ensures a greater lifetime to my code.
However, it still appears problematic to do so at this time...

In retrospect, it appears that the bus errors may have been caused by a
failed call to my Berkeley DBMS wrappers while the filename string was still
alive and perhaps still in use. Not sure just yet. This is one case where I
should have, and did, put CAMLparam wrappers on that filename string
argument and the Bus Errors subsequently went away.

But wholesale wrapping of all arguments to C glue routines is really not
warranted, and requires careful consideration of when these parameters will
be accessed in the code, and when OCaml heap allocations will occur. Only
those pointers that are both valid, and still live at the time of allocation
need protection by these CAMLparam wrappers.

Do I understand the situation correctly? Or has something more fundamental
changed in 3.08?

David McClain
Senior Corporate Scientist
Avisere, Inc.

+1.520.390.7738 (USA)
david.mcclain@avisere.com



-------------------
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] 2+ messages in thread

* Re: [Caml-list] OCaml 3.08 and Bus Errors
  2004-08-02  7:43 [Caml-list] OCaml 3.08 and Bus Errors David McClain
@ 2004-08-06 12:09 ` Damien Doligez
  0 siblings, 0 replies; 2+ messages in thread
From: Damien Doligez @ 2004-08-06 12:09 UTC (permalink / raw)
  To: David McClain; +Cc: caml

On Aug 2, 2004, at 09:43, David McClain wrote:

[You don't have to use the CAMLparamN macros if you never access any 
heap
  value after calling a function that might allocate in the Caml heap, 
but
  be aware that the caml_failwith does allocate before accessing its 
argument.]

> Do I understand the situation correctly?

Your assessment of the situation is perfectly correct.

>  Or has something more fundamental changed in 3.08?

IIRC, there was no fundamental change between 3.07 and 3.08.0 in this
part of the system.

-- Damien

-------------------
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] 2+ messages in thread

end of thread, other threads:[~2004-08-06 12:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-02  7:43 [Caml-list] OCaml 3.08 and Bus Errors David McClain
2004-08-06 12:09 ` Damien Doligez

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).