Bringing apdf from vendor into main branch.
[AROS-Contrib.git] / apdf / server / lib68k.c
blob9a42c2a1b1c54803de573235fe03b8a8d2a20dcb
1 #include <stddef.h>
2 #include <exec/types.h>
3 #include <exec/libraries.h>
4 #include <exec/resident.h>
5 #include <dos/dos.h>
6 #include <proto/exec.h>
8 #ifdef __SASC
9 # define SAVEDS //__saveds confuses data=faronly
10 # define ASM __asm
11 # define REG(r,a) register __ ## r a
12 #elif defined(__GNUC__)
13 # define SAVEDS /*__geta4*/
14 # define ASM
15 # define REG(r,a) a __asm(#r)
16 #endif
18 struct MyLibrary {
19 struct Library lib;
20 BPTR seglist;
23 #define LIB_VERSION 1
24 #define LIB_REVISION 0
26 int LibReserved(void) {
27 return 0;
30 static const APTR LibInitTable[];
31 extern const char LibName[];
32 extern const char LibId[];
34 #ifndef __HAVE_68881__
35 struct Library *__MathIeeeDoubBasBase;
36 struct Library *__MathIeeeDoubTransBase;
37 #endif
39 struct MyLibrary * ASM LibInit(REG(a0,BPTR seglist),
40 REG(d0,struct MyLibrary *base),
41 REG(a6,struct ExecBase *sysbase));
42 struct MyLibrary * ASM LibOpen(REG(a6,struct MyLibrary *base));
43 BPTR ASM LibClose(REG(a6,struct MyLibrary *base));
44 BPTR ASM LibExpunge(REG(a6,struct MyLibrary *base));
46 void (** SAVEDS ASM LibgetBase(void))();
48 static const struct Resident romtag={
49 RTC_MATCHWORD,
50 (struct Resident *)&romtag,
51 (APTR)((&romtag)+1),
52 RTF_AUTOINIT,
53 LIB_VERSION,
54 NT_LIBRARY,
56 (char*)LibName,
57 (char*)LibId,
58 (APTR)LibInitTable
61 static const APTR LibFuncTable[]={
62 LibOpen,
63 LibClose,
64 LibExpunge,
65 LibReserved,
66 LibgetBase,
67 (APTR)-1
70 #define INITBYTE_ENTRY(x) UWORD a##x;UWORD b##x;UBYTE c##x;UBYTE d##x
71 #define INITWORD_ENTRY(x) UWORD a##x;UWORD b##x;UWORD c##x
72 #define INITAPTR_ENTRY(x) UWORD a##x;UWORD b##x;APTR c##x
73 #define INITBYTE(S,E,x) 0xe000,offsetof(struct S,E),(x),0
74 #define INITWORD(S,E,x) 0xd000,offsetof(struct S,E),(x)
75 #define INITAPTR(S,E,x) 0xc000,offsetof(struct S,E),(x)
77 static const struct {
78 INITBYTE_ENTRY(0);
79 INITAPTR_ENTRY(1);
80 INITBYTE_ENTRY(2);
81 INITWORD_ENTRY(3);
82 INITWORD_ENTRY(4);
83 INITAPTR_ENTRY(5);
84 UWORD end;
85 } LibInitData={
86 INITBYTE(Node,ln_Type,NT_LIBRARY),
87 INITAPTR(Node,ln_Name,(APTR)LibName),
88 INITBYTE(Library,lib_Flags,LIBF_SUMUSED|LIBF_CHANGED),
89 INITWORD(Library,lib_Version,LIB_VERSION),
90 INITWORD(Library,lib_Revision,LIB_REVISION),
91 INITAPTR(Library,lib_IdString,(APTR)LibId),
95 static const APTR LibInitTable[]={
96 (APTR)((sizeof(struct MyLibrary)+3)&~3),
97 (APTR)LibFuncTable,
98 (APTR)&LibInitData,
99 (APTR)LibInit
102 struct ExecBase *SysBase;
104 extern void (*funcTable[])();
107 struct MyLibrary * SAVEDS ASM LibInit(REG(a0,BPTR seglist),
108 REG(d0,struct MyLibrary *base),
109 REG(a6,struct ExecBase *sysbase)) {
110 base->seglist=seglist;
111 SysBase=sysbase;
112 return base;
115 struct MyLibrary * SAVEDS ASM LibOpen(REG(a6,struct MyLibrary *base)) {
116 base->lib.lib_Flags&=~LIBF_DELEXP;
117 #ifndef __HAVE_68881__
118 __MathIeeeDoubBasBase = OpenLibrary("mathieeedoubbas.library",0);
119 if (__MathIeeeDoubBasBase)
120 __MathIeeeDoubTransBase = OpenLibrary("mathieeedoubtrans.library",0);
121 if (!__MathIeeeDoubBasBase || !__MathIeeeDoubTransBase)
122 return NULL;
123 #endif
124 ++base->lib.lib_OpenCnt;
125 return base;
128 BPTR SAVEDS ASM LibClose(REG(a6,struct MyLibrary *base)) {
129 BPTR retval=0;
130 #ifndef __HAVE_68881__
131 CloseLibrary(__MathIeeeDoubTransBase);
132 CloseLibrary(__MathIeeeDoubBasBase);
133 #endif
134 if(base->lib.lib_OpenCnt>0)
135 --base->lib.lib_OpenCnt;
136 if(base->lib.lib_OpenCnt==0 && base->lib.lib_Flags&LIBF_DELEXP)
137 retval=LibExpunge(base);
138 return retval;
141 BPTR SAVEDS ASM LibExpunge(REG(a6,struct MyLibrary *base)) {
142 BPTR ret=0;
143 base->lib.lib_Flags|=LIBF_DELEXP;
144 if(base->lib.lib_OpenCnt==0) {
145 struct Library *SysBase=*(struct Library**)4;
146 ret=base->seglist;
147 Remove(&base->lib.lib_Node);
148 FreeMem((UBYTE *)base-base->lib.lib_NegSize,
149 base->lib.lib_NegSize+base->lib.lib_PosSize);
151 return ret;
155 void (** SAVEDS ASM LibgetBase(void))() {
156 return funcTable;