8000 [08-pagealloc] Add pgfree to free page allocations · havensjg/osdev-demos@77c4d4c · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 77c4d4c

Browse files
committed
[08-pagealloc] Add pgfree to free page allocations
1 parent 63090a0 commit 77c4d4c

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

08-pagealloc/include/pgalloc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ pgalloc_free_block_t *pgalloc_alloc_free_block();
5252
int pgalloc_init(void);
5353

5454
/* Register a multi-page allocation in the list of multi-page allocations */
55-
int pgalloc_register_multipage(uint32_t base, uint32_t pages);
55+
int pgalloc_register_multipage(uint32_t base, uint32_t pages);
56+
57+
/* Free a page allocation */
58+
void pgfree(void *ptr);

08-pagealloc/src/kernel.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,26 @@ void kernel_main(void)
3232
}
3333

3434
/* Test the page allocator */
35-
void *page = pgalloc(1);
36-
printf("Allocated 1 page: %p\n",page);
35+
void *page1 = pgalloc(1);
36+
printf("Allocated 1 page: %p\n",page1);
3737

38-
page = pgalloc(1);
39-
printf("Allocated 1 page: %p\n",page);
38+
void *page2 = pgalloc(1);
39+
printf("Allocated 1 page: %p\n",page2);
4040

41-
page = pgalloc(10);
42-
printf("Allocated 10 pages: %p\n",page);
41+
void *page3 = pgalloc(10);
42+
printf("Allocated 10 pages: %p\n",page3);
4343

44-
page = pgalloc(1);
45-
printf("Allocated 1 page: %p\n",page);
44+
void *page4 = pgalloc(1);
45+
printf("Allocated 1 page: %p\n",page4);
46+
47+
/* Free non-contiguous allocations. Each should create a new entry in the free block list. */
48+
pgfree(page1);
49+
pgfree(page3);
50+
51+
/* Now allocate a page and see where it ends up */
52+
void *page5 = pgalloc(1);
53+
printf("Allocated 1 page: %p\n",page5);
54+
55+
/* Free page2, which is now between two freed fragments. pgfree should merge them together. */
56+
pgfree(page2);
4657
}

08-pagealloc/src/pgalloc.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,56 @@ int pgalloc_register_multipage(uint32_t base, uint32_t pages) {
205205

206206
printf("pgalloc_register_multipage: no free entries in the multipage list\n");
207207
return 1;
208+
}
209+
210+
/* Free a page allocation */
211+
void pgfree(void *ptr) {
212+
uint32_t pages = 1;
213+
uint32_t base = (uint32_t) ptr;
214+
215+
/* Check if it's multipage. If it's not in the list, it's one page */
216+
for (int i=0; i<PGALLOC_MULTIPAGE_LIST_SIZE; i++) {
217+
/* Check if base matches */
218+
if (pgalloc_multipage_list[i].base == base) {
219+
/* Retrieve page count */
220+
pages = pgalloc_multipage_list[i].pages;
221+
222+
/* Mark as unused */
223+
pgalloc_multipage_list[i].base = 0;
224+
pgalloc_multipage_list[i].pages = 0;
225+
break;
226+
}
227+
}
228+
229+
/* TODO: Shortcut: this allocation is right at the end of a free block */
230+
pgalloc_free_block_t *blk = NULL;
231+
232+
/* TODO: Shortcut: this allocation is right at the beginning of a free block */
233+
234+
/* Not either shortcut case: make a new entry in the free block list */
235+
if (blk == NULL) {
236+
/* New free block list entry */
237+
blk = pgalloc_alloc_free_block();
238+
if (blk == NULL) {
239+
printf("pgfree: unable to allocate free block list entry\n");
240+
return;
241+
}
242+
243+
/* Fill in the list entry */ 9421
244+
blk->base = base;
245+
blk->len = pages;
246+
blk->next = NULL;
247+
}
248+
249+
/* Add to the list */
250+
if (blk != NULL) {
251+
if (pgalloc_free_head == NULL) {
252+
pgalloc_free_head = blk;
253+
} else {
254+
pgalloc_free_tail->next = blk;
255+
}
256+
pgalloc_free_tail = blk;
257+
}
258+
259+
/* TODO: Merge if this makes the block adjacent to any others */
208260
}

0 commit comments

Comments
 (0)
0