[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
George Foot <george.foot@xxxxxxxxxx> writes:
>> The constructors are used to set up an indirect pointer to those
>> functions instead, so that the code only gets linked if it is actually
>> called.
>
> But `_midi_constructor' is in `midi.c' -- surely if it's always getting
> called, from `allegro.c', then its code has to be linked in, so all the
> code in `midi.c' has to be linked in anyway, even if you never call
> `install_sound' even?
Yes, that's what happens if there are no constructor functions. But the
call from allegro.c doesn't happen on platforms that do have constructors
(ie. gcc, which is all the good ones :-)
> I don't see any way that it would be possible to get around this
> problem without having separate initialisation functions for digital
> sound and MIDI music, and having `install_sound' just for backward
> compatibility.
Nah, it's already been gotten around. SET is a clever man :-)
The original problem was that as you say, some things in midi.c needed to
be initialised, but there was no dedicated midi_init() function in the
API, so install_sound() had to call those initialisers, which resulted in
all the MIDI code being linked into every program that called
install_sound(), regardless of whether it only actually played samples,
MOD music, or whatever. The constructor system totally avoids this
problem, though.
In sound.c, there is a global pointer to the MIDI init routine,
initialised to NULL. install_sound() checks that pointer, and calls it if
it is set. None of that has any direct reference to the MIDI code, so it
doesn't drag in unused routines.
In midi.c, there is a constructor function that initialises the global
pointer (which is declared in sound.c) to the real init function, that
lives in midi.c. But if your program doesn't call any MIDI functions,
there will be no need for midi.c to get linked at all, so that
constructur will never be executed, and the pointer will never be set, so
install_sound() will never call it. It is only when you add a call to
play_midi(), midi_out(), or whatever, somewhere in your program, that
midi.c will get linked, the constructor gets included, and so the init
code is executed. It's a preemptive setup: install_sound() will only call
the MIDI init functions if they are in fact included in your program, and
they will only be included if you do in fact make some calls to those
routines elsewhere in the code.
--
Shawn Hargreaves - shawn@xxxxxxxxxx - http://www.talula.demon.co.uk/
"A binary is barely software: it's more like hardware on a floppy disk."