I would like to lock into memory a small part of the code within a
driver. While the LockPages API is fairly straight forward, other
people in the group have mentioned that the real problem is how to get
the start address of the code pages to be locked (along with the
number of pages). I've come up with what I think is a simple way of
doing this and it seems to work (i.e. LockPages succeeds), but I'd
like to know what others think about it. So, here it is.
1) Put a #pragma code_seg(".somename") right before the code to be
locked.
2) Put the following routine right after the #pragma code_seg and
before the code to be locked:
void LockCode()
{
LockPages(LockCode, (PCHAR)EndLockCode - (PCHAR)LockCode,
NULL, LOCKFLAG_READ);
}
3) Put the following routine right after the code to be locked.
void EndLockCode()
{
}
4) Call LockCode sometime during initialization.
Since code segments start on page boundaries, and LockCode is the
first routine in the segment, it should be aligned on a page boundary.
Is this a safe assumption?
Also, do I need to round the size of the code to be locked (i.e.
EndLockCode - LockCode) up to the nearest multiple of the page size?
Are there any other reasons why this might not work?