8000 [08-pagealloc] Add freed memory to the beginning of the free block list · havensjg/osdev-demos@491e0ec · GitHub
[go: up one dir, main page]

Skip to content

Commit 491e0ec

Browse files
committed
[08-pagealloc] Add freed memory to the beginning of the free block list
1 parent 1220da3 commit 491e0ec

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

08-pagealloc/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Uses the memory map information from the multiboot bootloader to initialize a pa
44

55
Whenever pages are allocated, they are allocated from the end of the first free block of suitable size. The count of pages in the free block is decremented by the number of pages requested. If more than one page are requested, an entry is added into the multi-page allocation list, recording the number of pages allocated. If a free block no longer contains any pages, it is removed from the list.
66

7-
Whenever pages are freed, if the pages are directly after a free block, that block's page count is incremented by the allocation size retrieved from the multi-page allocation list. If the pages are directly before a free block, the base address is decreased to match the base address of the page and its page count increased by the allocation size. If two free blocks are found to be adjacent after the pages are freed, they are merged.
7+
Whenever pages are freed, if the pages are directly after a free block, that block's page count is incremented by the allocation size retrieved from the multi-page allocation list. If the pages are directly before a free block, the base address is decreased to match the base address of the page and its page count increased by the allocation size. If the freed pages are not adjacent to any other free block, a new free block is created and added to the front of the list so the freed memory is reused before the large blocks created by pgalloc_init. This should help reduce fragmentation in the free block list by using up smaller free blocks. If two free blocks are found to be adjacent after the pages are freed, they are merged.

08-pagealloc/src/pgalloc.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,13 @@ void pgfree(void *ptr) {
297297
/* Fill in the list entry */
298298
blk->base = base;
299299
blk->len = pages;
300-
blk->next = NULL;
301300

302-
/* Add to the list */
303-
if (blk != NULL) {
304-
if (pgalloc_free_head == NULL) {
305-
pgalloc_free_head = blk;
306-
} else {
307-
pgalloc_free_tail->next = blk;
308-
}
301+
/* Add to the front of the list. Due to how pgalloc works, this will give priority to previously used memory, which is usually closer in size than the free blocks created in pgalloc_init */
302+
if (pgalloc_free_tail == NULL) {
309303
pgalloc_free_tail = blk;
310304
}
305+
blk->next = pgalloc_free_head;
306+
pgalloc_free_head = blk;
311307
}
312308

313309
/* Merge if this makes the block adjacent to any others */

0 commit comments

Comments
 (0)
0