@grain/stdlib 0.6.3 → 0.6.5

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/map.gr CHANGED
@@ -436,13 +436,14 @@ provide let toArray = (map: Map<a, b>) => {
436
436
  use WasmI32.{ (+) as addWasmI32 }
437
437
  // Assign the values into the array.
438
438
  // We store them directly to prevent GC on uninitialized array data.
439
- let array = WasmI32.fromGrain(array)
439
+ let arrayPtr = WasmI32.fromGrain(array)
440
440
  let item = (key, value)
441
441
  WasmI32.store(
442
- addWasmI32(array, untagSimpleNumber(i) * 4n),
442
+ addWasmI32(arrayPtr, untagSimpleNumber(i) * 4n),
443
443
  Memory.incRef(WasmI32.fromGrain(item)),
444
444
  8n
445
445
  )
446
+ ignore(array)
446
447
  i + 1
447
448
  }
448
449
  reduce(reducer, 0, map)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grain/stdlib",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "The standard library for the Grain language.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://grain-lang.org",
package/pervasives.md CHANGED
@@ -855,9 +855,16 @@ Returns:
855
855
 
856
856
  ### Pervasives.**print**
857
857
 
858
- <details disabled>
859
- <summary tabindex="-1">Added in <code>0.1.0</code></summary>
860
- No other changes yet.
858
+ <details>
859
+ <summary>Added in <code>0.1.0</code></summary>
860
+ <table>
861
+ <thead>
862
+ <tr><th>version</th><th>changes</th></tr>
863
+ </thead>
864
+ <tbody>
865
+ <tr><td><code>0.6.0</code></td><td>Added support for custom suffixes</td></tr>
866
+ </tbody>
867
+ </table>
861
868
  </details>
862
869
 
863
870
  ```grain
package/runtime/malloc.gr CHANGED
@@ -3,8 +3,6 @@ module Malloc
3
3
 
4
4
  /*
5
5
  * This module implements a generic memory allocator.
6
- * The algorithm is quite simple, being based on the memory allocator
7
- * from pages 185-188 of K&R C (2nd edition).
8
6
  */
9
7
 
10
8
  from "runtime/unsafe/wasmi32" include WasmI32
@@ -20,6 +18,8 @@ use WasmI32.{
20
18
  (>>>),
21
19
  (==),
22
20
  (!=),
21
+ (&),
22
+ (^),
23
23
  }
24
24
  from "runtime/exception" include Exception
25
25
 
@@ -32,44 +32,64 @@ primitive (||) = "@or"
32
32
 
33
33
  primitive heapStart = "@heap.start"
34
34
 
35
- /* UNDERSTANDING THE STRUCTURE OF THE FREE LIST
36
- * The original K&R definition for the free list entry type was the following:
35
+ /* UNDERSTANDING THE STRUCTURE OF THE FREE LISTS
37
36
  *
38
- * union header {
39
- * struct {
40
- * union header *ptr;
41
- * unsigned size;
42
- * } s;
43
- * long x; // <- forces 8-byte alignment
44
- * };
37
+ * `malloc` allocates memory and `free` releases this memory. Two separate free
38
+ * lists are maintained, one for small blocks of 64 bytes, and one for larger
39
+ * blocks of multiples of 64 bytes. Each block has an 8-byte header and 8-byte
40
+ * footer to keep track of block sizes and maintain the free list.
41
+ *
42
+ * Most allocations in programs are small, so the separate free lists allow us
43
+ * to implement `malloc` and `free` in O(1) for small allocations and O(n)
44
+ * `malloc` and O(1) `free` for large allocations, where `n` is the size of the
45
+ * free list for large blocks.
46
+ *
47
+ * The small blocks are able to service:
48
+ * - Numbers (with the exception of large BigInts/Rationals)
49
+ * - Tuples/Arrays up to 8 elements
50
+ * - Records up to 6 elements
51
+ * - Variants up to 5 elements
52
+ * - Closures up to 6 elements
53
+ * - Bytes/Strings up to length 32
54
+ *
55
+ * Blocks in memory look like this:
45
56
  *
46
- * In memory, this is really just two ints (assuming we're working in 32-bit mode).
47
- * As such, we manually lay out the entries on the heap as follows (note that we
48
- * use helpers to facilitate accessing and setting these values):
57
+ * 8 bytes 8 bytes 64n - 16 bytes 8 bytes 8 bytes
58
+ * ┌─────────────────────┬────────────────┬─────────────────┬────────────────┬─────────────────────┐
59
+ * <prev block footer> <block header> <block content> │ <block footer> │ <next block header> │
60
+ * └─────────────────────┴────────────────┴─────────────────┴────────────────┴─────────────────────┘
49
61
  *
50
- * Grain C Equivalent
51
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52
- * let ptr === union header *ptr
53
- * getNext(ptr) === ptr->s.ptr
54
- * getSize(ptr) === ptr->s.size
62
+ * Block headers look like this:
63
+ * ┌───────────────────────┬──────────────┐
64
+ * │ <prev free block ptr> <block size> │
65
+ * └───────────────────────┴──────────────┘
66
+ *
67
+ * Block footers look like this:
68
+ * ┌───────────────────────┬──────────────┐
69
+ * │ <next free block ptr> │ <block size> │
70
+ * └───────────────────────┴──────────────┘
71
+ *
72
+ * The size is kept in the header and footer to allow us to quickly combine
73
+ * free blocks when blocks are freed.
74
+ *
75
+ * Pointers to the previous/next free blocks give us doubly-linked free lists,
76
+ * which makes it possible to remove blocks from the free list in constant
77
+ * time.
78
+ *
79
+ * A block is considered in use when the previous/next pointers are both zero.
55
80
  */
56
81
 
57
82
  /**
58
- * Pointer to the start of the free list. This is always a multiple of
83
+ * Pointers to the start of the free lists. This is always a multiple of
59
84
  * 8, with the exception of its initial value (used as a sentinel).
60
85
  */
61
- let mut freePtr = 1n
86
+ let mut smallBlockFreePtr = 1n
87
+ let mut largeBlockFreePtr = 1n
62
88
 
63
89
  /**
64
90
  * Size (in bytes) of entries in the free list.
65
91
  */
66
- let mallocHeaderSize = 8n
67
-
68
- /**
69
- * log_2(mallocHeaderSize) (multiplication by the header
70
- * size is equivalent to left-shifting by this amount)
71
- */
72
- let logMallocHeaderSize = 3n
92
+ let _HEADER_FOOTER_SIZE = 8n
73
93
 
74
94
  /**
75
95
  * The current size (in bytes) of the heap.
@@ -87,9 +107,9 @@ let _BASE = heapStart() + _RESERVED_RUNTIME_SPACE
87
107
  /**
88
108
  * The start pointer of the heap.
89
109
  */
90
- let _HEAP_START = _BASE + mallocHeaderSize
110
+ let _HEAP_START = _BASE + _HEADER_FOOTER_SIZE
91
111
 
92
- let _NEXT_OFFSET = 0n
112
+ let _PREV_NEXT_OFFSET = 0n
93
113
  let _SIZE_OFFSET = 4n
94
114
 
95
115
  /**
@@ -97,33 +117,65 @@ let _SIZE_OFFSET = 4n
97
117
  */
98
118
  let _PAGE_SIZE = 65536n
99
119
 
100
- let getNext = (ptr: WasmI32) => {
101
- WasmI32.load(ptr, _NEXT_OFFSET)
120
+ /**
121
+ * Size (in bytes) of blocks allocated by the allocator
122
+ */
123
+ let _UNIT_SIZE = 64n
124
+
125
+ /**
126
+ * log_2(_UNIT_SIZE) (multiplication by the header
127
+ * size is equivalent to left-shifting by this amount)
128
+ */
129
+ let logUnitSize = 6n
130
+
131
+ let headerGetPrevious = (headerPtr: WasmI32) => {
132
+ WasmI32.load(headerPtr, _PREV_NEXT_OFFSET)
133
+ }
134
+
135
+ let headerSetPrevious = (headerPtr: WasmI32, val: WasmI32) => {
136
+ WasmI32.store(headerPtr, val, _PREV_NEXT_OFFSET)
137
+ }
138
+
139
+ let headerGetSize = (headerPtr: WasmI32) => {
140
+ WasmI32.load(headerPtr, _SIZE_OFFSET)
141
+ }
142
+
143
+ let headerSetSize = (headerPtr: WasmI32, val: WasmI32) => {
144
+ WasmI32.store(headerPtr, val, _SIZE_OFFSET)
102
145
  }
103
146
 
104
- let setNext = (ptr: WasmI32, val: WasmI32) => {
105
- WasmI32.store(ptr, val, _NEXT_OFFSET)
147
+ // These functions are no different than the ones above, but exist to make the
148
+ // code much easier to follow
149
+
150
+ let footerGetNext = (footerPtr: WasmI32) => {
151
+ WasmI32.load(footerPtr, _PREV_NEXT_OFFSET)
152
+ }
153
+
154
+ let footerSetNext = (footerPtr: WasmI32, val: WasmI32) => {
155
+ WasmI32.store(footerPtr, val, _PREV_NEXT_OFFSET)
106
156
  }
107
157
 
108
- let getSize = (ptr: WasmI32) => {
109
- WasmI32.load(ptr, _SIZE_OFFSET)
158
+ let footerGetSize = (footerPtr: WasmI32) => {
159
+ WasmI32.load(footerPtr, _SIZE_OFFSET)
110
160
  }
111
161
 
112
- let setSize = (ptr: WasmI32, val: WasmI32) => {
113
- WasmI32.store(ptr, val, _SIZE_OFFSET)
162
+ let footerSetSize = (footerPtr: WasmI32, val: WasmI32) => {
163
+ WasmI32.store(footerPtr, val, _SIZE_OFFSET)
114
164
  }
115
165
 
116
166
  /**
117
- * Requests that the heap be grown by the given number of bytes.
167
+ * Requests that the heap be grown by the given number of units.
118
168
  *
119
- * @param nbytes: The number of bytes requested
169
+ * @param nunits: The number of units requested
120
170
  * @returns The pointer to the beginning of the extended region if successful or -1 otherwise
121
171
  */
122
- let growHeap = (nbytes: WasmI32) => {
172
+ let growHeap = (nunits: WasmI32) => {
123
173
  let mut reqSize = 0n
124
174
  let mut reqResult = 0n
125
175
  let mut origSize = heapSize
126
176
 
177
+ let nbytes = nunits << logUnitSize
178
+
127
179
  // If the size has not been initialized, do so.
128
180
  if (heapSize == 0n) {
129
181
  heapSize = memorySize() * _PAGE_SIZE - _HEAP_START
@@ -131,8 +183,7 @@ let growHeap = (nbytes: WasmI32) => {
131
183
  // More bytes requested than the initial heap size,
132
184
  // so we need to request more anyway.
133
185
  reqSize = nbytes - heapSize
134
- reqSize = reqSize >>> 16n
135
- reqSize += 1n
186
+ reqSize = (reqSize + _PAGE_SIZE - 1n) >>> 16n
136
187
  reqResult = memoryGrow(reqSize)
137
188
  if (reqResult == -1n) {
138
189
  -1n
@@ -161,49 +212,105 @@ let growHeap = (nbytes: WasmI32) => {
161
212
  }
162
213
  }
163
214
 
215
+ let removeFromFreeList = (blockPtr: WasmI32) => {
216
+ let blockSize = headerGetSize(blockPtr)
217
+ let blockFooterPtr = blockPtr + blockSize * _UNIT_SIZE - _HEADER_FOOTER_SIZE
218
+ let nextPtr = footerGetNext(blockFooterPtr)
219
+
220
+ let prevPtr = headerGetPrevious(blockPtr)
221
+ if (prevPtr == 1n) {
222
+ // this block was the start of the free list
223
+ if (blockSize == 1n) {
224
+ smallBlockFreePtr = nextPtr
225
+ } else {
226
+ largeBlockFreePtr = nextPtr
227
+ }
228
+
229
+ headerSetPrevious(nextPtr, prevPtr)
230
+ } else {
231
+ let prevSize = headerGetSize(prevPtr)
232
+ let prevFooterPtr = prevPtr + prevSize * _UNIT_SIZE - _HEADER_FOOTER_SIZE
233
+ footerSetNext(prevFooterPtr, nextPtr)
234
+ headerSetPrevious(nextPtr, prevPtr)
235
+ }
236
+ }
237
+
164
238
  /**
165
239
  * Frees the given allocated pointer.
166
240
  *
167
241
  * @param ap: The pointer to free
168
242
  */
169
243
  provide let free = (ap: WasmI32) => {
170
- let mut blockPtr = ap - 8n // 8 bytes for malloc header
171
- let mut p = freePtr
172
-
173
- // Edge case: for the first free (called by morecore), the free pointer
174
- // is actually already pointing to this node, so we don't do anything.
175
- if (blockPtr != freePtr) {
176
- // Find the location to insert this block into the free list
177
- while (true) {
178
- let nextp = getNext(p)
179
- if (
180
- blockPtr > p && blockPtr < nextp ||
181
- p >= nextp && (blockPtr > p || blockPtr < nextp)
182
- ) {
183
- break
184
- }
185
- p = nextp
244
+ let mut blockPtr = ap - _HEADER_FOOTER_SIZE
245
+ let mut blockSize = headerGetSize(blockPtr)
246
+
247
+ let nextBlockPtr = blockPtr + blockSize * _UNIT_SIZE
248
+ if (headerGetPrevious(nextBlockPtr) > 0n) {
249
+ // adjacent block is free, so merge
250
+ removeFromFreeList(nextBlockPtr)
251
+
252
+ let nextBlockSize = headerGetSize(nextBlockPtr)
253
+ blockSize += nextBlockSize
254
+ headerSetSize(blockPtr, blockSize)
255
+
256
+ let footerPtr = blockPtr + blockSize * _UNIT_SIZE - _HEADER_FOOTER_SIZE
257
+ footerSetSize(footerPtr, blockSize)
258
+ }
259
+
260
+ let prevBlockFooterPtr = blockPtr - _HEADER_FOOTER_SIZE
261
+ if (footerGetNext(prevBlockFooterPtr) > 0n) {
262
+ // (prev) adjacent block is free, so merge
263
+ let prevBlockSize = footerGetSize(prevBlockFooterPtr)
264
+ let prevBlockPtr = blockPtr - prevBlockSize * _UNIT_SIZE
265
+
266
+ if (prevBlockSize == 1n) {
267
+ // Since we merged, this block is already a part of the free list. If
268
+ // the old block was size 1, it needs to be switched to the large list.
269
+ removeFromFreeList(prevBlockPtr)
186
270
  }
187
271
 
188
- // Merge the block into the adjacent free list entry above, if needed
189
- let blockPtrSize = getSize(blockPtr)
190
- let next = getNext(p)
191
- if (blockPtr + blockPtrSize == next) {
192
- setSize(blockPtr, blockPtrSize + getSize(next))
193
- setNext(blockPtr, getNext(next))
194
- } else {
195
- setNext(blockPtr, next)
272
+ blockPtr = prevBlockPtr
273
+
274
+ blockSize += prevBlockSize
275
+ headerSetSize(blockPtr, blockSize)
276
+
277
+ let footerPtr = blockPtr + blockSize * _UNIT_SIZE - _HEADER_FOOTER_SIZE
278
+ footerSetSize(footerPtr, blockSize)
279
+ footerSetNext(footerPtr, footerGetNext(prevBlockFooterPtr))
280
+
281
+ if (prevBlockSize == 1n) {
282
+ if (largeBlockFreePtr != 1n) {
283
+ headerSetPrevious(largeBlockFreePtr, blockPtr)
284
+ }
285
+
286
+ let footerPtr = blockPtr + blockSize * _UNIT_SIZE - _HEADER_FOOTER_SIZE
287
+ footerSetNext(footerPtr, largeBlockFreePtr)
288
+ headerSetPrevious(blockPtr, 1n)
289
+
290
+ largeBlockFreePtr = blockPtr
196
291
  }
197
- // Merge the previous (adjacent) free list entry into this block, if needed
198
- let pSize = getSize(p)
199
- if (p + pSize == blockPtr) {
200
- setSize(p, pSize + getSize(blockPtr))
201
- setNext(p, getNext(blockPtr))
292
+ } else {
293
+ if (blockSize == 1n) {
294
+ if (smallBlockFreePtr != 1n) {
295
+ headerSetPrevious(smallBlockFreePtr, blockPtr)
296
+ }
297
+
298
+ let footerPtr = blockPtr + _UNIT_SIZE - _HEADER_FOOTER_SIZE
299
+ footerSetNext(footerPtr, smallBlockFreePtr)
300
+ headerSetPrevious(blockPtr, 1n)
301
+
302
+ smallBlockFreePtr = blockPtr
202
303
  } else {
203
- setNext(p, blockPtr)
304
+ if (largeBlockFreePtr != 1n) {
305
+ headerSetPrevious(largeBlockFreePtr, blockPtr)
306
+ }
307
+
308
+ let footerPtr = blockPtr + blockSize * _UNIT_SIZE - _HEADER_FOOTER_SIZE
309
+ footerSetNext(footerPtr, largeBlockFreePtr)
310
+ headerSetPrevious(blockPtr, 1n)
311
+
312
+ largeBlockFreePtr = blockPtr
204
313
  }
205
- // Set the free list head to this block
206
- freePtr = p
207
314
  }
208
315
  }
209
316
 
@@ -215,25 +322,48 @@ provide let free = (ap: WasmI32) => {
215
322
  * @param nbytes: The number of bytes to try to grow the heap by
216
323
  * @returns A pointer to the start of the free list if successful or -1 otherwise
217
324
  */
218
- let morecore = (nbytes: WasmI32) => {
325
+ let morecore = (nunits: WasmI32) => {
219
326
  let origSize = heapSize
220
- let mut cp = growHeap(nbytes)
327
+
328
+ let cp = growHeap(nunits + 1n) // include an extra unit for 4 headers/footers
221
329
 
222
330
  // If there was an error, fail
223
331
  if (cp == -1n) {
224
332
  Exception.panic("OutOfMemory: Maximum memory size exceeded")
225
333
  } else {
226
- // Set the size of the new block to the amount the
227
- // heap was grown.
334
+ // Set up the block. We'll add dummy headers/footers before and after the
335
+ // block to avoid unnecessary bounds checks elsewhere in the code.
228
336
  let grownAmount = heapSize - origSize
229
- setSize(cp, grownAmount)
337
+ let units = (grownAmount >>> logUnitSize) - 1n
338
+
339
+ let dummyFooter = cp
340
+ footerSetSize(dummyFooter, 0n)
341
+ footerSetNext(dummyFooter, 0n)
342
+
343
+ let blockHeader = dummyFooter + _HEADER_FOOTER_SIZE
344
+ headerSetSize(blockHeader, units)
345
+ headerSetPrevious(blockHeader, 0n)
346
+
347
+ let blockFooter = blockHeader + units * _UNIT_SIZE - _HEADER_FOOTER_SIZE
348
+ footerSetSize(blockFooter, units)
349
+ footerSetNext(blockFooter, 0n)
350
+
351
+ let dummyHeader = blockFooter + _HEADER_FOOTER_SIZE
352
+ headerSetSize(dummyHeader, 0n)
353
+ headerSetPrevious(dummyHeader, 0n)
354
+
230
355
  // Call free() with the new block to add it to the free list.
231
- free(cp + 8n)
356
+ free(blockHeader + _HEADER_FOOTER_SIZE)
357
+
232
358
  // Return the free list pointer.
233
- freePtr
359
+ largeBlockFreePtr
234
360
  }
235
361
  }
236
362
 
363
+ let roundBytesToUnits = bytes => {
364
+ (bytes + _UNIT_SIZE - 1n) >>> logUnitSize
365
+ }
366
+
237
367
  /**
238
368
  * Allocates the requested number of bytes, returning a pointer.
239
369
  *
@@ -241,70 +371,74 @@ let morecore = (nbytes: WasmI32) => {
241
371
  * @returns The pointer to the allocated region (8-byte aligned) or -1 if the allocation failed
242
372
  */
243
373
  provide let malloc = (nbytes: WasmI32) => {
244
- let mut nbytes = nbytes
245
- let mut prevp = freePtr
374
+ let mut nunits = roundBytesToUnits(nbytes + _HEADER_FOOTER_SIZE * 2n)
246
375
 
247
- // Set nbytes to the next multiple of mallocHeaderSize greater
248
- // than the given size
249
- let mut nunits = (nbytes + mallocHeaderSize - 1n) / mallocHeaderSize + 1n
250
- nbytes = nunits << logMallocHeaderSize // multiply by header size
376
+ // Fast path for small blocks
377
+ if (nunits == 1n && smallBlockFreePtr != 1n) {
378
+ let blockPtr = smallBlockFreePtr
379
+ headerSetPrevious(blockPtr, 0n)
380
+ let footer = blockPtr + _UNIT_SIZE - _HEADER_FOOTER_SIZE
381
+ let next = footerGetNext(footer)
382
+ footerSetNext(footer, 0n)
251
383
 
252
- // Handle initialization
253
- if (heapSize == 0n) {
254
- WasmI32.store(_BASE, _BASE, _NEXT_OFFSET)
255
- freePtr = _BASE
256
- prevp = _BASE
257
- WasmI32.store(_BASE, 0n, _SIZE_OFFSET)
384
+ headerSetPrevious(next, 1n)
385
+ smallBlockFreePtr = next
386
+
387
+ return blockPtr + _HEADER_FOOTER_SIZE
258
388
  }
259
389
 
260
- let mut ret = -1n
261
-
262
- // Search the freelist for any blocks large enough.
263
- for (let mut p = getNext(prevp);; {
264
- prevp = p
265
- p = getNext(p)
266
- }) {
267
- let size = getSize(p)
268
- if (size >= nbytes) {
269
- // If this block is big enough, allocate from it.
270
- if (size == nbytes) {
271
- // It's exactly the right size!
272
- setNext(prevp, getNext(p))
273
- } else {
274
- // Shrink it as needed
275
- let newSize = size - nbytes
276
- setSize(p, newSize)
277
- p += newSize
278
- setSize(p, nbytes)
279
- }
280
- // Update the pointer to the free list.
281
- freePtr = prevp
390
+ // Find a large enough block
391
+ let mut freeBlockPtr = largeBlockFreePtr
392
+ while (true) {
393
+ // Free list is empty; grow the heap
394
+ if (freeBlockPtr == 1n) {
395
+ freeBlockPtr = morecore(nunits)
396
+ }
397
+
398
+ let blockSize = headerGetSize(freeBlockPtr)
399
+ let footerPtr = freeBlockPtr + blockSize * _UNIT_SIZE - _HEADER_FOOTER_SIZE
282
400
 
283
- // Return region past the header
284
- ret = p + 8n
285
- break
401
+ // Perfectly sized block, or one unit larger to avoid leaving size 1 blocks
402
+ // in the large block free list
403
+ if (blockSize == nunits || blockSize == nunits + 1n) {
404
+ let blockPtr = freeBlockPtr
405
+
406
+ removeFromFreeList(blockPtr)
407
+ headerSetPrevious(blockPtr, 0n)
408
+ footerSetNext(footerPtr, 0n)
409
+
410
+ return blockPtr + _HEADER_FOOTER_SIZE
286
411
  }
287
412
 
288
- // We've reached the end of the free list. Time to grow the heap.
289
- if (p == freePtr) {
290
- // Attempt to grow the heap
291
- p = morecore(nbytes)
292
- // If growing the heap failed, return -1.
293
- if (p == -1n) {
294
- ret = -1n
295
- break
296
- }
413
+ // Take a chunk of this larger block
414
+ if (blockSize > nunits) {
415
+ let blockPtr = freeBlockPtr
416
+
417
+ let newSize = blockSize - nunits
418
+ headerSetSize(blockPtr, newSize)
419
+ let newFooterPtr = blockPtr + newSize * _UNIT_SIZE - _HEADER_FOOTER_SIZE
420
+ footerSetSize(newFooterPtr, newSize)
421
+ footerSetNext(newFooterPtr, footerGetNext(footerPtr))
422
+
423
+ let newBlockPtr = newFooterPtr + _HEADER_FOOTER_SIZE
424
+ headerSetSize(newBlockPtr, nunits)
425
+ headerSetPrevious(newBlockPtr, 0n)
426
+ footerSetSize(footerPtr, nunits)
427
+ footerSetNext(footerPtr, 0n)
428
+
429
+ return newBlockPtr + _HEADER_FOOTER_SIZE
297
430
  }
431
+
432
+ freeBlockPtr = footerGetNext(footerPtr)
298
433
  }
299
- ret
434
+
435
+ return -1n
300
436
  }
301
437
 
302
438
  /**
303
- * Returns the current free list pointer.
304
- * Used for debugging.
305
- *
306
- * @returns The free list pointer
439
+ * Leaks all memory in all free lists; used for testing.
307
440
  */
308
- provide let getFreePtr = () => {
309
- freePtr
441
+ provide let leakAll = () => {
442
+ smallBlockFreePtr = 1n
443
+ largeBlockFreePtr = 1n
310
444
  }
package/runtime/malloc.md CHANGED
@@ -46,18 +46,11 @@ Returns:
46
46
  |----|-----------|
47
47
  |`WasmI32`|The pointer to the allocated region (8-byte aligned) or -1 if the allocation failed|
48
48
 
49
- ### Malloc.**getFreePtr**
49
+ ### Malloc.**leakAll**
50
50
 
51
51
  ```grain
52
- getFreePtr : () => WasmI32
52
+ leakAll : () => Void
53
53
  ```
54
54
 
55
- Returns the current free list pointer.
56
- Used for debugging.
57
-
58
- Returns:
59
-
60
- |type|description|
61
- |----|-----------|
62
- |`WasmI32`|The free list pointer|
55
+ Leaks all memory in all free lists; used for testing.
63
56