home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 1 / Mecomp-CD.iso / amiga / tools / uhren / tolleuhr / source / libinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-05  |  5.1 KB  |  209 lines

  1. /*
  2. ** tolleuhr.library
  3. ** Written by Gunther Nikl (gnikl@informatik.uni-rostock.de)
  4. */
  5.  
  6. #include <exec/types.h>
  7.  
  8. #if !defined(__GNUC__)
  9. #define REG(x)
  10. #else
  11. #define REG(x) asm(#x)
  12. #endif
  13.  
  14. #include <exec/resident.h>
  15. #include <exec/execbase.h>
  16. #include <proto/exec.h>
  17.  
  18. struct LibBase {
  19.   struct Library  LibNode;
  20.   UWORD           Pad;
  21.   BPTR            SegList;
  22.   struct Library *SysBase;
  23. };
  24.  
  25. LONG Start();
  26.  
  27. /****************************************************************************/
  28.  
  29. #define VERSION  37
  30. #define REVISION  1
  31. #define LIBNAME  "tolleuhr.library\0tolleuhr 37.1 (15.11.95) by M.Fleischer and G.Nikl\r\n"
  32.  
  33. #if !defined(mc68020) && !defined(mc68030) && !defined(mc68040) && !defined(mc68060)
  34. #define REQUIRES_68020(x) (0)
  35. #else
  36. #define REQUIRES_68020(x) ((((struct ExecBase *)x)->AttnFlags & AFF_68020) == 0)
  37. #endif
  38.  
  39. /****************************************************************************/
  40.  
  41.   // First executable routine of this library; must return an error
  42.   // to the unsuspecting caller
  43.  
  44. LONG
  45. ReturnError(VOID)
  46. {
  47.   return -1;
  48. }
  49.  
  50. /****************************************************************************/
  51.  
  52.   /* LibReserved(VOID):
  53.    *
  54.    *  The mandatory reserved library function.
  55.    */
  56.  
  57. LONG
  58. LibReserved(VOID)
  59. {
  60.   return 0;
  61. }
  62.  
  63.   /* LibExpunge(REG(a6) struct LibBase *lib):
  64.    *
  65.    *  Expunge the library, remove it from memory
  66.    */
  67.  
  68. BPTR
  69. LibExpunge(struct LibBase *lib REG(a6))
  70. {
  71.   BPTR Result = 0;
  72.  
  73.   // Expunge it later
  74.  
  75.   lib->LibNode.lib_Flags |= LIBF_DELEXP;
  76.  
  77.   // Can we get away with this?
  78.  
  79.   if (!lib->LibNode.lib_OpenCnt) {
  80.  
  81.     struct Library *SysBase = lib->SysBase;
  82.  
  83.     // Return the seglist, so it can be unloaded
  84.  
  85.     Result = lib->SegList;
  86.  
  87.     // Remove the library from the public list
  88.  
  89.     Remove((struct Node *)lib);
  90.  
  91.     // Free the vector table and the library data
  92.  
  93.     FreeMem((UBYTE *)lib-lib->LibNode.lib_NegSize,lib->LibNode.lib_NegSize+lib->LibNode.lib_PosSize);
  94.   }
  95.  
  96.   // Return the segment pointer, if any
  97.  
  98.   return Result;
  99. }
  100.  
  101.   /* LibClose(REG(a6) struct LibBase *lib):
  102.    *
  103.    *  Close the library, as called by CloseLibrary()
  104.    */
  105.  
  106. BPTR
  107. LibClose(struct LibBase *lib REG(a6))
  108. {
  109.   BPTR Result = 0;
  110.  
  111.   // Decrement usage count and check how
  112.   // many customers we still have
  113.  
  114.   if (!--lib->LibNode.lib_OpenCnt && (lib->LibNode.lib_Flags & LIBF_DELEXP))
  115.     Result = LibExpunge(lib);
  116.  
  117.   return Result;
  118. }
  119.  
  120.   /* LibOpen(REG(a6) struct LibBase *lib):
  121.    *
  122.    *  Open the library, as called via OpenLibrary()
  123.    */
  124.  
  125. struct LibBase *
  126. LibOpen(struct LibBase *lib REG(a6))
  127. {
  128.   // Increment the user count and prevent delayed expunge
  129.  
  130.   lib->LibNode.lib_OpenCnt++;
  131.   lib->LibNode.lib_Flags &= ~LIBF_DELEXP;
  132.  
  133.   return lib;
  134. }
  135.  
  136.   /* LibInit():
  137.    *
  138.    *  Initialize the library.
  139.    */
  140.  
  141. struct LibBase *
  142. LibInit(BPTR SegList REG(a0),struct LibBase *lib REG(d0),struct Library *SysBase REG(a6))
  143. {
  144.   // Set up the header data; everything that doesn't get set
  145.   // up here will have been set up by InitResident().
  146.  
  147.   lib->LibNode.lib_Revision = REVISION;
  148.  
  149.   // Remember the segment pointer
  150.  
  151.   lib->SegList = SegList;
  152.  
  153.   // Remember the exec library base pointer
  154.  
  155.   lib->SysBase = SysBase;
  156.  
  157.   if (37>SysBase->lib_Version || REQUIRES_68020(SysBase)) {
  158.     FreeMem((UBYTE *)lib-lib->LibNode.lib_NegSize,lib->LibNode.lib_NegSize+lib->LibNode.lib_PosSize);
  159.     lib = NULL;
  160.   }
  161.  
  162.   return lib;
  163. }
  164.  
  165. /****************************************************************************/
  166.  
  167.   // This is the table of functions that make up the library. The first
  168.   // four are mandatory, everything following it are user callable
  169.   // routines. The table is terminated by the value -1.
  170.  
  171. const APTR LibVectors[] = {
  172.   LibOpen,
  173.   LibClose,
  174.   LibExpunge,
  175.   LibReserved,
  176.   Start,
  177.   (APTR) -1
  178. };
  179.  
  180.   // The following data structures and data are responsible for
  181.   // setting up the Library base data structure and the library
  182.   // function vector.
  183.  
  184. const ULONG LibInitTable[] = {
  185.   (ULONG)sizeof(struct LibBase), // Size of the base data structure
  186.   (ULONG)LibVectors,             // Points to the function vector
  187.   (ULONG)NULL,                   // Library base data structure setup table
  188.   (ULONG)LibInit                 // The address of the routine to do the setup
  189. };
  190.  
  191. /****************************************************************************/
  192.  
  193.   // The library loader looks for this marker in the memory
  194.   // the library code and data will occupy. It is responsible
  195.   // setting up the Library base data structure.
  196.  
  197. const struct Resident RomTag = {
  198.   RTC_MATCHWORD,                // Marker value.
  199.   (struct Resident *)&RomTag,   // This points back to itself.
  200.   (struct Resident *)&RomTag+1, // This points behind this marker.
  201.   RTF_AUTOINIT,                 // The Library should be set up according to the given table.
  202.   VERSION,                      // The version of this Library.
  203.   NT_LIBRARY,                   // This defines this module as a Library.
  204.   0,                            // Initialization priority of this Library; unused.
  205.   LIBNAME,                      // Points to the name of the Library.
  206.   LIBNAME+17,                   // The identification string of this Library.
  207.   (APTR)&LibInitTable           // This table is for initializing the Library.
  208. };
  209.