Date: | January 24, 2018 / year-entry #20 |
Tags: | code |
Orig Link: | https://blogs.msdn.microsoft.com/oldnewthing/20180124-00/?p=97875 |
Comments: | 4 |
Summary: | Be in the exception handler chain. |
A customer wanted to reserve a range of address space and be notified when the program first reads or writes a page in the range. It's not clear what the customer's goal is, but if it's true that all they want is to be notified of the access, without affecting the underlying memory, then it's not so hard.
In the simplest case, you can mark the page as
In the more complicated case where you want to detect reads and
writes separately,
you can mark the page as
Watch out for the multithreaded case, if two threads take access violations simultaneously on the same page. If you want this fancy memory management only for the duration of a function call, then you can install a structured exception handler around the code whose access is being monitored. If you need this beyond the scope of a single function, then you can use a vectored exception handler.
A variation of this is where you want to commit empty pages
on demand.
In that case,
you
the same technique that
the
There is a gotcha here:
Your custom page fault handler will be
called only for page faults incurred by user mode.
If the program passes a buffer to kernel mode (say as the source
of a Okay, so this works in the case where the program merely wants to be notified of the access, or if it wants to swoop in and allocate blank pages. Next time, we'll look at a more complicated scenario. |
Comments (4)
|
“It’s not clear what the customer’s goal is,”…The first thing that comes to mind is a data breakpoint on a range of addresses
What a bizarre request. The last time I needed to reserve a memory range was when I dealt with dual-ported memory between a Z8530 board and some sort of M68000 mini-computer.
Shared memory? Use the API given to you by the OS. In fact, just rely on the OS. Any OS. This sort of question suggests that the customer has never heard of virtual memory and associated software support going way back into the 1990s.
(I’m wide open to being proven wrong in cases I haven’t considered. But, really. It’s a waste of your time.)
One of the 1990s era Object-Oriented databases did funky things like this with virtual memory. It made for some interesting behavior when things when you started using it – there all sorts of unexpected side-effects.
There are too many typos in this post. :(