This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Dynamic Loading of Class Implementation at Runtime.



>     I've seen messages in the past that allude to this subject and I've
> searched long and hard for a (somewhat) platform independent mechanism
> to implement this feature but with no success. So... I'm in need of
> information on how to load, at runtime, from a shared library, the
> implementation of a class (or its subclass) and then be able to unload
> it and load a new implementation.

We did that in the Ptolemy project since 1991 or so
(URL:<http://ptolemy.eecs.berkeley.edu/>).

>     This requires the ability to resolve virtual and non-virtual member
> functions and not just C-type functions which is what has been the big
> problem.

The scheme we used is roughly the object factory pattern (though since we
were doing it before the books were written the terminology might not
match up right).  You can load and unload new classes, provided that they
are derived from a class that is already in the executable and that this
pre-existing class has a clone method (which serves as a "virtual
constructor").  The trick works either with shared libraries or the old
"ld -A" stuff from Berkeley Unix.

The key is that when a shared library is loaded, the constructors for
global and static objects are called.  One or more of these registers
an instance of each new class in the registry (alternatively, you can
register an object that can create an object of the new class).  Then,
by saying something like Registry::makeNew(className) you construct
objects of the new class.

To determine when it is safe to unmap the shared library you'll need
some kind of reference counting or garbage collection, to know that
there are objects left that reference the code.

Note that using this scheme you can only call virtual methods that
override virtual methods of the base class that was pre-loaded.
But with a suitable class design that can be powerful enough.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]