Caml_modify(), use outside of block fields

While reviewing my C interface stuff, I noticed a function caml_modify()

Otherwise, you are updating a field that previously contained a well-formed value; then, call the caml_modify function:

    caml_modify(&Field(v, n), w);

… and goes on to show it used that way, while making a list. Which I noticed because in fact I do build a list, but I don’t modify a block field. It happens in a loop, as seemed to me the obvious way -

                CAMLlocal3(tl, hd, mval);
                tl = Val_int(0);
                int i;
                for (i = count - 1; i >= 0; --i) {
                      int32 ival;
                      ... // ival to be cons'd to list
                      hd = caml_alloc(2, 0);
                      mval = Val_int(ival);
                      Store_field(hd, 0, mval);
                      Store_field(hd, 1, tl);
                      tl = hd;  // <--- oops?  should be caml_modify(&tl, hd); ??
               }  

So I just tried it, and it seems to be an improvement. If I’m right, maybe caml_modify() warrants more than incidental mention in the C interface documentation.

No, you do not need caml_modify here. Variable tl is a local variable that is already seen as a root by the garbage collector. You do not need to warn the GC that it might point to an allocated block. Function caml_modify/caml_initialize is needed when you make the field of a block on the major heap points to a block on the minor heap. And because it might be cumbersome to track dynamically on which heap a block lives, the previous rule becomes: Function caml_modify/caml_initialize is needed when you make the field of a not immediately allocated block points to another block.