home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / util / misc / fifolib38_1.lha / lib.c < prev    next >
C/C++ Source or Header  |  1995-12-19  |  3KB  |  121 lines

  1.  
  2. /*
  3.  *  LIB.C
  4.  *
  5.  *  Basic Library Resource Handling
  6.  *
  7.  *  NOTE: all data declarations should be initialized since we skip
  8.  *      normal C startup code (unless initial value is don't care)
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. #ifndef _DCC
  14. extern const LONG (*Vectors[])(); /*  Assembler library entry points */
  15. #else
  16. extern LONG Vectors();          /*  trick a reference in the code section */
  17.                   /*  __far would require the registered DICE */
  18. #endif
  19.  
  20. /* Library *LibBase = NULL; */    /*  Library Base pointer    */
  21. long    SegList  = 0;
  22. ExecBase *SysBase  = NULL;
  23. List    FifoList;
  24.  
  25. /*
  26.  *    The Initialization routine is given only a seglist pointer.  Since
  27.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  28.  *    and return either NULL or the library pointer.  Exec has Forbid()
  29.  *    for us during the call.
  30.  *
  31.  *    If you have an extended library structure you must specify the size
  32.  *    of the extended structure in MakeLibrary().
  33.  */
  34.  
  35. LibCall Library *
  36. LibInit(long segment)
  37. {
  38.     Library *lib;
  39.  
  40.     SysBase = *((ExecBase **) 4L);
  41.  
  42.     if ((lib = MakeLibrary(Vectors,NULL,NULL,sizeof(Library),NULL))) {
  43.     /* LibBase = lib; */
  44.     lib->lib_Node.ln_Type = NT_LIBRARY;
  45.     lib->lib_Node.ln_Name = LibName;
  46.     lib->lib_Flags = LIBF_CHANGED|LIBF_SUMUSED;
  47.     lib->lib_Version  = 38;
  48.     lib->lib_Revision = 1;
  49.     lib->lib_IdString = (APTR)LibId;
  50.     SegList = segment;
  51.     AddLibrary(lib);
  52.  
  53.     NewList((MaxList *)&FifoList);
  54.     }
  55.     return(lib);
  56. }
  57.  
  58. /*
  59.  *    We expunge the library and return the Seglist ONLY if the open count
  60.  *    is zero.    If the open count is not zero we set the DELAYED-EXPUNGE
  61.  *    flag and return NULL.
  62.  *
  63.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  64.  *    might be called from the memory allocator and thus we CANNOT DO A
  65.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  66.  *
  67.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  68.  *    therefore freeze if we called it ourselves.  As far as I can tell
  69.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  70.  *    below.
  71.  */
  72.  
  73. LibCall long
  74. LibExpunge(long dummy, Library *lib)
  75. {
  76.     if (lib->lib_OpenCnt) {
  77.     lib->lib_Flags |= LIBF_DELEXP;
  78.     return(NULL);
  79.     }
  80.     Remove((MaxNode *)lib);
  81.     FreeMem((void *)lib-lib->lib_NegSize, lib->lib_NegSize+lib->lib_PosSize);
  82.  
  83.     return(SegList);
  84. }
  85.  
  86. /*
  87.  *    Open is given the library pointer and the version request.  Either
  88.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  89.  *    Exec has Forbid() for us during the call.
  90.  */
  91.  
  92. LibCall Library *
  93. LibOpen(long version, Library *lib)
  94. {
  95.     ++lib->lib_OpenCnt;
  96.     lib->lib_Flags &= ~LIBF_DELEXP;
  97.     return(lib);
  98. }
  99.  
  100. /*
  101.  *    Close is given the library pointer and the version request.  Be sure
  102.  *    not to decrement the open count if already zero.    If the open count
  103.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  104.  *    and return the seglist.  Otherwise we return NULL.
  105.  *
  106.  *    Note that this routine never sets LIBF_DELEXP on its own.
  107.  *
  108.  *    Exec has Forbid() for us during the call.
  109.  */
  110.  
  111. LibCall long
  112. LibClose(long dummy, Library *lib)
  113. {
  114.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  115.     return(NULL);
  116.     if (lib->lib_Flags & LIBF_DELEXP)
  117.     return(LibExpunge(0, lib));
  118.     return(NULL);
  119. }
  120.  
  121.