I've got an idea how a compiler could convert a callable object to a
plain C function-pointer at runtime, i.e. each callable object would
get an individually runtime-"compiled" C-function.
The compiler would have to generate a skeleton of a stub-function which
will be called as a normal C-fuction and call the individual object's calling-operator with the same parameters the C-function is called with
but of course with the implicit this or context-pointer (if it is a
lamda). This skeleton has an unassigned pointer to the called function -object (with a function object I don't mean necessarily a function<>
object but all objects with a calling-operator). Then there's some
additional code that would be called at runtime when a stub would be
copyied to some memory that's writable as well as executable; this "adjustment-code" will relocate the individually adjusted stub for an
object to its address and relocate the this or context-pointer. So if
you would create such a stub the readjustment-code would be called with
the address of the called object, allocate executable storage for the
copied stub, readjust all the pointers within it, adjust the pointer
to the called object and everything is fine.
It would be best that the callable object's parameters are all native
types or references so that there wouldn't be any additional object
-copying inside the stub.
I came to this idea because with Windows there are some APIs with
callbacks where you can't pass a context-pointer to the API which
will be handed to the callback. Normaly I use a lambda without a
capture here and this lambda accesses a thread-local variable defined
outside that lambda as a context. But that fits only when the callback
won't be called again after the API-call has finished. So in this
cases such a conversion if a callable object to a bare function-pointer
would be nice.
I've got an idea how a compiler could convert a callable object to a
plain C function-pointer at runtime, i.e. each callable object would
get an individually runtime-"compiled" C-function.
The compiler would have to generate a skeleton of a stub-function which
will be called as a normal C-fuction and call the individual object's calling-operator with the same parameters the C-function is called with
but of course with the implicit this or context-pointer (if it is a
lamda). This skeleton has an unassigned pointer to the called function -object (with a function object I don't mean necessarily a function<>
object but all objects with a calling-operator). Then there's some
additional code that would be called at runtime when a stub would be
copyied to some memory that's writable as well as executable; this "adjustment-code" will relocate the individually adjusted stub for an
object to its address and relocate the this or context-pointer. So if
you would create such a stub the readjustment-code would be called with
the address of the called object, allocate executable storage for the
copied stub, readjust all the pointers within it, adjust the pointer
to the called object and everything is fine.
It would be best that the callable object's parameters are all native
types or references so that there wouldn't be any additional object
-copying inside the stub.
I came to this idea because with Windows there are some APIs with
callbacks where you can't pass a context-pointer to the API which
will be handed to the callback. Normaly I use a lambda without a
capture here and this lambda accesses a thread-local variable defined
outside that lambda as a context. But that fits only when the callback
won't be called again after the API-call has finished. So in this
cases such a conversion if a callable object to a bare function-pointer
would be nice.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 113 |
Nodes: | 8 (1 / 7) |
Uptime: | 34:27:20 |
Calls: | 2,498 |
Files: | 8,649 |
Messages: | 1,906,637 |