package core_kernel

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file pool_intf.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
(** A manual memory manager for a set of mutable tuples.  The point of [Pool] is to
    allocate a single long-lived block of memory (the pool) that lives in the OCaml
    major heap, and then to reuse the block, rather than continually allocating blocks
    on the minor heap.

    A pool stores a bounded-size set of tuples, where client code is responsible for
    explicitly controlling when the pool allocates and frees tuples.  One [create]s a
    pool of a certain capacity, which returns an empty pool that can hold that many
    tuples.  One then uses [new] to allocate a tuple, which returns a [Pointer.t] to the
    tuple.  One then uses [get] and [set] along with the pointer to get and set slots of
    the tuple.  Finally, one [free]'s a pointer to the pool's memory for a tuple, making
    the memory available for subsequent reuse.

    In typical usage, one wraps up a pool with an abstract interface, giving nice names
    to the tuple slots, and only exposing mutation where desired.

    All the usual problems with manual memory allocation are present with pools:

    - one can mistakenly use a pointer after it is freed
    - one can mistakenly free a pointer multiple times
    - one can forget to free a pointer

    There is a debugging functor, [Pool.Error_check], that is useful for building pools
    to help debug incorrect pointer usage.
*)

open! Import

(** [S] is the module type for a pool. *)
module type S = sig
  module Slots : Tuple_type.Slots
  module Slot : Tuple_type.Slot

  module Pointer : sig
    (** A pointer to a tuple in a pool.  ['slots] will look like [('a1, ..., 'an)
        Slots.tn], and the tuples have type ['a1 * ... * 'an]. *)
    type 'slots t [@@deriving sexp_of, typerep]

    (** The [null] pointer is a distinct pointer that does not correspond to a tuple in
        the pool.  It is a function to prevent problems due to the value restriction. *)
    val null : unit -> _ t

    val is_null : _ t -> bool
    val phys_compare : 'a t -> 'a t -> int
    val phys_equal : 'a t -> 'a t -> bool

    module Id : sig
      type t [@@deriving bin_io, sexp]

      val to_int63 : t -> Int63.t
      val of_int63 : Int63.t -> t
    end
  end

  (** A pool.  ['slots] will look like [('a1, ..., 'an) Slots.tn], and the pool holds
      tuples of type ['a1 * ... * 'an]. *)
  type 'slots t [@@deriving sexp_of]

  include Invariant.S1 with type 'a t := 'a t

  (** [pointer_is_valid t pointer] returns [true] iff [pointer] points to a live tuple in
      [t], i.e. [pointer] is not null, not free, and is in the range of [t].

      A pointer might not be in the range of a pool if it comes from another pool for
      example.  In this case unsafe_get/set functions would cause a segfault. *)
  val pointer_is_valid : 'slots t -> 'slots Pointer.t -> bool

  (** [id_of_pointer t pointer] returns an id that is unique for the lifetime of
      [pointer]'s tuple.  When the tuple is freed, the id is no longer valid, and
      [pointer_of_id_exn] will fail on it.  [Pointer.null ()] has a distinct id from all
      non-null pointers. *)
  val id_of_pointer : 'slots t -> 'slots Pointer.t -> Pointer.Id.t

  (** [pointer_of_id_exn t id] returns the pointer corresponding to [id].  It fails if the
      tuple corresponding to [id] was already [free]d. *)
  val pointer_of_id_exn : 'slots t -> Pointer.Id.t -> 'slots Pointer.t

  (** [create slots ~capacity ~dummy] creates an empty pool that can hold up to [capacity]
      N-tuples.  The slots of [dummy] are stored in free tuples.  [create] raises if
      [capacity < 0 || capacity > max_capacity ~slots_per_tuple]. *)
  val create
    :  (('tuple, _) Slots.t as 'slots)
    -> capacity:int
    -> dummy:'tuple
    -> 'slots t

  (** [max_capacity] returns the maximum capacity allowed when creating a pool. *)
  val max_capacity : slots_per_tuple:int -> int

  (** [capacity] returns the maximum number of tuples that the pool can hold. *)
  val capacity : _ t -> int

  (** [length] returns the number of tuples currently in the pool.

      {[
        0 <= length t <= capacity t
      ]}
  *)
  val length : _ t -> int

  (** [grow t ~capacity] returns a new pool [t'] with the supplied capacity.  The new pool
      is to be used as a replacement for [t].  All live tuples in [t] are now live in
      [t'], and valid pointers to tuples in [t] are now valid pointers to the identical
      tuple in [t'].  It is an error to use [t] after calling [grow t].

      [grow] raises if the supplied capacity isn't larger than [capacity t]. *)
  val grow : ?capacity:int (** default is [2 * capacity t] *) -> 'a t -> 'a t

  (** [is_full t] returns [true] if no more tuples can be allocated in [t]. *)
  val is_full : _ t -> bool

  (** [free t pointer] frees the tuple pointed to by [pointer] from [t]. *)
  val free : 'slots t -> 'slots Pointer.t -> unit

  (** [unsafe_free t pointer] frees the tuple pointed to by [pointer] without checking
      [pointer_is_valid] *)
  val unsafe_free : 'slots t -> 'slots Pointer.t -> unit

  (** [new<N> t a0 ... a<N-1>] returns a new tuple from the pool, with the tuple's
      slots initialized to [a0] ... [a<N-1>].  [new] raises if [is_full t]. *)
  val new1 : ('a0 Slots.t1 as 'slots) t -> 'a0 -> 'slots Pointer.t

  val new2 : (('a0, 'a1) Slots.t2 as 'slots) t -> 'a0 -> 'a1 -> 'slots Pointer.t

  val new3
    :  (('a0, 'a1, 'a2) Slots.t3 as 'slots) t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'slots Pointer.t

  val new4
    :  (('a0, 'a1, 'a2, 'a3) Slots.t4 as 'slots) t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'slots Pointer.t

  val new5
    :  (('a0, 'a1, 'a2, 'a3, 'a4) Slots.t5 as 'slots) t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'slots Pointer.t

  val new6
    :  (('a0, 'a1, 'a2, 'a3, 'a4, 'a5) Slots.t6 as 'slots) t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'a5
    -> 'slots Pointer.t

  val new7
    :  (('a0, 'a1, 'a2, 'a3, 'a4, 'a5, 'a6) Slots.t7 as 'slots) t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'a5
    -> 'a6
    -> 'slots Pointer.t

  val new8
    :  (('a0, 'a1, 'a2, 'a3, 'a4, 'a5, 'a6, 'a7) Slots.t8 as 'slots) t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'a5
    -> 'a6
    -> 'a7
    -> 'slots Pointer.t

  val new9
    :  (('a0, 'a1, 'a2, 'a3, 'a4, 'a5, 'a6, 'a7, 'a8) Slots.t9 as 'slots) t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'a5
    -> 'a6
    -> 'a7
    -> 'a8
    -> 'slots Pointer.t

  val new10
    :  (('a0, 'a1, 'a2, 'a3, 'a4, 'a5, 'a6, 'a7, 'a8, 'a9) Slots.t10 as 'slots) t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'a5
    -> 'a6
    -> 'a7
    -> 'a8
    -> 'a9
    -> 'slots Pointer.t

  val new11
    :  (('a0, 'a1, 'a2, 'a3, 'a4, 'a5, 'a6, 'a7, 'a8, 'a9, 'a10) Slots.t11 as 'slots) t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'a5
    -> 'a6
    -> 'a7
    -> 'a8
    -> 'a9
    -> 'a10
    -> 'slots Pointer.t

  val new12
    :  (('a0, 'a1, 'a2, 'a3, 'a4, 'a5, 'a6, 'a7, 'a8, 'a9, 'a10, 'a11) Slots.t12 as 'slots)
         t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'a5
    -> 'a6
    -> 'a7
    -> 'a8
    -> 'a9
    -> 'a10
    -> 'a11
    -> 'slots Pointer.t

  val new13
    :  (('a0, 'a1, 'a2, 'a3, 'a4, 'a5, 'a6, 'a7, 'a8, 'a9, 'a10, 'a11, 'a12) Slots.t13
        as
        'slots)
         t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'a5
    -> 'a6
    -> 'a7
    -> 'a8
    -> 'a9
    -> 'a10
    -> 'a11
    -> 'a12
    -> 'slots Pointer.t

  val new14
    :  (( 'a0
        , 'a1
        , 'a2
        , 'a3
        , 'a4
        , 'a5
        , 'a6
        , 'a7
        , 'a8
        , 'a9
        , 'a10
        , 'a11
        , 'a12
        , 'a13 )
          Slots.t14
        as
        'slots)
         t
    -> 'a0
    -> 'a1
    -> 'a2
    -> 'a3
    -> 'a4
    -> 'a5
    -> 'a6
    -> 'a7
    -> 'a8
    -> 'a9
    -> 'a10
    -> 'a11
    -> 'a12
    -> 'a13
    -> 'slots Pointer.t

  (** [get_tuple t pointer] allocates an OCaml tuple isomorphic to the pool [t]'s tuple
      pointed to by [pointer]. The tuple gets copied, but its slots do not. *)
  val get_tuple : (('tuple, _) Slots.t as 'slots) t -> 'slots Pointer.t -> 'tuple

  (** [get t pointer slot] gets [slot] of the tuple pointed to by [pointer] in
      pool [t].

      [set t pointer slot a] sets to [a] the [slot] of the tuple pointed to by [pointer]
      in pool [t].

      In [get] and [set], it is an error to refer to a pointer that has been [free]d.  It
      is also an error to use a pointer with any pool other than the one the pointer was
      [new]'d from or [grow]n to.  These errors will lead to undefined behavior, but will
      not segfault.

      [unsafe_get] is comparable in speed to [get] for immediate values, and 5%-10% faster
      for pointers.

      [unsafe_get] and [unsafe_set] skip bounds checking, and can thus segfault. *)
  val get
    :  ((_, 'variant) Slots.t as 'slots) t
    -> 'slots Pointer.t
    -> ('variant, 'slot) Slot.t
    -> 'slot

  val unsafe_get
    :  ((_, 'variant) Slots.t as 'slots) t
    -> 'slots Pointer.t
    -> ('variant, 'slot) Slot.t
    -> 'slot

  val set
    :  ((_, 'variant) Slots.t as 'slots) t
    -> 'slots Pointer.t
    -> ('variant, 'slot) Slot.t
    -> 'slot
    -> unit

  val unsafe_set
    :  ((_, 'variant) Slots.t as 'slots) t
    -> 'slots Pointer.t
    -> ('variant, 'slot) Slot.t
    -> 'slot
    -> unit
end

module type Pool = sig
  module Tuple_type = Tuple_type

  module type S = S

  (** This uses a [Uniform_array.t] to implement the pool.  We expose that [Pointer.t] is
      an [int] so that OCaml can avoid the write barrier, due to knowing that [Pointer.t]
      isn't an OCaml pointer. *)
  include S with type 'a Pointer.t = private int (** @inline *)

  (** An [Unsafe] pool is like an ordinary pool, except that the [create] function does
      not require an initial element.  The pool stores a dummy value for each slot.
      Such a pool is only safe if one never accesses a slot from a [free]d tuple.

      It makes sense to use [Unsafe] if one has a small constrained chunk of code where
      one can prove that one never accesses a [free]d tuple, and one needs a pool where
      it is difficult to construct a dummy value.

      Some [Unsafe] functions are faster than the corresponding safe version because they
      do not have to maintain values with the correct represention in the [Uniform_array]
      backing the pool: [free], [create], [grow].
  *)
  module Unsafe : sig
    include S with type 'a Pointer.t = private int

    (** [create slots ~capacity] creates an empty pool that can hold up to [capacity]
        N-tuples.  The elements of a [free] tuple may contain stale and/or invalid values
        for their types, and as such any access to a [free] tuple from this pool is
        unsafe. *)
    val create : ((_, _) Slots.t as 'slots) -> capacity:int -> 'slots t
  end

  (** [Debug] builds a pool in which every function can run [invariant] on its pool
      argument(s) and/or print a debug message to stderr, as determined by
      [!check_invariant] and [!show_messages], which are initially both [true].

      The performance of the pool resulting from [Debug] is much worse than that of the
      input [Pool], even with all the controls set to [false]. *)
  module Debug (Pool : S) : sig
    include
      S
      with type 'a Pointer.t = 'a Pool.Pointer.t
      with type Pointer.Id.t = Pool.Pointer.Id.t
      with type 'a t = 'a Pool.t

    val check_invariant : bool ref
    val show_messages : bool ref
  end

  (** [Error_check] builds a pool that has additional error checking for pointers, in
      particular to detect using a [free]d pointer or multiply [free]ing a pointer.

      [Error_check] has a significant performance cost, but less than that of [Debug].

      One can compose [Debug] and [Error_check], e.g:

      {[
        module M = Debug (Error_check (Pool))
      ]}
  *)
  module Error_check (Pool : S) : S
end