---
# System prepended metadata

title: 'Chapter 05 : Memory Management'

---

Chapter 05：Memory Management
===

:::info 

記憶體管理就是將記憶體抽象化的技術，會使用到兩個基本資料結構：
==bitmap and linked list==。
由於目前尚未介紹多執行緒環境以及 user process，因此此章節的 source code 並非最終版本，user space 的部分未來再補上，但基本的概念都是不變的。
&emsp; 1. Bitmap
&emsp; 2. Linked List
&emsp; 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

![截圖 2025-10-09 下午2.38.36](https://hackmd.io/_uploads/rJWqOC4axe.png)

# 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

![截圖 2025-10-09 晚上11.41.01](https://hackmd.io/_uploads/H1HhPLHaee.png)

# 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
>![截圖 2025-10-11 凌晨2.35.27](https://hackmd.io/_uploads/rJKzM0Iplg.png)

>[!Note] Functions Overview
>![截圖 2025-10-11 凌晨2.49.38](https://hackmd.io/_uploads/H1IDH0ITxg.png)

>[!Caution] Functions Overview (updated)
>![截圖 2026-01-06 凌晨1.48.25](https://hackmd.io/_uploads/SyYKtOFVbe.png)


>[!Note] Memory Blocks
>![截圖 2025-10-10 清晨5.43.40](https://hackmd.io/_uploads/BJ5o3or6xe.png)

>[!Note] Clear TLB
>![截圖 2025-10-05 下午3.06.17](https://hackmd.io/_uploads/HyNMt5kaxl.png)

>[!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)
>![zAk3W](https://hackmd.io/_uploads/r1JAAJFTgl.png)

## 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
![截圖 2025-10-10 凌晨4.01.54](https://hackmd.io/_uploads/B14C45Hpxx.png)

RAM size: 3GB
![截圖 2025-10-10 清晨5.16.34](https://hackmd.io/_uploads/ByaIIjrTee.png)

Check Bitmap
![截圖 2025-10-10 晚上11.49.04](https://hackmd.io/_uploads/r15DssUplx.png)

Check memory block free list
![截圖 2025-10-11 凌晨1.43.30](https://hackmd.io/_uploads/rJElUpLTlg.png)
