aboutsummaryrefslogtreecommitdiff
path: root/src/lib/allocate.s
diff options
context:
space:
mode:
authormithe24 <mithe24@student.sdu.dk>2025-10-08 13:35:41 +0200
committermithe24 <mithe24@student.sdu.dk>2025-10-29 13:49:57 +0100
commitfa03afa46984fbf6eacbd5540e4e318d161cbbe0 (patch)
treecd5e2f567a407ee459d94fcfd8ed3088fce3bc96 /src/lib/allocate.s
parent07816c4c9c257c327bb93a72ed3a9b680816136c (diff)
downloadsorter-fa03afa46984fbf6eacbd5540e4e318d161cbbe0.tar.gz
sorter-fa03afa46984fbf6eacbd5540e4e318d161cbbe0.zip
chore(snippets): move snippetss to src/
Diffstat (limited to 'src/lib/allocate.s')
-rw-r--r--src/lib/allocate.s21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/lib/allocate.s b/src/lib/allocate.s
new file mode 100644
index 0000000..fdd9bf8
--- /dev/null
+++ b/src/lib/allocate.s
@@ -0,0 +1,21 @@
+# void *allocate(int n)
+#
+# A naive memory allocator that simply retrieves some new space from the OS.
+# It is not possible to deallocate the memory again.
+.globl allocate
+.type allocate, @function
+allocate:
+ push %rdi
+ # 1. Find the current end of the data segment.
+ movq $12, %rax # brk
+ xorq %rdi, %rdi # 0 means we retrieve the current end.
+ syscall
+ # 2. Add the amount of memory we want to allocate.
+ pop %rdi # the argument
+ push %rax # current end, which is where the allocated memory will start
+ addq %rax, %rdi # compute the new end
+ movq $12, %rax # brk
+ syscall
+ pop %rax # the old end, which is the address of our allocated memory
+ ret
+