Many operating systems allow one to memory map files, and read from them lazily. If the operating system can do this then effectively it has the power to create regular pointers out of thunks.
Does any operating system allow an application programmer to create pointers out of his own thunks?
I know that to a limited extent operating systems already support this capability because one can create a pipe, map it into memory, and connect a process to the pipe to accomplish some of what I'm talking about so this functionality doesn't seem too impossible or unreasonable.
A simple example of this feature is a pointer which counts the number of times it's been dereferenced. The following program would output zero, and then one.
static void setter(void* environment, void* input) {
/* You can't set the counter */
}
static void getter(void* environment, void* output) {
*((int*)output) = *((int*)environment)++;
}
int main(int argc, char** argv) {
volatile int counter = 0;
volatile int * const x = map_special_voodoo_magic(getter, setter, sizeof(*x),
&counter);
printf("%i\n", *x);
printf("%i\n", *x);
unmap_special_voodoo_magic(x);
}
P.S. A volatile qualifier is required because the value pointed to by x changes unexpectedly right? As well, the compiler has no reason to think that dereferencing x would change counter so a volatile is needed there too right?