home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / xprzedza.lzh / Libinit.c < prev    next >
C/C++ Source or Header  |  1992-11-30  |  5KB  |  159 lines

  1. #define  _USEOLDEXEC_ 1
  2. #include <exec/types.h>
  3. #include <exec/nodes.h>
  4. #include <exec/memory.h>
  5. #include <exec/resident.h>
  6. #include <exec/libraries.h>
  7. #include <libraries/dos.h>
  8. #include <proto/exec.h>
  9. #include <proto/dos.h>
  10. #include <string.h>
  11.  
  12. /* Version and Revision */
  13. #define MYVERSION   0
  14. #define MYREVISION  90
  15.  
  16. /* Prototypes */
  17. ULONG __saveds __asm _LibExpunge( register __a6 struct MyLibrary *libbase );
  18. ULONG __saveds __asm _LibInit( register __a0 APTR seglist,
  19.                                 register __d0 struct MyLibrary *libbase );
  20. void __saveds __asm _UserLibInit(register __a6 struct MyLibrary *libbase );
  21. void __saveds __asm _UserLibCleanup(register __a6 struct MyLibrary *libbase);
  22.  
  23. struct MyLibrary {
  24.         struct       Library ml_Lib;
  25.         ULONG        ml_SegList;
  26.         ULONG        ml_Flags;
  27.         APTR         ml_ExecBase;      /*              pointer to exec base */
  28. };
  29.  
  30. typedef LONG (*PFL)();   /* pointer to function returning 32-bit int        */
  31.  
  32. /* library initialization table, used for AUTOINIT libraries                */
  33. struct InitTable {
  34.         ULONG        *it_DataSize;       /* library data space size         */
  35.         PFL          *it_FuncTable;      /* table of entry points           */
  36.         APTR         it_DataInit;        /* table of data initializers      */
  37.         PFL          it_InitFunc;        /* initialization function to run  */
  38. };
  39.  
  40.  
  41. /* symbols generated by blink */
  42. extern char __far _LibID[];             /* ID string                        */
  43. extern char __far _LibName[];           /* Name string                      */
  44. extern char __far RESLEN;               /* size of init data                */
  45. extern long __far NEWDATAL;             /* size of global data              */
  46. extern PFL _LibFuncTab[];               /* my function table                */
  47. #define DATAWORDS ((long)&NEWDATAL)     /* magic to get right tpye of reloc */ 
  48. #define SIZEJMPTAB ((long)libbase->ml_origbase->ml_numjmps)
  49.                                         /* size in bytes of jmp table       */
  50.  
  51. /* From libent.o, needed to determine where data is loaded by loadseg       */
  52. extern long far _Libmergeddata; 
  53.  
  54.  
  55. struct InitTable __far _LibInitTab =  {
  56.         (long *)(&RESLEN+sizeof(struct MyLibrary)),
  57.         _LibFuncTab,
  58.         NULL,                        /* will initialize my own data */
  59.         _LibInit,
  60. };
  61.  
  62. __saveds __asm
  63. ULONG _LibInit( register __a0 APTR seglist,
  64.         register __d0 struct MyLibrary *libbase )
  65. {
  66.     long *reloc;
  67.     long *sdata;
  68.     char *ddata;
  69.     long nrelocs;
  70.  
  71.       
  72.     libbase->ml_SegList = (ULONG) seglist;
  73.  
  74.     /* init. library structure (since I don't do automatic data init.) */
  75.     libbase->ml_Lib.lib_Node.ln_Type = NT_LIBRARY;
  76.     libbase->ml_Lib.lib_Node.ln_Name =  _LibName;
  77.     libbase->ml_Lib.lib_Flags = LIBF_SUMUSED | LIBF_CHANGED;
  78.     libbase->ml_Lib.lib_Version = MYVERSION;
  79.     libbase->ml_Lib.lib_Revision = MYREVISION;
  80.     libbase->ml_Lib.lib_IdString = (APTR) _LibID;
  81.  
  82.      /* Start of copy of global data after structure */
  83.     ddata = (char *)libbase + sizeof(struct MyLibrary); 
  84.  
  85.     sdata = (long *)&_Libmergeddata; /* where loadseg loaded the data */
  86.     memcpy(ddata, (void *)sdata, DATAWORDS*4);
  87.  
  88.     /* perform relocs if we want one global section for all programs */
  89.     /* that have this lib open. If we want a global section for each */
  90.     /* open, copy the relocs, and do them on each open call.         */
  91.     sdata = sdata + DATAWORDS;
  92.     nrelocs = *sdata;
  93.     sdata++;
  94.     while (nrelocs > 0)
  95.     {
  96.        reloc = (long *)((long)ddata + *sdata++);
  97.        *reloc += (long)ddata;
  98.        nrelocs--;
  99.     }
  100.     _UserLibInit(libbase);
  101.  
  102.     return ( (ULONG) libbase );
  103. }
  104.  
  105. LONG __saveds __asm
  106. _LibOpen( register __a6 struct        MyLibrary *libbase )
  107. {
  108.     /* mark us as having another customer */
  109.     libbase->ml_Lib.lib_OpenCnt++;
  110.  
  111.     /* clear delayed expunges (standard procedure)                */
  112.     libbase->ml_Lib.lib_Flags &= ~LIBF_DELEXP;
  113.  
  114.     return ( (LONG) libbase );
  115. }
  116.  
  117. ULONG __saveds __asm
  118. _LibClose( register __a6 struct MyLibrary *libbase )
  119. {
  120.     ULONG retval = 0;
  121.     
  122.     if (( --libbase->ml_Lib.lib_OpenCnt == 0 ) &&
  123.                         ( libbase->ml_Lib.lib_Flags & LIBF_DELEXP ))
  124.     {
  125.         /* no more people have me open,
  126.          * and I have a delayed expunge pending
  127.          */
  128.          retval = _LibExpunge( libbase ); /* return segment list        */
  129.     }
  130.  
  131.     return (retval);
  132. }
  133.  
  134. ULONG __saveds __asm
  135. _LibExpunge( register __a6 struct MyLibrary *libbase )
  136. {
  137.     ULONG seglist = 0;
  138.     LONG  libsize;
  139.  
  140.     libbase->ml_Lib.lib_Flags |= LIBF_DELEXP;
  141.     if ( libbase->ml_Lib.lib_OpenCnt == 0 )
  142.     {
  143.         /* really expunge: remove libbase and freemem        */
  144.         _UserLibCleanup(libbase);
  145.         seglist = libbase->ml_SegList;
  146.  
  147.         Remove( (struct Node *) libbase);
  148.  
  149.         libsize = libbase->ml_Lib.lib_NegSize + libbase->ml_Lib.lib_PosSize;
  150.         FreeMem( (char *) libbase - libbase->ml_Lib.lib_NegSize,(LONG) libsize );
  151.     }
  152.  
  153.     /* return NULL or real seglist                                */
  154.     return ( (ULONG) seglist );
  155. }
  156.  
  157.  
  158.  
  159.