| 1 |
(* Copyright (C) 2013,2019,2022 Matthew Fluet. |
| 2 |
* Copyright (C) 1999-2009 Henry Cejtin, Matthew Fluet, Suresh |
| 3 |
* Jagannathan, and Stephen Weeks. |
| 4 |
* Copyright (C) 1997-2000 NEC Research Institute. |
| 5 |
* |
| 6 |
* MLton is released under a HPND-style license. |
| 7 |
* See the file MLton-LICENSE for details. |
| 8 |
*) |
| 9 |
|
| 10 |
functor MkIO (S : sig |
| 11 |
type outstream |
| 12 |
val openOut: string -> outstream |
| 13 |
end) = |
| 14 |
struct |
| 15 |
open S |
| 16 |
|
| 17 |
fun mkstemps {prefix, suffix} = |
| 18 |
let |
| 19 |
val name = concat [prefix, MLtonRandom.alphaNumString 6, suffix] |
| 20 |
in |
| 21 |
(* Make sure the temporary file name doesn't already exist. *) |
| 22 |
if OS.FileSys.access (name, []) |
| 23 |
then mkstemps {prefix = prefix, suffix = suffix} |
| 24 |
else (name, openOut name) |
| 25 |
end |
| 26 |
fun mkstemp s = mkstemps {prefix = s, suffix = ""} |
| 27 |
fun tempPrefix _ = raise Fail "MLton.IO.tempPrefix" |
| 28 |
end |
| 29 |
|
| 30 |
(* This file is just a dummy provided in place of the structure that MLton |
| 31 |
* supplies so that we can compile under old MLtons and non-MLton SML compilers. |
| 32 |
*) |
| 33 |
structure MLton: MLTON = |
| 34 |
struct |
| 35 |
structure MLton = PreMLton |
| 36 |
|
| 37 |
val debug = MLton.debug |
| 38 |
val eq = fn _ => raise Fail "MLton.eq" |
| 39 |
val equal = fn _ => raise Fail "MLton.equal" |
| 40 |
val hash = fn _ => raise Fail "MLton.hash" |
| 41 |
val isMLton = MLton.isMLton |
| 42 |
val safe = true |
| 43 |
val share = fn _ => raise Fail "MLton.share" |
| 44 |
val shareAll = fn _ => raise Fail "MLton.shareAll" |
| 45 |
val size: 'a -> IntInf.int = fn _ => ~1 |
| 46 |
val sizeAll: 'a -> IntInf.int = fn _ => ~1 |
| 47 |
|
| 48 |
structure Array = |
| 49 |
struct |
| 50 |
open Array |
| 51 |
|
| 52 |
fun unfoldi (n, a, f) = |
| 53 |
let |
| 54 |
val r = ref a |
| 55 |
val a = |
| 56 |
tabulate (n, fn i => |
| 57 |
let |
| 58 |
val (b, a') = f (i, !r) |
| 59 |
val _ = r := a' |
| 60 |
in |
| 61 |
b |
| 62 |
end) |
| 63 |
in |
| 64 |
(a, !r) |
| 65 |
end |
| 66 |
end |
| 67 |
|
| 68 |
structure Exn = MLton.Exn |
| 69 |
|
| 70 |
structure GC = MLton.GC |
| 71 |
|
| 72 |
structure Platform = MLtonPlatform |
| 73 |
|
| 74 |
structure Process = MLton.Process |
| 75 |
|
| 76 |
structure Profile = |
| 77 |
struct |
| 78 |
structure Data = |
| 79 |
struct |
| 80 |
type t = unit |
| 81 |
|
| 82 |
val equals = fn _ => raise Fail "Profile.Data.equals" |
| 83 |
val free = fn _ => raise Fail "Profile.Data.free" |
| 84 |
val malloc = fn _ => raise Fail "Profile.Data.malloc" |
| 85 |
val write = fn _ => raise Fail "Profile.Data.write" |
| 86 |
end |
| 87 |
val isOn = false |
| 88 |
val withData = fn _ => raise Fail "Profile.withData" |
| 89 |
end |
| 90 |
|
| 91 |
structure Random = MLtonRandom |
| 92 |
|
| 93 |
structure Rusage = |
| 94 |
struct |
| 95 |
type t = {stime: Time.time, utime: Time.time} |
| 96 |
|
| 97 |
fun measureGC _ = () |
| 98 |
|
| 99 |
(* Fake it with Posix.ProcEnv.times |
| 100 |
* and Timer.totalCPUTimer and Timer.checkCPUTimes. |
| 101 |
*) |
| 102 |
fun rusage () = |
| 103 |
let |
| 104 |
val zero = {utime = Time.zeroTime, stime = Time.zeroTime} |
| 105 |
in |
| 106 |
let |
| 107 |
val {gc = {usr = gcutime, sys = gcstime}, ...} = |
| 108 |
Timer.checkCPUTimes (Timer.totalCPUTimer ()) |
| 109 |
val {utime, stime, cutime, cstime, ...} = |
| 110 |
Posix.ProcEnv.times () |
| 111 |
in |
| 112 |
{self = {utime = utime, stime = stime}, |
| 113 |
children = {utime = cutime, stime = cstime}, |
| 114 |
gc = {utime = gcutime, stime = gcstime}} |
| 115 |
end handle Time => {children = zero, gc = zero, self = zero} |
| 116 |
(* The handle Time is there because of a bug in SML/NJ that |
| 117 |
* causes a Time exception to be raised on machines with a |
| 118 |
* large uptime (enough that the number of clock ticks is |
| 119 |
* >= 2^31). |
| 120 |
*) |
| 121 |
end |
| 122 |
end |
| 123 |
|
| 124 |
structure TextIO = MkIO (TextIO) |
| 125 |
|
| 126 |
structure Vector = |
| 127 |
struct |
| 128 |
open Vector |
| 129 |
|
| 130 |
fun create n = |
| 131 |
let |
| 132 |
val r = ref (Array.fromList []) |
| 133 |
val subLim = ref 0 |
| 134 |
fun sub i = |
| 135 |
if 0 <= i andalso i < !subLim |
| 136 |
then Array.sub (!r, i) |
| 137 |
else raise Subscript |
| 138 |
val updateLim = ref 0 |
| 139 |
fun update (i, x) = |
| 140 |
if 0 <= i andalso i < !updateLim |
| 141 |
then if i = !updateLim andalso i < n |
| 142 |
then (r := (Array.tabulate (i + 1, fn j => |
| 143 |
if i = j |
| 144 |
then x |
| 145 |
else Array.sub (!r, j))); |
| 146 |
subLim := i + 1; |
| 147 |
updateLim := i + 1) |
| 148 |
else raise Subscript |
| 149 |
else |
| 150 |
Array.update (!r, i, x) |
| 151 |
val gotIt = ref false |
| 152 |
fun done () = |
| 153 |
if !gotIt then |
| 154 |
raise Fail "already got vector" |
| 155 |
else |
| 156 |
if n = !updateLim then |
| 157 |
(gotIt := true; |
| 158 |
updateLim := 0; |
| 159 |
Array.vector (!r)) |
| 160 |
else |
| 161 |
raise Fail "vector not full" |
| 162 |
in |
| 163 |
{done = done, |
| 164 |
sub = sub, |
| 165 |
update = update} |
| 166 |
end |
| 167 |
|
| 168 |
fun unfoldi (n, a, f) = |
| 169 |
let |
| 170 |
val r = ref a |
| 171 |
val v = |
| 172 |
tabulate (n, fn i => |
| 173 |
let |
| 174 |
val (b, a') = f (i, !r) |
| 175 |
val _ = r := a' |
| 176 |
in |
| 177 |
b |
| 178 |
end) |
| 179 |
in |
| 180 |
(v, !r) |
| 181 |
end |
| 182 |
end |
| 183 |
end |