package ctypes

  1. Overview
  2. Docs
Combinators for binding to C libraries without writing any C

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.23.0.tar.gz
sha256=cae47d815b27dd4c824a007f1145856044542fe2588d23a443ef4eefec360bf1
md5=b1af973ec9cf7867a63714e92df82f2a

doc/src/ctypes/ctypes_ptr.ml.html

Source file ctypes_ptr.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(*
 * Copyright (c) 2013 Jeremy Yallop.
 *
 * This file is distributed under the terms of the MIT License.
 * See the file LICENSE for details.
 *)

(* Boxed pointers to C memory locations . *)

[@@@warning "-9"]

module Raw = struct
  include Nativeint

  let of_nativeint x = x
  let to_nativeint x = x

  let null = zero
end

type voidp = Raw.t

module Fat :
sig
  (** A fat pointer, which holds a reference to the reference type, the C memory
      location, and an OCaml object. *)
  type (_,_) t

  (** [make ?managed ~reftyp raw] builds a fat pointer from the reference
      type [reftyp], the C memory location [raw], and (optionally) an OCaml
      value, [managed].  The [managed] argument may be used to manage the
      lifetime of the C object; a typical use it to attach a finaliser to
      [managed] which releases the memory associated with the C object whose
      address is stored in [raw_ptr]. *)
  val make : managed:'m -> reftyp:'typ -> voidp -> ('m,'typ) t

  val is_null : (_,_) t -> bool

  val reftype : (_,'typ) t -> 'typ

  val managed : ('m,_) t -> 'm

  val set_managed : ('m,_) t -> 'm -> unit

  val coerce : ('m,_) t -> 'typ -> ('m,'typ) t

  (** Return the raw pointer address.  The function is unsafe in the sense
      that it dissociates the address from the value which manages the memory,
      which may trigger associated finalisers, invalidating the address. *)
  val unsafe_raw_addr : (_,_) t -> voidp

  val add_bytes : ('m,'typ) t -> int -> ('m,'typ) t

  val compare : (_,'typ) t -> (_,'typ) t -> int

  val diff_bytes : (_,'typ) t -> (_,'typ) t -> int
end =
struct
  type ('m, 'typ) t =
    { reftyp  : 'typ;
      raw     : voidp;
      mutable managed : 'm; }

  let make ~managed ~reftyp raw = { reftyp; raw; managed }

  let is_null { raw } = Raw.(compare zero) raw = 0

  let reftype { reftyp } = reftyp

  let managed { managed } = managed

  let set_managed p m = p.managed <- m

  let coerce p reftyp = { p with reftyp }

  let unsafe_raw_addr { raw } = raw

  let add_bytes p bytes = { p with raw = Raw.(add p.raw (of_int bytes)) }

  let compare l r = Raw.compare l.raw r.raw

  let diff_bytes l r = Raw.(to_int (sub r.raw l.raw))
end
OCaml

Innovation. Community. Security.