2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: mkernel/mm/lock.c
5 * PURPOSE: Locking/unlocking virtual memory areas
6 * PROGRAMMER: David Welch (welch@mcmail.com)
9 BOOL
VirtualUnlock(LPVOID lpAddress
, DWORD cbSize
)
11 * FUNCTION: Unlocks pages from the virtual address space of the current
14 * lpAddress = Beginning of the region to unlock
15 * cbSize = Size (in bytes) of the region to unlock
16 * RETURNS: Success or failure
22 BOOL
VirtualLock(LPVOID lpvAddress
, DWORD cbSize
)
24 * FUNCTION: Prevents range of memory from being swapped out
26 * lpvAddress - The base of the range to lock
27 * cbSize - The size of the range to lock
29 * TRUE - the function succeeds
30 * FALSE - the function failed (use GetLastError for details)
32 * NOTES: I'm guessing the kernel loads every page as well as locking it.
35 unsigned int first_page
= PAGE_ROUND_DOWN((int)lpvAddress
);
36 unsigned int length
= PAGE_ROUND_DOWN( ((int)lpvAddress
) + cbSize
) -
38 memory_area
* marea
=NULL
;
39 memory_area
* current
=NULL
;
43 * Check the process isn't trying to lock too much
45 if ( ((length
/PAGESIZE
)+1) > 30)
52 * Find the corresponding vmarea(s)
54 marea
= find_first_marea(memory_area_list_head
,first_page
,
63 * Check the memory areas are committed, continuous and not
67 if (current
->base
!= first_page
)
72 while (current
!=NULL
&& current
->base
< (first_page
+length
) )
74 if (!(current
->state
& MEM_COMMIT
) ||
75 current
->access
& PAGE_NOACCESS
)
80 if ( current
->next
==NULL
)
82 if ((current
->base
+ current
->length
) !=
91 if ( (current
->base
+current
->length
) !=
98 current
=current
->next
;
102 * Lock/load the areas in memory
103 * (the pages aren't loaded just by touching them to avoid the
104 * overhead of a page fault)
107 while (current
!=NULL
&& current
->base
< (first_page
+length
) )
110 for (i
=0; i
<current
->length
; i
++)
112 if (!current
->load_page(marea
,i
))
115 * If the page couldn't be loaded we unlock
116 * the locked pages and abort
118 VirtualUnlock(lpvAddress
,
119 current
->base
+i
-PAGESIZE
);