BB2 C port
API reference: Blitz libraries in C and the shared code
Loading...
Searching...
No Matches
deflib.h
Go to the documentation of this file.
1/*
2 * deflib.h - init code shared by the AmigaOS call libraries of
3 * blitz2:deflibs (deflibs/NAME.c, see notes/deflibs.md).
4 *
5 * The originals (cd/extras/blitzlibs/amigalibs/NAME.library1) all have the
6 * same init:
7 *
8 * init: MOVE.l 4,a6
9 * LEA name(pc),a1
10 * loop: MOVEQ #0,d0
11 * JSR OpenLibrary(a6) ; resources: OpenResource
12 * TST.l d0
13 * BEQ init+6 ; = the middle of the LEA
14 * RTS
15 *
16 * Fixed (intended deviation, #44): in the original, the init never
17 * returns on failure. The branch target is the displacement word of the
18 * LEA, which executes as ORI.b #0,(a0)+ (the MOVEQ is its immediate), so
19 * it calls OpenLibrary again with d0 = 0 and whatever a1 holds after the
20 * failed call (a scratch register of exec) - an endless loop: a program
21 * using a call of a missing library hangs at startup. Here the init
22 * returns the base or 0, like the original's CrM.library (crmlib.c) and
23 * AmiBlitz3's deflibs; the finit of a library closes it only if the base
24 * is not 0 (CloseLibrary(0) crashes before Kickstart 2.0). With base 0
25 * the program runs; a call of the missing library crashes (JSR to a
26 * negative offset from 0), so a program that may run without it checks
27 * for it itself (e.g. its own OpenLibrary_) before using its calls.
28 * Not a clean exit instead: the deflibs are initialised before the
29 * command libraries (inits by falling library number), so an exit from
30 * the init (exitslib's End) would run their finits without their inits,
31 * and it would need a declaration change (the init's !libs) and take
32 * away the use of an optional library.
33 *
34 * Fixed (intended deviation, #45) for the resources (ciaa, ciab, disk,
35 * misc, potgo, battclock, battmem, card): they were made from the library
36 * template, whose finit (MOVE.l 4,a6:JMP CloseLibrary(a6)) is 6 bytes
37 * longer than theirs (RTS), without adjusting the LEA: in the original it
38 * points 6 bytes into the name ("ciab.resource" -> "esource"), so
39 * OpenResource always fails and the init hangs. Here the resource files
40 * pass the name itself.
41 */
42#ifndef DEFLIB_H
43#define DEFLIB_H
44
45#include "blitzexec.h"
46
47/* OpenLibrary(name, 0): the base, 0 if it cannot be opened (fixed, #44:
48 the original retries forever, see above) */
49static inline ULONG deflib_openlibrary(const char *name)
50{
51 return (ULONG)OpenLibrary((CONST_STRPTR)name, 0);
52}
53
54/* finit of a library: CloseLibrary(base) if the init opened it (the
55 original: MOVE.l 4,a6:JMP CloseLibrary(a6), never 0 there) */
56static inline void deflib_closelibrary(struct Library *base)
57{
58 if (base)
59 CloseLibrary(base);
60}
61
62/* OpenResource(name): the base, 0 if there is none (fixed, #44: the
63 original retries forever, see above) */
64static inline ULONG deflib_openresource(const char *name)
65{
66 return (ULONG)OpenResource((CONST_STRPTR)name);
67}
68
69#endif
char * name
Definition asllib.c:150
static void deflib_closelibrary(struct Library *base)
Definition deflib.h:56
static ULONG deflib_openresource(const char *name)
Definition deflib.h:64
static ULONG deflib_openlibrary(const char *name)
Definition deflib.h:49