diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-17 05:30:57 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-17 05:30:57 +0000 |
| commit | 4dd85fd6ecd9a67e5c2b68380873e6e114714355 (patch) | |
| tree | b9dad1d0845f4c2ae7e6efed171fbee40ccd1f31 /tests/i/memory-allocation/program.cps | |
| parent | e782898e420c7f2abd83b97c160194d0e70598dd (diff) | |
feat(memory): add tracked MALLOC and FREE builtins
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'tests/i/memory-allocation/program.cps')
| -rw-r--r-- | tests/i/memory-allocation/program.cps | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/i/memory-allocation/program.cps b/tests/i/memory-allocation/program.cps new file mode 100644 index 0000000..85a24dc --- /dev/null +++ b/tests/i/memory-allocation/program.cps @@ -0,0 +1,63 @@ +TYPE Raw = C_TYPE("void*") +TYPE IntPointer = ^INTEGER +TYPE Pair + DECLARE First : INTEGER + DECLARE Second : INTEGER +ENDTYPE +TYPE PairPointer = ^Pair +DECLARE Empty : Raw +DECLARE P : Raw +DECLARE Q : Raw +DECLARE R : Raw +DECLARE Number : INTEGER +DECLARE Pointer : IntPointer +DECLARE PairAddress : PairPointer +DECLARE Result : Raw +OUTPUT MALLOC(0) = Empty +OUTPUT FREE(Empty) = Empty +P <- malloc(8) +Pointer <- (IntPointer)P +Pointer^ <- 42 +OUTPUT Pointer^ +Q <- MALLOC((C_INT)16) +PairAddress <- (PairPointer)Q +PairAddress^.First <- 11 +PairAddress^.Second <- 22 +OUTPUT PairAddress^.First, ":", PairAddress^.Second +R <- MALLOC(1) +// Remove a middle node, then the head, then the tail. +Result <- FREE(Q) +OUTPUT Result = Q +FREE(R) +FREE(P) +TRY + FREE(P) +CATCH + OUTPUT "double free caught" +ENDTRY +Pointer <- ^Number +TRY + FREE((Raw)Pointer) +CATCH + OUTPUT "stack address caught" +ENDTRY +Q <- MALLOC(16) +PairAddress <- (PairPointer)Q +Pointer <- ^PairAddress^.Second +TRY + FREE((Raw)Pointer) +CATCH + OUTPUT "interior address caught" +ENDTRY +FREE(Q) +TRY + MALLOC(-1) +CATCH + OUTPUT "allocation failure caught" +ENDTRY +// Allocation still works after all caught failures. +P <- MALLOC(8) +Pointer <- (IntPointer)P +Pointer^ <- 99 +OUTPUT Pointer^ +FREE(P) |
