home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD1.img / d1xx / d169 / src / dres / core / library.c < prev    next >
C/C++ Source or Header  |  1988-11-22  |  5KB  |  194 lines

  1.  
  2. /*
  3.  *  LIBRARY.C
  4.  *
  5.  *  Example fully working library for Aztec C ... with comments (which is
  6.  *  a miracle in itself).   By Matthew Dillon.    PUBLIC DOMAIN.
  7.  *
  8.  *  Aztec Compile with +BCDLp
  9.  *        +B    No startup reference
  10.  *        +C    Large code
  11.  *        +D    Large data
  12.  *        +L    32 bit Integers
  13.  *        +p    compatibility mode  (D2/D3 preserved)
  14.  *
  15.  *  Since Original release, the following has been fixed:
  16.  *    -slight bug in LibClose() .. did not expunge library if DELEXP set
  17.  *     on final close.  (thanks to Rico Mariani for finding the bug)
  18.  *    -Now uses MakeLibrary call rather than a hardwired Library structure.
  19.  */
  20.  
  21. #define NULL 0L
  22.  
  23. typedef struct Library    LIB;
  24.  
  25. extern LIB *MakeLibrary();
  26.  
  27. #define VERSION     1        /*    NOTE!  String in dc.b below must also be */
  28. #define REVISION    3        /*           Changed!              */
  29.  
  30. #asm
  31. VERSION     equ     1
  32.  
  33.     ;   RLIB.ASM
  34.     ;
  35.     ;   Run-time library tag
  36.  
  37.         FAR     data
  38.  
  39.         public  _CInit
  40.         public  _LibOpen
  41.         public  _LibClose
  42.         public  _LibExpunge
  43.  
  44. Start:        clr.l   D0
  45.         rts
  46.  
  47. InitDesc:   dc.w    $4AFC    ;RTC_MATCHWORD
  48.         dc.l    InitDesc    ;Pointer to beginning
  49.         dc.l    EndCode    ;Note sure it matters
  50.         dc.b    0        ;flags (NO RTF_AUTOINIT)
  51.         dc.b    VERSION    ;version
  52.         dc.b    9        ;NT_LIBRARY
  53.         dc.b    0        ;priority (doesn't matter)
  54.         dc.l    _Libname    ;Name of library
  55.         dc.l    _Libid    ;ID string (note CR-LF at end)
  56.         dc.l    Init    ;Pointer to init routine
  57.  
  58. _Libname:   dc.b    "dres.library",0
  59. _Libid:     dc.b    "dres.library 1.3 (29 Sep 1988)",13,10,0
  60. EndCode:
  61.  
  62. Init:        move.l  A6,-(sp)    ;Must save A6
  63.         move.l  A0,-(sp)    ;Segment list
  64.         jsr     _CInit
  65.         addq.l  #4,sp
  66.         move.l  (sp)+,A6
  67.         rts
  68.  
  69. cifc        macro
  70.         move.l  D0,-(sp)    ;Make a C call and save A6 to boot
  71.         move.l  A6,-(sp)
  72.         jsr     \1
  73.         move.l  (sp)+,A6
  74.         addq.l  #4,sp
  75.         rts
  76.         endm
  77.  
  78. __LibOpen:    cifc    _LibOpen
  79. __LibClose:    cifc    _LibClose
  80. __LibExpunge:    cifc    _LibExpunge
  81.  
  82. #endasm
  83.  
  84. extern char Libname[1];
  85. extern char Libid[1];
  86.  
  87. LIB  *Lib, *DResBase;         /*  Library Base pointer     */
  88. long Seglist;            /*  Save the DOS seglist    */
  89.  
  90.  
  91. /*
  92.  *    The Initialization routine is given only a seglist pointer.  Since
  93.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  94.  *    and return either NULL or the library pointer.  Exec has Forbid()
  95.  *    for us during the call.
  96.  *
  97.  *    If you have an extended library structure you must specify the size
  98.  *    of the extended structure in MakeLibrary().
  99.  */
  100.  
  101. #include "libfuncs.h"
  102.  
  103. LIB *
  104. CInit(segment)
  105. {
  106.     extern long SysBase;
  107.     extern long DOSBase;
  108.  
  109.     SysBase = *(long *)4;
  110.     DOSBase = OpenLibrary("dos.library", 0);
  111.     if (DOSBase == NULL)
  112.     return(NULL);
  113.     DResBase = Lib = MakeLibrary(LibVectors,NULL,NULL,sizeof(LIB),NULL);
  114.     Lib->lib_Node.ln_Type = NT_LIBRARY;
  115.     Lib->lib_Node.ln_Name = Libname;
  116.     Lib->lib_Flags = LIBF_CHANGED|LIBF_SUMUSED;
  117.     Lib->lib_Version  = VERSION;
  118.     Lib->lib_Revision = REVISION;
  119.     Lib->lib_IdString = (APTR)Libid;
  120.     Seglist = segment;
  121.     AddLibrary(Lib);
  122.     return(Lib);
  123. }
  124.  
  125. /*
  126.  *    Open is given the library pointer and the version request.  Either
  127.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  128.  *    Exec has Forbid() for us during the call.
  129.  */
  130.  
  131. LIB *
  132. LibOpen(lib,version)
  133. LIB *lib;
  134. {
  135.     ++lib->lib_OpenCnt;
  136.     lib->lib_Flags &= ~LIBF_DELEXP;
  137.     return(lib);
  138. }
  139.  
  140. /*
  141.  *    Close is given the library pointer and the version request.  Be sure
  142.  *    not to decrement the open count if already zero.    If the open count
  143.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  144.  *    and return the seglist.  Otherwise we return NULL.
  145.  *
  146.  *    Note that this routine never sets LIBF_DELEXP on its own.
  147.  *
  148.  *    Exec has Forbid() for us during the call.
  149.  */
  150.  
  151. LibClose(lib)
  152. LIB *lib;
  153. {
  154.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  155.     return(NULL);
  156.     if (lib->lib_Flags & LIBF_DELEXP)
  157.     return(LibExpunge(lib));
  158.     return(NULL);
  159. }
  160.  
  161. /*
  162.  *    We expunge the library and return the Seglist ONLY if the open count
  163.  *    is zero.    If the open count is not zero we set the DELAYED-EXPUNGE
  164.  *    flag and return NULL.
  165.  *
  166.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  167.  *    might be called from the memory allocator and thus we CANNOT DO A
  168.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  169.  *
  170.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  171.  *    therefore freeze if we called it ourselves.  As far as I can tell
  172.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  173.  *    below.
  174.  */
  175.  
  176. LibExpunge(lib)
  177. LIB *lib;
  178. {
  179.     extern long DOSBase;
  180.  
  181.     if (lib->lib_OpenCnt) {
  182.     lib->lib_Flags |= LIBF_DELEXP;
  183.     return(NULL);
  184.     }
  185.     if (DOSBase) {
  186.     CloseLibrary(DOSBase);
  187.     DOSBase = NULL;
  188.     }
  189.     Remove(lib);
  190.     FreeMem((char *)lib-lib->lib_NegSize, lib->lib_NegSize+lib->lib_PosSize);
  191.     return(Seglist);
  192. }
  193.  
  194.