自定义块是否被OCaml复制过? [英] Are custom blocks ever copied by OCaml?

查看:117
本文介绍了自定义块是否被OCaml复制过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为libcat的C库,用于与我的猫蓬松交互。

 模块类型CAT = sig 
类型cat
val find:... - > cat
val feed:cat - >单位
...
结束;;
模块Cat:CAT = ...

libcat中已经内置了相当多的内存管理,像说缓存,释放被摧毁的玩具,甚至可能有限的垃圾收集器来清空垃圾。然而,整体libcat要求用户明确地释放未使用的资源,比如丢失的玩具。



我为Cat.find编写了一个C存根,用libcat cat_find例程,然后将结果指针存储到由caml_alloc_custom创建的自定义块中。



我已将finalize方法添加到传递到caml_alloc_custom的custom_operations结构中。至关重要的是,我已经完成了这个最终确定的方法,因为当我接听电话异常时,她已经厌倦了她的门。

我是现在担心,如果OCaml重复了Cat.cat类型的自定义块,那么OCaml的垃圾回收器可能会在我们仍在玩时蓬松起来。例如:

  let fluffy = Cat.find ;; 
fluffy.yodel ;;
let meow = fluffy ;;
...
meow.feed ;;

我们必须假设......会在最后一次显式引用蓬松字符后触发OCaML的垃圾回收器,通过打破盘子。这个垃圾收集事件是要叫蓬松的敲定方法,并释放她?或者喵只是指蓬松的原始自定义块,从而防止蓬松被释放?



我可以想象在这种情况下蓬松不会被释放,否则OCaml会一定要在custom_operations结构中寻求一个重复的方法,但我觉得最好问一下。如果蓬松可能实际上被释放,我可以通过只让OCaml处理她参考来防止这种情况吗?粗略地说:

  type cat_name = real_cat ref 
type real_cat

解决方案

自定义块本身,也就是从 caml_alloc_custom 是Caml堆的一部分,可以像任何其他对象一样移动.¹自定义块包含指向数据结构的指针,这些数据结构也可以通过C²代码访问并存在于Caml堆之外; Caml将自定义块的内容视为不透明,甚至不知道它是否包含指针,因此它不会触及这些数据结构。



当您写 let meow = fluffy ,这里没有复制:你只是给同一个对象一个新的名字。 Caml永远不会复制自定义块;如果你需要的话,你必须在你的库中提供一个 copy_cat 原始元素。

¹
只有小垃圾收集器和压实器实际上移动块,主要的gc不会。但那不是你应该依赖的。



²
或者Fortran或您的程序或库使用的任何其他语言。


Imagine I've a C library called libcat for interacting with my cat fluffy. I'm therefore writing bindings for OCaml to simplify interactions with fluffy.

module type CAT = sig 
   type cat
   val find : ... -> cat
   val feed : cat -> unit
   ...
end ;;
module Cat : CAT = ...

There is considerable memory management already built into libcat, like say caching, freeing destroyed toys, and maybe even a limited scope garbage collector for emptying the litter. Yet, overall libcat requires that users explicitly free unused resources, like lost toys.

I've written a C stub for Cat.find that finds and allocates the cat using libcat's cat_find routine, but then stores the resulting pointer to the cat in a custom block created with caml_alloc_custom.

I've added a finalize method into the custom_operations struct passed into caml_alloc_custom. Crucially, I've made this finalize method free the cat because I'm sick of her scratching on the door while I'm answering a phone exception.

I'm now worried that, if OCaml ever duplicates a custom block of type Cat.cat, then OCaml's garbage collector may free fluffy while we're still playing. For example :

let fluffy = Cat.find ;;
fluffy.yodel ;;
let meow = fluffy ;;
...
meow.feed ;;

We must assume that ... will trigger OCaML's garbage collector after the last explicit reference to fluffy, say by breaking dishes. Is this garbage collection event going to call fluffy's finalize method and free her? Or will meow simply refer to fluffy's original custom block, thus preventing fluffy from being freed?

I'd imagine fluffy isn't freed in this situation, well otherwise OCaml would surely ask for a duplicate method in custom_operations struct, but I felt it better to ask. If fluffy might in fact be freed, can I prevent this by only letting OCaml handle her by reference? Roughly :

  type cat_name = real_cat ref
  type real_cat

解决方案

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.

这篇关于自定义块是否被OCaml复制过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆