From 2e904095e7db075d810948be1b8b968394ea89f2 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 13 Aug 2025 20:58:29 +0200 Subject: [PATCH] Implemented memory allocations --- lib/mem/mem_linux.q | 14 ++++++++++++++ lib/mem/mem_linux_arm.q | 4 ++++ lib/mem/mem_linux_x86.q | 4 ++++ lib/mem/mem_mac.q | 14 ++++++++++++++ lib/mem/mem_mac_arm.q | 4 ++++ lib/mem/mem_mac_x86.q | 4 ++++ lib/mem/mem_unix.q | 13 +++++++++++++ lib/mem/mem_windows.q | 21 +++++++++++++++++++++ tests/memory.q | 8 ++++++++ tests/out-of-memory.q | 6 ++++++ tests/tests_test.go | 2 ++ 11 files changed, 94 insertions(+) create mode 100644 lib/mem/mem_linux.q create mode 100644 lib/mem/mem_linux_arm.q create mode 100644 lib/mem/mem_linux_x86.q create mode 100644 lib/mem/mem_mac.q create mode 100644 lib/mem/mem_mac_arm.q create mode 100644 lib/mem/mem_mac_x86.q create mode 100644 lib/mem/mem_unix.q create mode 100644 lib/mem/mem_windows.q create mode 100644 tests/memory.q create mode 100644 tests/out-of-memory.q diff --git a/lib/mem/mem_linux.q b/lib/mem/mem_linux.q new file mode 100644 index 0000000..ca095f2 --- /dev/null +++ b/lib/mem/mem_linux.q @@ -0,0 +1,14 @@ +mmap(address int, length uint, protection int, flags int) -> *any { + return syscall(_mmap, address, length, protection, flags) +} + +munmap(address *any, length uint) -> int { + return syscall(_munmap, address, length) +} + +const { + read 0x1 + write 0x2 + private 0x02 + anonymous 0x20 +} \ No newline at end of file diff --git a/lib/mem/mem_linux_arm.q b/lib/mem/mem_linux_arm.q new file mode 100644 index 0000000..233a01a --- /dev/null +++ b/lib/mem/mem_linux_arm.q @@ -0,0 +1,4 @@ +const { + _mmap 222 + _munmap 215 +} \ No newline at end of file diff --git a/lib/mem/mem_linux_x86.q b/lib/mem/mem_linux_x86.q new file mode 100644 index 0000000..9c4a2a3 --- /dev/null +++ b/lib/mem/mem_linux_x86.q @@ -0,0 +1,4 @@ +const { + _mmap 9 + _munmap 11 +} \ No newline at end of file diff --git a/lib/mem/mem_mac.q b/lib/mem/mem_mac.q new file mode 100644 index 0000000..6fce1a7 --- /dev/null +++ b/lib/mem/mem_mac.q @@ -0,0 +1,14 @@ +mmap(address int, length uint, protection int, flags int) -> *any { + return syscall(_mmap, address, length, protection, flags, 0, 0) +} + +munmap(address *any, length uint) -> int { + return syscall(_munmap, address, length) +} + +const { + read 0x1 + write 0x2 + private 0x02 + anonymous 0x1000 +} \ No newline at end of file diff --git a/lib/mem/mem_mac_arm.q b/lib/mem/mem_mac_arm.q new file mode 100644 index 0000000..fcb8159 --- /dev/null +++ b/lib/mem/mem_mac_arm.q @@ -0,0 +1,4 @@ +const { + _mmap 0xC5 + _munmap 0x49 +} \ No newline at end of file diff --git a/lib/mem/mem_mac_x86.q b/lib/mem/mem_mac_x86.q new file mode 100644 index 0000000..53bdcf4 --- /dev/null +++ b/lib/mem/mem_mac_x86.q @@ -0,0 +1,4 @@ +const { + _mmap 0x20000C5 + _munmap 0x2000049 +} \ No newline at end of file diff --git a/lib/mem/mem_unix.q b/lib/mem/mem_unix.q new file mode 100644 index 0000000..00fd01d --- /dev/null +++ b/lib/mem/mem_unix.q @@ -0,0 +1,13 @@ +alloc(length int) -> (address *any) { + x := mmap(0, length, read|write, private|anonymous) + + if x < 0x1000 { + return 0 + } + + return x +} + +free(address *any, length int) { + munmap(address, length) +} \ No newline at end of file diff --git a/lib/mem/mem_windows.q b/lib/mem/mem_windows.q new file mode 100644 index 0000000..8d0eec2 --- /dev/null +++ b/lib/mem/mem_windows.q @@ -0,0 +1,21 @@ +alloc(length int) -> (address *any) { + return kernel32.VirtualAlloc(0, length, commit|reserve, readwrite) +} + +free(address *any, length int) { + kernel32.VirtualFree(address, length, decommit) +} + +const { + readwrite 0x0004 + commit 0x1000 + reserve 0x2000 + decommit 0x4000 +} + +extern { + kernel32 { + VirtualAlloc(address int, size uint, flags uint32, protection uint32) -> (address *any) + VirtualFree(address *any, size uint, type uint32) -> (success bool) + } +} \ No newline at end of file diff --git a/tests/memory.q b/tests/memory.q new file mode 100644 index 0000000..ebbb483 --- /dev/null +++ b/tests/memory.q @@ -0,0 +1,8 @@ +import mem + +main() { + length := 1024 + address := mem.alloc(length) + assert address != 0 + mem.free(address, length) +} \ No newline at end of file diff --git a/tests/out-of-memory.q b/tests/out-of-memory.q new file mode 100644 index 0000000..8559a3f --- /dev/null +++ b/tests/out-of-memory.q @@ -0,0 +1,6 @@ +import mem + +main() { + address := mem.alloc(0x4000000000000) + assert address == 0 +} \ No newline at end of file diff --git a/tests/tests_test.go b/tests/tests_test.go index 08f1b4c..7e10540 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -50,6 +50,8 @@ var tests = []run{ {"loop-limit", "", "", 0}, {"loop-limit-dynamic", "", "", 0}, {"loop-keepalive", "", "", 0}, + {"memory", "", "", 0}, + {"out-of-memory", "", "", 0}, {"hello", "", "Hello\nHello\nHello\n", 0}, {"script", "", "Hello\n", 0}, } -- 2.51.2