99#include <mm/alloc.h>
1010#include <panic.h>
1111#include <stdatomic.h>
12+ #include "hartlock.h"
1213
1314void free (void * ptr ) {
1415 if (!ptr )
@@ -17,9 +18,12 @@ void free(void *ptr) {
1718 struct mm_alloc_segment * segment = segment_of_ptr ((uptr )ptr );
1819 struct mm_alloc_page * page = page_of_ptr ((uptr )ptr );
1920 struct mm_alloc_block * block = ptr ;
20- if (segment -> hart == get_hart_locals ()-> hart ) {
21+
22+ WITH_HARTLOCK (hartlock );
23+
24+ if (segment -> hart == get_hart_locals (hartlock )-> hart ) {
2125 // This is a local free; i.e., we're running on the hart that owns the page.
22- struct mm_alloc_heap * heap = get_hart_locals ()-> heap ;
26+ struct mm_alloc_heap * heap = get_hart_locals (hartlock )-> heap ;
2327
2428 page_local_free_push (page , block );
2529 if (page_is_empty (page )) {
@@ -42,8 +46,8 @@ void free(void *ptr) {
4246 }
4347}
4448
45- void * alloc_small (usize size , struct mm_alloc_heap * heap ) {
46- assert (heap -> hart == get_hart_locals ()-> hart );
49+ void * alloc_small (usize size , struct hartlock * hartlock , struct mm_alloc_heap * heap ) {
50+ assert (heap -> hart == get_hart_locals (hartlock )-> hart );
4751 assert (0 < size && size <= 1024 );
4852
4953 // Compute the index into pages_direct.
@@ -57,7 +61,7 @@ void *alloc_small(usize size, struct mm_alloc_heap *heap) {
5761 // generic routine.
5862 struct mm_alloc_block * block = page_free_pop (page );
5963 if (!block )
60- return alloc_generic (size , heap );
64+ return alloc_generic (size , hartlock , heap );
6165
6266 // Increment the counter of used objects.
6367 page -> used_blocks ++ ;
@@ -82,19 +86,19 @@ static void *alloc_generic_from_page(struct mm_alloc_page *page,
8286 return block ;
8387}
8488
85- static void * alloc_huge (usize size , struct mm_alloc_heap * heap ) {
89+ static void * alloc_huge (usize size , struct hartlock * hartlock , struct mm_alloc_heap * heap ) {
8690 assert (size_is_huge (size ));
8791
8892 // Every huge object goes in its own page, so allocate one and use it
8993 // directly.
90- struct mm_alloc_page * page = page_new_huge (heap , size );
94+ struct mm_alloc_page * page = page_new_huge (hartlock , heap , size );
9195 if (!page )
9296 return nullptr ;
9397 return alloc_generic_from_page (page , heap );
9498}
9599
96- void * alloc_generic (usize size , struct mm_alloc_heap * heap ) {
97- assert (heap -> hart == get_hart_locals ()-> hart );
100+ void * alloc_generic (usize size , struct hartlock * hartlock , struct mm_alloc_heap * heap ) {
101+ assert (heap -> hart == get_hart_locals (hartlock )-> hart );
98102 assert (size );
99103
100104 // Go through the delayed free list to free everything.
@@ -107,7 +111,7 @@ void *alloc_generic(usize size, struct mm_alloc_heap *heap) {
107111
108112 // Huge objects get handled separately.
109113 if (size_is_huge (size ))
110- return alloc_huge (size , heap );
114+ return alloc_huge (size , hartlock , heap );
111115
112116 // Check every page of the size class for free objects.
113117 usize size_class = size_class_of_size (size );
@@ -147,9 +151,9 @@ void *alloc_generic(usize size, struct mm_alloc_heap *heap) {
147151 // new one.
148152 assert (list_is_empty (& heap -> pages [size_class ]));
149153 if (size_is_small (size ))
150- page = page_new_small (heap , size_class );
154+ page = page_new_small (hartlock , heap , size_class );
151155 else
152- page = page_new_large (heap , size_class );
156+ page = page_new_large (hartlock , heap , size_class );
153157
154158 // If we couldn't allocate a new page, we're out of memory.
155159 if (!page )
0 commit comments