aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
author2025-06-29 19:30:55 +0530
committer2025-06-29 19:30:55 +0530
commitb5234910c90a3bfa7ac842b1e5f6c4f77ebbda58 (patch)
tree1af14f5906e43b2b2090ca31d8220ea2c91e0e56 /src
parent0.7.0 (diff)
downloadsudomsg-b5234910c90a3bfa7ac842b1e5f6c4f77ebbda58.tar
sudomsg-b5234910c90a3bfa7ac842b1e5f6c4f77ebbda58.tar.gz
sudomsg-b5234910c90a3bfa7ac842b1e5f6c4f77ebbda58.tar.bz2
sudomsg-b5234910c90a3bfa7ac842b1e5f6c4f77ebbda58.tar.lz
sudomsg-b5234910c90a3bfa7ac842b1e5f6c4f77ebbda58.tar.xz
sudomsg-b5234910c90a3bfa7ac842b1e5f6c4f77ebbda58.tar.zst
sudomsg-b5234910c90a3bfa7ac842b1e5f6c4f77ebbda58.zip
Improved prism theme
Diffstat (limited to 'src')
-rw-r--r--src/posts/primer-on-memory-management/index.md48
1 files changed, 27 insertions, 21 deletions
diff --git a/src/posts/primer-on-memory-management/index.md b/src/posts/primer-on-memory-management/index.md
index 5e41448..41e79f5 100644
--- a/src/posts/primer-on-memory-management/index.md
+++ b/src/posts/primer-on-memory-management/index.md
@@ -61,6 +61,8 @@ struct data {
};
```
+Which creates an object of the following layout in memory:
+
Offset | Content
-------|-----------------------
0x0000 | char character (1B)
@@ -103,14 +105,14 @@ The assembly output on x86_64:
``` nasm
a:
- push rbp
- mov rbp, rsp
- lea rsi, [rbp + 16]
- lea rdi, [rip + b]
- mov edx, 65536 ;; also this is assignment in x86
- call memcpy@PLT ;; notice the memcpy
- pop rbp
- ret
+ push rbp
+ mov rbp, rsp
+ lea rsi, [rbp + 16]
+ lea rdi, [rip + b]
+ mov edx, 65536 ;; also this is assignment in x86
+ call memcpy@PLT ;; notice the memcpy
+ pop rbp
+ ret
```
## Refer and Point
@@ -251,10 +253,10 @@ with `LW` and `SW` as shown bellow for adding 2 variables.
``` asm
; int c = a + b;
- lw $1, 12($fp) ; load
- lw $2, 8($fp) ; load
- addu $1, $1, $2 ; the actual action
- sw $1, 4($fp) ; store
+ lw $1, 12($fp) ; load
+ lw $2, 8($fp) ; load
+ addu $1, $1, $2 ; the actual action
+ sw $1, 4($fp) ; store
```
Functions in compiled code are invoked via their memory address which is
@@ -302,7 +304,8 @@ neck due to fetching code and data is the same bus (not parallel).
#### Harvard Architecture
-*[JIT]: Just In Time Compilation *[ROM]: Read only Memory
+*[JIT]: Just In Time Compilation
+*[ROM]: Read only Memory
The data and code use different memory bus and therefore different addressing
space. This is common in embedded where the code is in ROM. Self-modifying and
@@ -407,7 +410,7 @@ void dealloc<typename T>(T* old){
}
```
-Observe the dealloc function or method. Its really fast but it frees any thing
+Observe the `dealloc` function or method. Its really fast but it frees any thing
allocated after it. This means that while it's good for local variables, it also
implies that the return variable of a function must be at the top of the stack
after the function returns. This means returning pointer to local variable is
@@ -438,7 +441,7 @@ Example are network sockets or widgets.
[[nodiscard]] char *bar(){
char* a = malloc(10); // request 10 bytes of heap from runtime
if (!a){
- return NULL; // malloc fails discuss later
+ return nullptr; // malloc fails discuss later
}
strcpy(a, "hello");
return a; // Will not free a
@@ -504,11 +507,11 @@ pointers.
### Out of Memory Errors
-Most Programs assume ```malloc``` will not error which is mostly true as
+Most Programs assume `malloc` will not error which is mostly true as
operating systems overcommit memory. But still either due lack of overcommit so
out of memory or other reason (such as internal fragmentation). This must be
-handled (```malloc``` returns null and exception in certain languages). Be very
-careful and never assume ```malloc``` will always return memory.
+handled (`malloc` returns null and exception in certain languages). Be very
+careful and never assume `malloc` will always return memory.
### Lifetime Errors
@@ -623,8 +626,8 @@ A method which I personally use in the case of trees is to create an object pool
(which is similar to arena but only for a single type). This simplifies clean up
of tree when done.
-*[RAII]: Resource acquisition is initialization *[OOP]: Object Oriented
-Programming
+*[RAII]: Resource acquisition is initialization
+*[OOP]: Object Oriented Programming
### RAII
@@ -640,7 +643,10 @@ reference counting.
Similar pattern for defer but this works by inserting code hooks to the end of
scope so you can call cleanup after you are done. This is in Zig.
-``` zig var list = ArrayList(u8).init(allocator); defer list.deinit(); ```
+``` zig
+var list = ArrayList(u8).init(allocator);
+defer list.deinit();
+```
Now this method is not memory management specific and ownership and lifetimes
are technically also for system resources too like files, devices, windows, etc.