@grain/stdlib 0.5.12 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +200 -0
- package/LICENSE +1 -1
- package/README.md +25 -2
- package/array.gr +1512 -199
- package/array.md +2032 -94
- package/bigint.gr +239 -140
- package/bigint.md +450 -106
- package/buffer.gr +595 -102
- package/buffer.md +903 -145
- package/bytes.gr +401 -110
- package/bytes.md +551 -63
- package/char.gr +228 -49
- package/char.md +373 -7
- package/exception.gr +26 -12
- package/exception.md +29 -5
- package/float32.gr +130 -109
- package/float32.md +185 -57
- package/float64.gr +112 -99
- package/float64.md +185 -57
- package/hash.gr +47 -37
- package/hash.md +21 -3
- package/int16.gr +430 -0
- package/int16.md +618 -0
- package/int32.gr +200 -269
- package/int32.md +254 -289
- package/int64.gr +142 -225
- package/int64.md +254 -289
- package/int8.gr +511 -0
- package/int8.md +786 -0
- package/json.gr +2084 -0
- package/json.md +608 -0
- package/list.gr +120 -68
- package/list.md +125 -80
- package/map.gr +560 -57
- package/map.md +672 -56
- package/marshal.gr +239 -227
- package/marshal.md +36 -4
- package/number.gr +626 -676
- package/number.md +738 -153
- package/option.gr +33 -35
- package/option.md +58 -42
- package/package.json +2 -2
- package/path.gr +148 -187
- package/path.md +47 -96
- package/pervasives.gr +75 -416
- package/pervasives.md +85 -180
- package/priorityqueue.gr +433 -74
- package/priorityqueue.md +422 -54
- package/queue.gr +362 -80
- package/queue.md +433 -38
- package/random.gr +67 -75
- package/random.md +68 -40
- package/range.gr +135 -63
- package/range.md +198 -43
- package/rational.gr +284 -0
- package/rational.md +545 -0
- package/regex.gr +933 -1066
- package/regex.md +59 -60
- package/result.gr +23 -25
- package/result.md +54 -39
- package/runtime/atof/common.gr +78 -82
- package/runtime/atof/common.md +22 -10
- package/runtime/atof/decimal.gr +102 -127
- package/runtime/atof/decimal.md +28 -7
- package/runtime/atof/lemire.gr +56 -71
- package/runtime/atof/lemire.md +9 -1
- package/runtime/atof/parse.gr +83 -110
- package/runtime/atof/parse.md +12 -2
- package/runtime/atof/slow.gr +28 -35
- package/runtime/atof/slow.md +9 -1
- package/runtime/atof/table.gr +19 -18
- package/runtime/atof/table.md +10 -2
- package/runtime/atoi/parse.gr +153 -136
- package/runtime/atoi/parse.md +50 -1
- package/runtime/bigint.gr +410 -517
- package/runtime/bigint.md +71 -57
- package/runtime/compare.gr +176 -85
- package/runtime/compare.md +31 -1
- package/runtime/dataStructures.gr +144 -32
- package/runtime/dataStructures.md +267 -31
- package/runtime/debugPrint.gr +34 -15
- package/runtime/debugPrint.md +37 -5
- package/runtime/equal.gr +53 -52
- package/runtime/equal.md +30 -1
- package/runtime/exception.gr +38 -47
- package/runtime/exception.md +10 -8
- package/runtime/gc.gr +23 -152
- package/runtime/gc.md +13 -17
- package/runtime/malloc.gr +31 -31
- package/runtime/malloc.md +11 -3
- package/runtime/numberUtils.gr +191 -172
- package/runtime/numberUtils.md +17 -9
- package/runtime/numbers.gr +1695 -1021
- package/runtime/numbers.md +1098 -134
- package/runtime/string.gr +540 -242
- package/runtime/string.md +76 -6
- package/runtime/unsafe/constants.gr +30 -13
- package/runtime/unsafe/constants.md +80 -0
- package/runtime/unsafe/conv.gr +55 -28
- package/runtime/unsafe/conv.md +41 -9
- package/runtime/unsafe/memory.gr +10 -30
- package/runtime/unsafe/memory.md +15 -19
- package/runtime/unsafe/tags.gr +37 -21
- package/runtime/unsafe/tags.md +88 -8
- package/runtime/unsafe/wasmf32.gr +30 -36
- package/runtime/unsafe/wasmf32.md +64 -56
- package/runtime/unsafe/wasmf64.gr +30 -36
- package/runtime/unsafe/wasmf64.md +64 -56
- package/runtime/unsafe/wasmi32.gr +49 -66
- package/runtime/unsafe/wasmi32.md +102 -94
- package/runtime/unsafe/wasmi64.gr +52 -79
- package/runtime/unsafe/wasmi64.md +108 -100
- package/runtime/utils/printing.gr +13 -15
- package/runtime/utils/printing.md +11 -3
- package/runtime/wasi.gr +294 -295
- package/runtime/wasi.md +62 -42
- package/set.gr +574 -64
- package/set.md +634 -54
- package/stack.gr +181 -64
- package/stack.md +271 -42
- package/string.gr +453 -533
- package/string.md +241 -151
- package/uint16.gr +369 -0
- package/uint16.md +585 -0
- package/uint32.gr +470 -0
- package/uint32.md +737 -0
- package/uint64.gr +471 -0
- package/uint64.md +737 -0
- package/uint8.gr +369 -0
- package/uint8.md +585 -0
- package/uri.gr +1093 -0
- package/uri.md +477 -0
- package/{sys → wasi}/file.gr +914 -500
- package/{sys → wasi}/file.md +454 -50
- package/wasi/process.gr +292 -0
- package/{sys → wasi}/process.md +164 -6
- package/wasi/random.gr +77 -0
- package/wasi/random.md +80 -0
- package/{sys → wasi}/time.gr +15 -22
- package/{sys → wasi}/time.md +5 -5
- package/immutablearray.gr +0 -929
- package/immutablearray.md +0 -1038
- package/immutablemap.gr +0 -493
- package/immutablemap.md +0 -479
- package/immutablepriorityqueue.gr +0 -360
- package/immutablepriorityqueue.md +0 -291
- package/immutableset.gr +0 -498
- package/immutableset.md +0 -449
- package/runtime/debug.gr +0 -2
- package/runtime/debug.md +0 -6
- package/runtime/unsafe/errors.gr +0 -36
- package/runtime/unsafe/errors.md +0 -204
- package/sys/process.gr +0 -254
- package/sys/random.gr +0 -79
- package/sys/random.md +0 -66
package/runtime/malloc.gr
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
@runtimeMode
|
|
2
|
+
module Malloc
|
|
2
3
|
|
|
3
4
|
/*
|
|
4
5
|
* This module implements a generic memory allocator.
|
|
@@ -6,29 +7,30 @@
|
|
|
6
7
|
* from pages 185-188 of K&R C (2nd edition).
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
from "runtime/unsafe/wasmi32" include WasmI32
|
|
11
|
+
use WasmI32.{
|
|
12
|
+
(+),
|
|
13
|
+
(-),
|
|
14
|
+
(*),
|
|
13
15
|
divU as (/),
|
|
14
16
|
gtU as (>),
|
|
15
17
|
geU as (>=),
|
|
16
18
|
ltU as (<),
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
19
|
+
(<<),
|
|
20
|
+
(>>>),
|
|
21
|
+
(==),
|
|
22
|
+
(!=),
|
|
23
|
+
}
|
|
24
|
+
from "runtime/exception" include Exception
|
|
23
25
|
|
|
24
|
-
primitive memorySize
|
|
25
|
-
primitive memoryGrow
|
|
26
|
+
primitive memorySize = "@wasm.memory_size"
|
|
27
|
+
primitive memoryGrow = "@wasm.memory_grow"
|
|
26
28
|
|
|
27
|
-
primitive (!)
|
|
28
|
-
primitive (&&)
|
|
29
|
-
primitive (||)
|
|
29
|
+
primitive (!) = "@not"
|
|
30
|
+
primitive (&&) = "@and"
|
|
31
|
+
primitive (||) = "@or"
|
|
30
32
|
|
|
31
|
-
primitive
|
|
33
|
+
primitive heapStart = "@heap.start"
|
|
32
34
|
|
|
33
35
|
/* UNDERSTANDING THE STRUCTURE OF THE FREE LIST
|
|
34
36
|
* The original K&R definition for the free list entry type was the following:
|
|
@@ -74,13 +76,13 @@ let logMallocHeaderSize = 3n
|
|
|
74
76
|
*/
|
|
75
77
|
let mut heapSize = 0n
|
|
76
78
|
|
|
77
|
-
|
|
79
|
+
provide let _RESERVED_RUNTIME_SPACE = 0x800n
|
|
78
80
|
|
|
79
81
|
/**
|
|
80
82
|
* The base the heap. The block at this address will be size 0 and
|
|
81
83
|
* serve as the root of the free list.
|
|
82
84
|
*/
|
|
83
|
-
let _BASE =
|
|
85
|
+
let _BASE = heapStart() + _RESERVED_RUNTIME_SPACE
|
|
84
86
|
|
|
85
87
|
/**
|
|
86
88
|
* The start pointer of the heap.
|
|
@@ -129,7 +131,7 @@ let growHeap = (nbytes: WasmI32) => {
|
|
|
129
131
|
// More bytes requested than the initial heap size,
|
|
130
132
|
// so we need to request more anyway.
|
|
131
133
|
reqSize = nbytes - heapSize
|
|
132
|
-
reqSize = reqSize
|
|
134
|
+
reqSize = reqSize >>> 16n
|
|
133
135
|
reqSize += 1n
|
|
134
136
|
reqResult = memoryGrow(reqSize)
|
|
135
137
|
if (reqResult == -1n) {
|
|
@@ -146,7 +148,7 @@ let growHeap = (nbytes: WasmI32) => {
|
|
|
146
148
|
let newRegionStart = memorySize() * _PAGE_SIZE
|
|
147
149
|
|
|
148
150
|
reqSize = nbytes
|
|
149
|
-
reqSize = reqSize
|
|
151
|
+
reqSize = reqSize >>> 16n
|
|
150
152
|
reqSize += 1n
|
|
151
153
|
|
|
152
154
|
reqResult = memoryGrow(reqSize)
|
|
@@ -164,7 +166,7 @@ let growHeap = (nbytes: WasmI32) => {
|
|
|
164
166
|
*
|
|
165
167
|
* @param ap: The pointer to free
|
|
166
168
|
*/
|
|
167
|
-
|
|
169
|
+
provide let free = (ap: WasmI32) => {
|
|
168
170
|
let mut blockPtr = ap - 8n // 8 bytes for malloc header
|
|
169
171
|
let mut p = freePtr
|
|
170
172
|
|
|
@@ -238,8 +240,8 @@ let morecore = (nbytes: WasmI32) => {
|
|
|
238
240
|
* @param nbytes: The number of bytes to allocate
|
|
239
241
|
* @returns The pointer to the allocated region (8-byte aligned) or -1 if the allocation failed
|
|
240
242
|
*/
|
|
241
|
-
|
|
242
|
-
let mut nbytes =
|
|
243
|
+
provide let malloc = (nbytes: WasmI32) => {
|
|
244
|
+
let mut nbytes = nbytes
|
|
243
245
|
let mut prevp = freePtr
|
|
244
246
|
|
|
245
247
|
// Set nbytes to the next multiple of mallocHeaderSize greater
|
|
@@ -258,12 +260,10 @@ export let malloc = (nb: WasmI32) => {
|
|
|
258
260
|
let mut ret = -1n
|
|
259
261
|
|
|
260
262
|
// Search the freelist for any blocks large enough.
|
|
261
|
-
for (
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
266
|
-
) {
|
|
263
|
+
for (let mut p = getNext(prevp);; {
|
|
264
|
+
prevp = p
|
|
265
|
+
p = getNext(p)
|
|
266
|
+
}) {
|
|
267
267
|
let size = getSize(p)
|
|
268
268
|
if (size >= nbytes) {
|
|
269
269
|
// If this block is big enough, allocate from it.
|
|
@@ -305,6 +305,6 @@ export let malloc = (nb: WasmI32) => {
|
|
|
305
305
|
*
|
|
306
306
|
* @returns The free list pointer
|
|
307
307
|
*/
|
|
308
|
-
|
|
308
|
+
provide let getFreePtr = () => {
|
|
309
309
|
freePtr
|
|
310
310
|
}
|
package/runtime/malloc.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Malloc
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Values
|
|
6
|
+
|
|
7
|
+
Functions and constants included in the Malloc module.
|
|
8
|
+
|
|
1
9
|
### Malloc.**_RESERVED_RUNTIME_SPACE**
|
|
2
10
|
|
|
3
11
|
```grain
|
|
@@ -7,7 +15,7 @@ _RESERVED_RUNTIME_SPACE : WasmI32
|
|
|
7
15
|
### Malloc.**free**
|
|
8
16
|
|
|
9
17
|
```grain
|
|
10
|
-
free : WasmI32
|
|
18
|
+
free : (ap: WasmI32) => Void
|
|
11
19
|
```
|
|
12
20
|
|
|
13
21
|
Frees the given allocated pointer.
|
|
@@ -21,7 +29,7 @@ Parameters:
|
|
|
21
29
|
### Malloc.**malloc**
|
|
22
30
|
|
|
23
31
|
```grain
|
|
24
|
-
malloc : WasmI32
|
|
32
|
+
malloc : (nbytes: WasmI32) => WasmI32
|
|
25
33
|
```
|
|
26
34
|
|
|
27
35
|
Allocates the requested number of bytes, returning a pointer.
|
|
@@ -41,7 +49,7 @@ Returns:
|
|
|
41
49
|
### Malloc.**getFreePtr**
|
|
42
50
|
|
|
43
51
|
```grain
|
|
44
|
-
getFreePtr : ()
|
|
52
|
+
getFreePtr : () => WasmI32
|
|
45
53
|
```
|
|
46
54
|
|
|
47
55
|
Returns the current free list pointer.
|