Conversation
|
That is indeed a very good idea for bindings. Thanks. |
|
I'm surprised EDIT: Or I misunderstood the problematic cases. |
|
@bobot Given that |
|
@bobot Is it these two? https://github.com/ocaml/ocaml/blob/trunk/runtime/weak.c#L63-L79 |
|
I've covered those cases @bobot. |
|
For the record: I'm not enthusiastic at the idea that NULL (the all-zero bit pattern) is guaranteed to be a well-formed OCaml value, even after we get rid of "naked pointers". In the motivating example, |
|
A value guaranteed not to overlap with any ordinary OCaml values would be a useful addition to I don't think that is the main motivation here, but I'd still be very grateful for it. |
Except that if this particular value is made easily available under the name |
|
Noting down for the sake of completeness: Marshaling needs to be made aware of |
|
Do we really want to be able to marshal |
|
Is the NULL pointer value actually used in many C bindings? The two examples from #9534 aren't terribly convincing. In In This does not in fact create a zero word, as only the second word of |
|
I am yet to see an example that requires |
|
The frama-c |
Cc the authors @pascal-cuoq and @damiendoligez . Yet, to me, this code is one of the very few legitimate uses of the Obj module around. Where do we go from here? I don't know. |
|
Just to clarify, the functions and structures in this module are fine, and handled correctly by flambda2. The problematic parts are the endianness computations and the zeroword definition, for which a C stub would have been much better in my opinion (and for integer endianness, at least, it's available as |
|
@kayceesrk , @stedolan and I have expressed doubts on the usefulness of |
|
Yes. Let's close this PR since we haven't found a convincing usecase for null yet. |
This PR adds a distinguished
nullvalue to theObjmodule that has the same bit pattern asNULLin C (all bits set to 0 according to C99 standard).The need for
Obj.nullC bindings and low-level code sometimes have the need to represent a value that is either
NULLor is a valid OCaml value. This is permitted by the OCaml compiler as the GC uses the page table to identify thatNULLis outside the heap and skips following the object. However, withno-naked-pointersmode, where the GC only consults the page table for code pointers,NULLvalue to be a value on the major heap i.e,Is_block(NULL) && !Is_young(NULL)holds. This would lead to a crash if the GC follows theNULLpointer.Implementation
In the naked pointer mode, the only places where the GC decides whether to follow a pointer is in
caml_darkenandmark_slice_darken. We add a check to see if the value is non-NULL before following it.edit: In
weak.c, the checks inMust_be_Marked_during_markandIs_Dead_during_cleannow check forNULLvalue.Interaction with Multicore OCaml
This scheme is also compatible with the multicore compiler. Multicore currently uses a parallel minor GC, and
Obj.nullposes no special issues. The alternative concurrent minor GC uses a fast read barrier test to decide whether a pointer is in a foreign minor heap arena. See section 4.3.2 in the multicore GC paper.Assuming a similar 16-bit address space, the precondition we need is that
PQ(bx) != 0 /\ PQ(bx) != 1.It is unlikely that the minor heap area gets allocated between at the very beginning of the virtual memory area. If we get unlucky, we'll just reserve that space and request another contiguous virtual memory chunk. In the worst case, on 64-bit address space, with the current multicore settings for the number of domains (128 max) with each domain having a maximum 16MB of minor heap arena, we may have to reserve (not allocate) 8GB of virtual address space. @stedolan.