summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhyang <hyang@hyang.xyz>2024-01-06 23:09:34 -0800
committerhyang <hyang@hyang.xyz>2024-01-06 23:09:34 -0800
commit4535ecd8944df8af03c67d07249695de051dc505 (patch)
tree43b6f9e194724408a64c57f903873b86558afcba
parenta30c02a727cc62de36e9a061afc8fcc3704c4ff1 (diff)
fix
-rw-r--r--content/posts/malloc/index.md5
1 files changed, 3 insertions, 2 deletions
diff --git a/content/posts/malloc/index.md b/content/posts/malloc/index.md
index 874ee4a..d38b248 100644
--- a/content/posts/malloc/index.md
+++ b/content/posts/malloc/index.md
@@ -3,14 +3,15 @@ title: "The C Memory Allocator"
date: 2023-12-21T14:34:30-08:00
draft: false
description: C dynamic memory management from scratch
+ShowToc: true
---
In C, dynamic memory is manually managed, meaning that it is up to the programmer to properly allocate memory, and freeing it when done. Memory is allocated using `malloc()` function, and freed with `free()`. You can read more about it [here](https://linux.die.net/man/3/malloc). The API is pretty simple, and there's not much to talk about on the surface. Let's see how it is implemented.
## Goals of malloc()
-In most operating systems today, everytime you run a program, the kernel abstracts away the memory the program uses into virtual memory. The virtual memory is completely separate from the physical memory, meaning it has its own memory addresses, size, etc. Every program has a fixed amount of memory when it starts, and has to call upon the kernel to request more memory. The problem is, every operating system has its own way of requesting memory from the kernel. The primary goal of malloc and free API abstracting away all of this away from the programmer.
+In most operating systems today, everytime you run a program, the kernel abstracts away the memory the program uses into virtual memory. The [virtual memory](https://en.wikipedia.org/wiki/Virtual_memory) is completely separate from the physical memory, meaning it has its own memory addresses, size, etc. Every program has a fixed amount of memory when it starts, and has to [call](https://en.wikipedia.org/wiki/System_call) upon the kernel to request more memory. The problem is, every operating system has its own way of requesting memory from the kernel. The primary goal of malloc and free API abstracting away all of this away from the programmer.
-Another goal of malloc is performance. System calls are expensive, and so a common goal among all allocators is reducing the amount of system calls as much as possible. Usually, this is achieved by requesting huge chunks of memory from kernel to be shared with subsequent malloc calls, and recycling freed memory. This however, introduces its own set of problems, such as [fragmentation](https://en.wikipedia.org/wiki/Fragmentation_(computing)).
+Another goal of malloc is performance. System calls are expensive, and so a common goal among all allocators is reducing the amount of requests for memory as much as possible. Usually, this is achieved by requesting huge chunks of memory from kernel to be shared with subsequent malloc calls, and recycling freed memory. This however, introduces its own set of problems, such as [fragmentation](https://en.wikipedia.org/wiki/Fragmentation_(computing)). We'll have to account for that as well.
## Implementing malloc() and free()