Given something like:

void *p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
0, 0);

is there a way to detect a write somewhere / anywhere within p -> p + size -
1 (given the constaints of mmap alignment).

I know about mprotect() + trapping SIGSEGV and have been able to get that to
work, but am looking for a more elegant way to do this within a library
because, AFAIK:

-- There is no way to prevent the user of the library from also mprotect'ing
something else and/or trapping SIGSEGV in a different manner.
-- I'm not sure that the writes are guaranteed to succeed given this
scenario (and what I'm looking for is a notification of when a write happens
in order to make some decisions, and not to prevent the write or do some
custom error handling).

I need mmap and the memory it returns to function as efficiently as, say,
malloc(), but am not neccessarily opposed to a file backed SHARED/NON-ANON
mapping if there is someway to accomplish what I want with that also.

I think windows has a good solution for this in VirtualAlloc() /
GetWriteWatch() and I'm basically looking for the same on linux.

If there is a better place to post the question, please advise.

Pseudo code always gratefully accepted

Thanks,

- Dave