home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / System / MorphOS / Developer / emulexamples / library / skeleton / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-02  |  7.6 KB  |  232 lines

  1. #include    "libdata.h"
  2. #include    "ppcexample.library_VERSION.h"
  3.  
  4. #define    DEBUG_INIT(x)        x;
  5. #define    DEBUG_OPEN(x)        x;
  6. #define    DEBUG_CLOSE(x)        x;
  7. #define    DEBUG_EXPUNGE(x)    x;
  8. #define    DEBUG_NULL(x)        x;
  9.  
  10. void    LibEnd(void);
  11.  
  12. void    (*LibFuncTable[])(void);
  13. struct Library*    LIB_Init(struct LibBase        *MyLibBase,
  14.                          BPTR            SegList,
  15.                          struct ExecBase    *SBase);
  16. extern    ULONG    Dummy;
  17.  
  18.  
  19. struct LibInitStruct
  20. {
  21.     ULONG    LibSize;
  22.     void    *FuncTable;
  23.     void    *DataTable;
  24.     void    (*InitFunc)(void);
  25. };
  26.  
  27. struct LibInitStruct LibInitStruct=
  28. {
  29.     sizeof(struct LibBase),
  30.     LibFuncTable,
  31.     NULL,
  32.     (void (*)(void)) &LIB_Init
  33. };
  34.  
  35.  
  36. struct Resident LibResident=
  37. {
  38.     RTC_MATCHWORD,
  39.     &LibResident,
  40.     &LibResident + 1,
  41.     RTF_PPC | RTF_AUTOINIT,
  42.     VERSION,
  43.     NT_LIBRARY,
  44.     0,
  45.     "ppcexample.library",
  46.     VSTRING,
  47.     &LibInitStruct
  48. };
  49.  
  50. /*
  51.  * To tell the loader that this is a new emulppc elf and not
  52.  * one for the ppc.library.
  53.  * ** IMPORTANT **
  54.  */
  55. ULONG    __amigappc__=1;
  56.  
  57. /***********************************************************************/
  58. /***********************************************************************/
  59. /***********************************************************************/
  60. /***********************************************************************/
  61. /***********************************************************************/
  62. /***********************************************************************/
  63. /***********************************************************************/
  64. /***********************************************************************/
  65. /***********************************************************************/
  66.  
  67. /* The lib execute init protection which simply returns
  68.  */
  69.  
  70. ULONG    NoExecute(void)
  71. {
  72.   return(0);
  73. }
  74.  
  75. /***********************************************************************/
  76. /***********************************************************************/
  77. /***********************************************************************/
  78. /***********************************************************************/
  79. /***********************************************************************/
  80. /***********************************************************************/
  81. /***********************************************************************/
  82. /***********************************************************************/
  83. /***********************************************************************/
  84.  
  85. /*
  86.  * Resident is a RTF_PPC one...that means it`s called
  87.  * natively with normal PPC SysV4 ABI argument order.
  88.  */
  89. struct Library* LIB_Init(struct LibBase        *MyLibBase,
  90.                          BPTR            SegList,
  91.                          struct ExecBase    *SBase)
  92. {
  93.   DEBUG_INIT(dprintf("LibInit: LibBase 0x%lx SegList 0x%lx SBase 0x%lx\n"));
  94.  
  95.   MyLibBase->SegList    =    SegList;
  96.   MyLibBase->SBase    =    SBase;
  97.   return(&MyLibBase->Lib);
  98. }
  99.  
  100.  
  101.  
  102. /***********************************************************************/
  103. /***********************************************************************/
  104. /***********************************************************************/
  105. /***********************************************************************/
  106. /***********************************************************************/
  107. /***********************************************************************/
  108. /***********************************************************************/
  109. /***********************************************************************/
  110. /***********************************************************************/
  111.  
  112. ULONG    LibExpunge(struct LibBase *MyLibBase)
  113. {
  114. BPTR    MySegment;
  115.  
  116.   DEBUG_EXPUNGE(dprintf("LIB_Expunge: LibBase 0x%lx <%s> OpenCount %ld\n",
  117.                         MyLibBase,
  118.                         MyLibBase->Lib.lib_Node.ln_Name,
  119.                         MyLibBase->Lib.lib_OpenCnt));
  120.  
  121.   MySegment    =    MyLibBase->SegList;
  122.  
  123.   if (MyLibBase->Lib.lib_OpenCnt)
  124.   {
  125.     DEBUG_EXPUNGE(dprintf("LIB_Expunge: set LIBF_DELEXP\n"));
  126.     MyLibBase->Lib.lib_Flags |= LIBF_DELEXP;
  127.     return(NULL);
  128.   }
  129.  
  130.   /* We don`t need a forbid() because Expunge and Close
  131.    * are called with a pending forbid.
  132.    * But let`s do it for savety if somebody does it by hand.
  133.    */
  134.   Forbid();
  135.   DEBUG_EXPUNGE(dprintf("LIB_Expunge: remove the library\n"));
  136.   Remove(&MyLibBase->Lib);
  137.   Permit();
  138.  
  139.   DEBUG_EXPUNGE(dprintf("LIB_Expunge: free the library\n"));
  140.   FreeMem((APTR)((ULONG)(MyLibBase) - (ULONG)(MyLibBase->Lib.lib_NegSize)),
  141.           MyLibBase->Lib.lib_NegSize + MyLibBase->Lib.lib_PosSize);
  142.  
  143.   DEBUG_EXPUNGE(dprintf("LIB_Expunge: return Segment 0x%lx to ramlib\n",
  144.                         MySegment));
  145.   return((ULONG) MySegment);
  146. }
  147.  
  148. ULONG    LIB_Expunge(void)
  149. {
  150. struct LibBase    *MyLibBase=(struct LibBase*) REG_A6;
  151.  
  152.   DEBUG_EXPUNGE(dprintf("LIB_Expunge:\n"));
  153.   return(LibExpunge(MyLibBase));
  154. }
  155.  
  156. /***********************************************************************/
  157. /***********************************************************************/
  158. /***********************************************************************/
  159. /***********************************************************************/
  160. /***********************************************************************/
  161. /***********************************************************************/
  162. /***********************************************************************/
  163. /***********************************************************************/
  164. /***********************************************************************/
  165.  
  166. struct Library*    LIB_Open(void)
  167. {
  168. struct LibBase    *MyLibBase=(struct LibBase*) REG_A6;
  169.  
  170.   DEBUG_OPEN(dprintf("LIB_Open: 0x%lx <%s> OpenCount %ld\n",
  171.                      MyLibBase,
  172.                      MyLibBase->Lib.lib_Node.ln_Name,
  173.                      MyLibBase->Lib.lib_OpenCnt));
  174.  
  175.  
  176.   MyLibBase->Lib.lib_Flags    &=    ~LIBF_DELEXP;
  177.   MyLibBase->Lib.lib_OpenCnt++;
  178.   return(&MyLibBase->Lib);
  179. }
  180.  
  181. /***********************************************************************/
  182. /***********************************************************************/
  183. /***********************************************************************/
  184. /***********************************************************************/
  185. /***********************************************************************/
  186. /***********************************************************************/
  187. /***********************************************************************/
  188. /***********************************************************************/
  189. /***********************************************************************/
  190.  
  191. ULONG    LIB_Close(void)
  192. {
  193. struct LibBase    *MyLibBase=(struct LibBase*) REG_A6;
  194.  
  195.   DEBUG_CLOSE(dprintf("LIB_Close: 0x%lx <%s> OpenCount %ld\n",
  196.                       MyLibBase,
  197.                       MyLibBase->Lib.lib_Node.ln_Name,
  198.                       MyLibBase->Lib.lib_OpenCnt));
  199.  
  200.   if ((--MyLibBase->Lib.lib_OpenCnt) > 0)
  201.   {
  202.     DEBUG_CLOSE(dprintf("LIB_Close: done\n"));
  203.   }
  204.   else
  205.   {
  206.     if (MyLibBase->Lib.lib_Flags & LIBF_DELEXP)
  207.     {
  208.       DEBUG_CLOSE(dprintf("LIB_Close: LIBF_DELEXP set\n"));
  209.       return(LibExpunge(MyLibBase));
  210.     }
  211.   }
  212.   return(0);
  213. }
  214.  
  215. /***********************************************************************/
  216. /***********************************************************************/
  217. /***********************************************************************/
  218. /***********************************************************************/
  219. /***********************************************************************/
  220. /***********************************************************************/
  221. /***********************************************************************/
  222. /***********************************************************************/
  223. /***********************************************************************/
  224.  
  225. ULONG    LIB_Reserved(void)
  226. {
  227.   DEBUG_NULL(dprintf("LIB_Reserved:\n"));
  228.  
  229.   return(0);
  230. }
  231.  
  232.