@@ -97,10 +97,9 @@ impl Vmalloc {
9797 this
9898 }
9999
100- pub ( super ) fn alloc ( & mut self , mut npages : usize ) -> Option < VirtAddr > {
101- npages += 1 ; // allocate a guard page
102-
103- let size_bytes = npages * Size4KiB :: SIZE as usize ;
100+ pub ( super ) fn alloc ( & mut self , npages : usize ) -> Option < VirtAddr > {
101+ // +1: area for the guard page.
102+ let size_bytes = ( npages + 1 ) * Size4KiB :: SIZE as usize ;
104103
105104 let area = self
106105 . free_list
@@ -125,6 +124,10 @@ impl Vmalloc {
125124 area_cursor. remove ( ) ;
126125 }
127126
127+ // subtract the size of the guard page since we are not required to allocate
128+ // a frame for that area.
129+ let size_bytes = size_bytes - Size4KiB :: SIZE as usize ;
130+
128131 let mut address_space = AddressSpace :: this ( ) ;
129132 let mut offset_table = address_space. offset_page_table ( ) ;
130133
@@ -155,10 +158,9 @@ impl Vmalloc {
155158 Some ( address)
156159 }
157160
158- pub ( super ) fn dealloc ( & mut self , addr : VirtAddr , mut npages : usize ) {
159- npages += 1 ; // deallocate the vmalloc guard page
160-
161- let size = npages * Size4KiB :: SIZE as usize ;
161+ pub ( super ) fn dealloc ( & mut self , addr : VirtAddr , npages : usize ) {
162+ // +1: area for the guard page.
163+ let size = ( npages + 1 ) * Size4KiB :: SIZE as usize ;
162164
163165 // check if this block can be merged into another block.
164166 let merge = self
@@ -176,6 +178,9 @@ impl Vmalloc {
176178 self . free_list . insert ( box VmallocArea :: new ( addr, size) ) ;
177179 }
178180
181+ // subtract the size of the guard page since its not mapped.
182+ let size = size - Size4KiB :: SIZE as usize ;
183+
179184 let mut address_space = AddressSpace :: this ( ) ;
180185 let mut offset_table = address_space. offset_page_table ( ) ;
181186
0 commit comments