Are custom blocks ever copied by OCaml?

谁说我不能喝 提交于 2019-12-05 08:59:58

The custom block itself, that is, the bytes obtained from caml_alloc_custom, is part of the Caml heap and can be moved like any other object.¹ It's very common for the custom block to contain pointers to data structures that are also accessed by C² code and live outside the Caml heap; Caml treats the contents of the custom block as opaque and doesn't even know whether it contains pointers, so it won't touch these data structures.

When you write let meow = fluffy, there is no copy going on: you're just giving a new name to the same object. Caml will never duplicate a custom block; if you want that, you have to provide a copy_cat primitive in your library.

¹ Only the minor garbage collector and the compactor actually move blocks around, the major gc doesn't. But that's not something you should rely on.

² Or Fortran, or whatever other language your program or library uses.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!