How to achieve bidirectional load-time linking
- From: ben mitch <news@xxxxxxxxxxxx>
- Date: Mon, 16 Jun 2008 11:59:52 +0200
Hi
Consider a host application that allows users to author plugins as dynamic libraries. The host needs to call the plugin to ask it to perform, and also provides an API which the plugin can call:
HOST ==> PLUGIN ==> API
If the functions in API are encapsulated, data-wise, no problem:
0) HOST is the executable, PLUGIN exports functions for HOST to call, and API is a dynamic library with exported functions for PLUGIN to call.
But what if the functions in API operate on data that is shared with HOST? In this case, some interaction must take place between HOST and API, and it's not clear to me what is the correct module/linking arrangement to use. Ideas so far...
1) Function Stubs: As 0, but API function exports are stubs that call implementations in HOST. API includes one function to connect the function pointers associated with the stubs at start-up. Inefficient, because one API call becomes two function calls and a pointer resolution.
2) Function Pointers: As 1, but the function pointers are exposed directly as exports from API, rather than being hidden behind function stubs. Less robust to misuse, but more efficient.
3) Main Stub: HOST and API reside in a single dynamic library, and main() in the executable is a stub that hands control over to it. Efficient, but not sure if it introduces problems when HOST is updated.
4) As 3, but HOST and API reside in separate libraries, whilst main() is still a stub handing control to the HOST library. Avoids problems on updating HOST?
5) Executable exports: Executable implements HOST and API, there is no library, and PLUGIN author gets only a header showing what API functions will be available at load-time. At load-time, the plugins are linked with functions exported from the executable. May be impossible on some or all platforms.
6) Struct Of Functions: API is offered as a .h header only (no binary library), and a struct of function pointers is passed to PLUGIN after loading; the same pointer approach as in (2), but without needing a dynamic library.
The various approaches have their pros and cons - I would much appreciate some input - how should I choose between these? Is there, ideally, a "standard solution"? I favour 5, or 3/4 if that is impossible due to system limitations. 6 seems like the best of the inefficient rest, but smacks of reinventing the wheel (where the wheel is the load-time linking process).
I have tried to keep this question language and OS non-specific. I realize that there may be issues with specific compilers/systems, but I feel there should be an underlying truth, nonetheless.
Thanks in advance, guys.
Ben Mitch
.
- Follow-Ups:
- Re: How to achieve bidirectional load-time linking
- From: Raja Mukherji
- Re: How to achieve bidirectional load-time linking
- From: Reinder Verlinde
- Re: How to achieve bidirectional load-time linking
- Prev by Date: Re: Arrays and like objects
- Next by Date: Cartooning Your Way to English or Foreign Language Learning Success
- Previous by thread: Re: Arrays and like objects
- Next by thread: Re: How to achieve bidirectional load-time linking
- Index(es):
Relevant Pages
|