type key_type = int type boxed_key = key_type ref ref let mk_key : unit -> boxed_key = (* We should take a random value here, is there a random function in OCaml?*) let bid = ref 0 in (* According to OCaml Gc module documentation, Pervasives.ref is one of the few ways of getting a boxed value the compiler will never alias. *) fun () -> incr bid; Pervasives.ref (Pervasives.ref !bid) (* A phantom type to preserve type safety *) type 'a key = boxed_key (* Comparing keys with == grants that if a key is unmarshalled (in the same process where it was created or in another one) it is not mistaken for an already existing one (unmarshal has no right to alias). If the initial value of bid is taken at random, then one also avoids potential collisions *) module HT = Hashtbl.Make(struct type t = key_type ref let equal k1 k2 = k1 == k2 let hash id = !id end) (* A key is the (unique) value inside a boxed key, hence it does not keep its corresponding boxed key reachable (replacing key_type by boxed_key would make the key always reachable) *) let values : Obj.t HT.t = HT.create 1001 let collect k = HT.remove values !k (* The only reference to the boxed key is the one returned, when the user drops it the value eventually disappears from the values table above *) let create (v : 'a) : 'a key = let k = mk_key () in HT.add values !k (Obj.repr v); Gc.finalise collect k; k let get (k : 'a key) : 'a = Obj.obj (HT.find values !k)