Module Picos_fd

Externally reference counted file descriptors.

val report_leaks : bool Stdlib.ref

If !report_leaks = true then undisposed file descriptors will be reported at exit.

include Picos_rc.S with type Resource.t = Unix.file_descr
module Resource : Picos_rc.Resource with type t = Unix.file_descr

Resource type.

type t

Opaque type alias for the resource type.

val create : ?dispose:bool -> Resource.t -> t

create resource adds an entry for the resource with an initial reference count of 1 to the table for the resource and returns the resource as a value of the opaque alias type.

The optional dispose argument defaults to true. When explicitly specified as ~dispose:false, the resource will not be disposed when the reference count becomes zero. This is intended for special cases where a resource is e.g. managed outside of the control of the user program.

val unsafe_get : t -> Resource.t

unsafe_get opaque_resource casts the opaque alias type back to the resource type.

⚠️ This should only be called and the resource used either after creating the reference counting entry or after incrementing the reference count and before the matching decrement.

val incr : t -> unit

incr opaque_resource tries to find the entry for the resource and increment the reference count.

  • raises Invalid_argument

    in case no entry is found for the resource or the reference count was zero or the resource was marked as closed previously by a decrement operation.

val decr : ?close:bool -> t -> unit

decr opaque_resource tries to find the entry for the resource and decrement the reference count. If the reference count becomes zero, the entry for the resource will be removed and the resource will be disposed, unless ~dispose:false was specified for create.

The optional close argument defaults to false. When explicitly specified as ~close:true the resource will be marked as closed and attempts to increment the reference will fail.

  • raises Invalid_argument

    in case no entry is found for the resource or the reference count was zero.

type info = {
  1. resource : Resource.t;
    (*

    The resource.

    *)
  2. count : int;
    (*

    Reference count. This may be 0.

    *)
  3. closed : bool;
    (*

    Whether the resource has been closed, see decr.

    *)
  4. dispose : bool;
    (*

    Whether to dispose the resource, see create.

    *)
  5. bt : Stdlib.Printexc.raw_backtrace;
    (*

    Backtrace captured at create.

    *)
}

Information on a resource.

val infos : unit -> info Stdlib.Seq.t

infos () returns a sequence of entries in the reference counting table at the point in time of of calling infos ().