Chapter 05:Memory Management
===
:::info
記憶體管理就是將記憶體抽象化的技術,會使用到兩個基本資料結構:
==bitmap and linked list==。
由於目前尚未介紹多執行緒環境以及 user process,因此此章節的 source code 並非最終版本,user space 的部分未來再補上,但基本的概念都是不變的。
  1. Bitmap
  2. Linked List
  3. Memory Management
:::
>[time=Wed, Oct 1, 2025 8:14 PM]
---
https://youtu.be/vLVOlLu7CCA
<iframe width="560" height="315" src="https://www.youtube.com/embed/vLVOlLu7CCA?si=GeOkxE1ZTv7W_p0E" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
# Bitamp
* The ==bitmap_init()== function initializes the value of bitmap.
* The ==bitmap_reset()== function resets bits to zero.
* The ==bitmap_set()== function sets the value(0 or 1) to the bitmap.
* The ==bitmap_check()== function checks the value of bitmap.
* The ==bitmap_acquire()== function acquires the available contiguous bitmap.
(if successful, then set the bitmap. Otherwise return -1.)
* The ==bitmap_release()== function releases contiguous bits in the bitmap beginning at btmp_idx.
## Source Code
https://github.com/srhuang/a-os/commit/edc70334997001fa8c4c37468e949e42bc5cff11
## Compile
```
make all
```
## Put on hard disk
```
sh gen.sh
```
## Checkpoint

# Linked List
* The ==list_init()== function initializes a linked list.
* The ==list_insert()== function inserts an element before the specified element.
* The ==list_remove()== function removes the element from the list.
* The ==list_append()== function appends the element to the tail of the list.
* The ==list_push()== function pushes the element to the head of the list.
* The ==list_pop()== function pops the element from the head of the list.
* The ==list_empty()== function returns true if the list is empty.
* The ==list_len()== function returns the length of the list.
* The ==list_find()== function returns true if the specified element is in the list.
* The ==list_traversal()== function traverses the list and returns the first element matching the check function.
## Source Code
https://github.com/srhuang/a-os/commit/63e692150595606de8557e314921ed45f6ededc8
## Compile
```
make all
```
## Put on hard disk
```
sh gen.sh
```
## Checkpoint

# Memory Management
>[!Note] 分成兩部分:
>* `page_malloc` 主要負責以 page 為單位的記憶體配置,由 bitmap 來做紀錄。
>* `sys_malloc` 主要負責小於 page size 的記憶體配置,並且由 kernel/user process 自行管控紀錄。
>[!Note] Bitmap 與 管理記憶體大小的關係
>* 1 個 bit 管理 1 page size(4 KB) 的 RAM。
>* 1 個 byte 管理 32 KB RAM。
>* 4 KB 管理 128 MB。
>* 128 KB 管理 4GB。
>[!Note] Memory Layout
>
>[!Note] Functions Overview
>
>[!Caution] Functions Overview (updated)
>
>[!Note] Memory Blocks
>
>[!Note] Clear TLB
>
>[!Note] Combining Protection of Both Levels of Page Tables
>Reference: [StackOverflow](https://stackoverflow.com/questions/59948388/how-to-properly-set-privileges-in-page-directory-entries)
>
## Source Code
https://github.com/srhuang/a-os/commit/cfc479aa5d6aaf7ae0691f345c8962fc054d6cb9
>[!Warning] Bug Fix
>https://github.com/srhuang/a-os/commit/58fd6d96e7b18bb12fc1352126e95571071135ff
## Compile
```
make all
```
## Put on hard disk
```
sh gen.sh
```
## Checkpoint
RAM size: 512 MB

RAM size: 3GB

Check Bitmap

Check memory block free list
