Source file destinations.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
open! Core
let iobuf_destination buf =
let provided_buf = Iobuf.sub_shared buf in
let module Dest = struct
let next_buf ~ensure_capacity =
if ensure_capacity > Iobuf.length provided_buf
then failwith "No more room in [iobuf_destination]";
provided_buf
;;
let close () =
Iobuf.flip_lo provided_buf;
Iobuf.resize ~len:(Iobuf.length provided_buf) buf;
Iobuf.resize ~len:0 provided_buf
;;
end
in
(module Dest : Writer_intf.Destination)
;;
let raw_iobuf_destination buf =
let module Dest = struct
let next_buf ~ensure_capacity =
if ensure_capacity > Iobuf.length buf
then failwith "No more room in [iobuf_destination]";
buf
;;
let close () = ()
end
in
(module Dest : Writer_intf.Destination)
;;
let black_hole_destination ~len ~touch_memory =
let buf = Iobuf.create ~len in
if touch_memory then Iobuf.zero buf;
let module Dest = struct
let next_buf ~ensure_capacity =
Iobuf.reset buf;
if ensure_capacity > Iobuf.length buf
then
raise_s
[%message
"Record too large for [black_hole_destination]"
(ensure_capacity : int)
~destination_capacity:(Iobuf.length buf : int)];
buf
;;
let close () = ()
end
in
(module Dest : Writer_intf.Destination)
;;
module Temp_buffer : sig
type t =
{ copy_to : (module Writer_intf.Destination) -> unit
; dest : (module Writer_intf.Destination)
}
val create : unit -> t
end = struct
type t =
{ copy_to : (module Writer_intf.Destination) -> unit
; dest : (module Writer_intf.Destination)
}
type internal =
{ mutable buffers : (read_write, Iobuf.seek, Iobuf.global) Iobuf.t list }
let create () =
let t = { buffers = [] } in
let module Dest = struct
let next_buf ~ensure_capacity =
match t.buffers with
| buf :: _ when Iobuf.length buf >= ensure_capacity -> buf
| _ ->
let capacity = Int.max ensure_capacity 1_000 in
let buf = Iobuf.create ~len:capacity in
t.buffers <- buf :: t.buffers;
buf
;;
let close () = ()
end
in
let dest = (module Dest : Writer_intf.Destination) in
let copy_to (module D : Writer_intf.Destination) =
let in_order_buffers = List.rev t.buffers in
let out_buf = ref (D.next_buf ~ensure_capacity:0) in
List.iter in_order_buffers ~f:(fun in_buf ->
Iobuf.flip_lo in_buf;
let in_buf_len = Iobuf.length in_buf in
if Iobuf.length !out_buf < in_buf_len
then out_buf := D.next_buf ~ensure_capacity:in_buf_len;
Iobuf.Blit_fill.blito ~src:in_buf ~dst:!out_buf ());
t.buffers <- []
in
{ copy_to; dest }
;;
end
module Buffer_until_initialized = struct
type state =
| Buffering_to of Temp_buffer.t
| Needs_transfer of
{ src : Temp_buffer.t
; dst : (module Writer_intf.Destination)
}
| Set of (module Writer_intf.Destination)
type t = { mutable state : state }
let create () =
let temp_buffer = Temp_buffer.create () in
{ state = Buffering_to temp_buffer }
;;
let set_destination t destination =
match t.state with
| Buffering_to temp_buffer ->
t.state <- Needs_transfer { src = temp_buffer; dst = destination }
| Needs_transfer _ | Set _ ->
failwith "Tried to set Buffer_until_initialized which already had destination"
;;
let to_destination t =
let module Dest = struct
let next_buf ~ensure_capacity =
let (module D) =
match t.state with
| Needs_transfer { src; dst } ->
src.copy_to dst;
t.state <- Set dst;
dst
| Buffering_to temp_buffer -> temp_buffer.dest
| Set d -> d
in
D.next_buf ~ensure_capacity
;;
let close () =
let (module D) =
match t.state with
| Needs_transfer { src; dst } ->
src.copy_to dst;
dst
| Buffering_to temp_buffer -> temp_buffer.dest
| Set d -> d
in
D.close ();
t.state <- Set (black_hole_destination ~len:1024 ~touch_memory:false)
;;
end
in
(module Dest : Writer_intf.Destination)
;;
end