Skip to content

Commit

Permalink
Finish documenting memswap() and implement it
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Dec 11, 2024
1 parent a80ae0d commit b8add1c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion stdlib/mem.jou
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ declare memcpy(dest: void*, source: void*, size: long) -> void* # copy memory,
declare memmove(dest: void*, source: void*, size: long) -> void* # copy memory, overlaps are ok

# Swaps the contents of two memory regions of the same size.
# This does nothing if the memory regions
# This does nothing if the same memory region is passed twice.
# This may not work correctly if two different but overlapping regions are given.
def memswap(a: void*, b: void*, size: long) -> None:
a_bytes: byte* = a
b_bytes: byte* = b

for i = 0L; i < size; i++:
old_a = a_bytes[i]
a_bytes[i] = b_bytes[i]
b_bytes[i] = old_a
15 changes: 15 additions & 0 deletions tests/should_succeed/memlibtest.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import "stdlib/mem.jou"
import "stdlib/io.jou"


def main() -> int:
a = 123
b = 456

memswap(&a, &b, sizeof(a))
printf("%d %d\n", a, b) # Output: 456 123

memswap(&a, &a, sizeof(a)) # does nothing
printf("%d %d\n", a, b) # Output: 456 123

return 0

0 comments on commit b8add1c

Please sign in to comment.