home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / hobby / palmoon / palmoon.EXE / startup.c < prev   
Encoding:
C/C++ Source or Header  |  1998-09-10  |  7.9 KB  |  279 lines

  1. /* MathLib: Pilot shared library of IEEE-754 double math functions
  2.  *
  3.  * Main library entry point and control routines; see file shlib.htm
  4.  * (many thanks to Ian Goldberg) for details on how shared libraries
  5.  * are structured and controlled.  This module is written for the
  6.  * MetroWerks CodeWarrior C compiler, and will require significant
  7.  * changes to compile cleanly with anything else.
  8.  *
  9.  * Copyright (C) 1997 Rick Huebner
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU Library General Public License as
  13.  * published by the Free Software Foundation; either version 2 of
  14.  * the License, or (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU Library General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU Library General Public License
  22.  * along with this program; see file COPYING.LIB.  If not, write to the
  23.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24.  * Boston, MA 02111-1307, USA
  25.  *
  26.  * Version 1.01, 23 August 1997, Rick Huebner
  27.  */
  28. #include <Pilot.h>
  29. #include <SysAll.h>
  30.  
  31. #include "MathLib.h"
  32. #include "MathLibPrv.h"
  33.  
  34. // Convenient macros for defining the dispatch table (below)
  35. #define NFUNCS                51    // Number of entry points in library
  36. #define SIZEOF_OFFSET        sizeof(Word)
  37. #define SIZEOF_OFFSET_TABLE    ((1+NFUNCS) * SIZEOF_OFFSET)    // for name, plus one per function
  38. #define SIZEOF_JMP            4    // Memory model/platform dependent
  39.  
  40. #define LIBENTRY_OFFSET(x)    (SIZEOF_OFFSET_TABLE + x * SIZEOF_JMP)
  41. #define LIBNAME_OFFSET        (SIZEOF_OFFSET_TABLE + NFUNCS * SIZEOF_JMP)
  42.  
  43.  
  44.  
  45. /* Main entry point of the library, automatically invoked by the
  46.  * OS when the library is loaded.  Fills in the SysLibTblEntry
  47.  * structure passed by the OS to publish the address of
  48.  * our dispatch table.  This MUST be the first routine in
  49.  * the code segment.  By naming it __Startup__, we sneakily
  50.  * replace the normal startup code that Codewarrior would
  51.  * usually insert for a regular application.  The only bad
  52.  * part about this trick is that we have to put up with the
  53.  * linker complaining about it; just ignore the error.
  54.  */
  55. Err __Startup__(UInt refnum, SysLibTblEntryPtr entryP) {
  56. #pragma unused(refnum)
  57.     // Install pointer to our dispatch table
  58.     entryP->dispatchTblP = DispatchTable();
  59.     
  60.     // Initialize globals handle to zero until mathlib_open called
  61.     entryP->globalsP = 0;
  62.     
  63.     return mlErrNone;
  64. }
  65.  
  66.  
  67.  
  68. /* Library dispatch table.  Offset of library name, followed
  69.  * by offset of each function's jmp instruction, followed by
  70.  * a jmp for each function, followed by the library name used
  71.  * for SysLibFind() calls.
  72.  */
  73. asm Ptr *DispatchTable(void) {
  74.     lea        table, a0
  75.     rts
  76.  
  77. table:
  78.     dc.w    LIBNAME_OFFSET
  79.     dc.w    LIBENTRY_OFFSET(0)    // Must start from 0
  80.     dc.w    LIBENTRY_OFFSET(1)
  81.     dc.w    LIBENTRY_OFFSET(2)
  82.     dc.w    LIBENTRY_OFFSET(3)
  83.     dc.w    LIBENTRY_OFFSET(4)
  84.     dc.w    LIBENTRY_OFFSET(5)
  85.     dc.w    LIBENTRY_OFFSET(6)
  86.     dc.w    LIBENTRY_OFFSET(7)
  87.     dc.w    LIBENTRY_OFFSET(8)
  88.     dc.w    LIBENTRY_OFFSET(9)
  89.     dc.w    LIBENTRY_OFFSET(10)
  90.     dc.w    LIBENTRY_OFFSET(11)
  91.     dc.w    LIBENTRY_OFFSET(12)
  92.     dc.w    LIBENTRY_OFFSET(13)
  93.     dc.w    LIBENTRY_OFFSET(14)
  94.     dc.w    LIBENTRY_OFFSET(15)
  95.     dc.w    LIBENTRY_OFFSET(16)
  96.     dc.w    LIBENTRY_OFFSET(17)
  97.     dc.w    LIBENTRY_OFFSET(18)
  98.     dc.w    LIBENTRY_OFFSET(19)
  99.     dc.w    LIBENTRY_OFFSET(20)
  100.     dc.w    LIBENTRY_OFFSET(21)
  101.     dc.w    LIBENTRY_OFFSET(22)
  102.     dc.w    LIBENTRY_OFFSET(23)
  103.     dc.w    LIBENTRY_OFFSET(24)
  104.     dc.w    LIBENTRY_OFFSET(25)
  105.     dc.w    LIBENTRY_OFFSET(26)
  106.     dc.w    LIBENTRY_OFFSET(27)
  107.     dc.w    LIBENTRY_OFFSET(28)
  108.     dc.w    LIBENTRY_OFFSET(29)
  109.     dc.w    LIBENTRY_OFFSET(30)
  110.     dc.w    LIBENTRY_OFFSET(31)
  111.     dc.w    LIBENTRY_OFFSET(32)
  112.     dc.w    LIBENTRY_OFFSET(33)
  113.     dc.w    LIBENTRY_OFFSET(34)
  114.     dc.w    LIBENTRY_OFFSET(35)
  115.     dc.w    LIBENTRY_OFFSET(36)
  116.     dc.w    LIBENTRY_OFFSET(37)
  117.     dc.w    LIBENTRY_OFFSET(38)
  118.     dc.w    LIBENTRY_OFFSET(39)
  119.     dc.w    LIBENTRY_OFFSET(40)
  120.     dc.w    LIBENTRY_OFFSET(41)
  121.     dc.w    LIBENTRY_OFFSET(42)
  122.     dc.w    LIBENTRY_OFFSET(43)
  123.     dc.w    LIBENTRY_OFFSET(44)
  124.     dc.w    LIBENTRY_OFFSET(45)
  125.     dc.w    LIBENTRY_OFFSET(46)
  126.     dc.w    LIBENTRY_OFFSET(47)
  127.     dc.w    LIBENTRY_OFFSET(48)
  128.     dc.w    LIBENTRY_OFFSET(49)
  129.     dc.w    LIBENTRY_OFFSET(50)    // NFUNCS must equal this index + 1
  130.     jmp        mathlib_open    // These must match the sequence in MathLib.h
  131.     jmp        mathlib_close
  132.     jmp        mathlib_sleep
  133.     jmp        mathlib_wake
  134.     jmp        mathlib_acos
  135.     jmp        mathlib_asin
  136.     jmp        mathlib_atan
  137.     jmp        mathlib_atan2
  138.     jmp        mathlib_cos
  139.     jmp        mathlib_sin
  140.     jmp        mathlib_tan
  141.     jmp        mathlib_sincos
  142.     jmp        mathlib_cosh
  143.     jmp        mathlib_sinh
  144.     jmp        mathlib_tanh
  145.     jmp        mathlib_acosh
  146.     jmp        mathlib_asinh
  147.     jmp        mathlib_atanh
  148.     jmp        mathlib_exp
  149.     jmp        mathlib_frexp
  150.     jmp        mathlib_ldexp
  151.     jmp        mathlib_log
  152.     jmp        mathlib_log10
  153.     jmp        mathlib_modf
  154.     jmp        mathlib_expm1
  155.     jmp        mathlib_log1p
  156.     jmp        mathlib_logb
  157.     jmp        mathlib_log2
  158.     jmp        mathlib_pow
  159.     jmp        mathlib_sqrt
  160.     jmp        mathlib_hypot
  161.     jmp        mathlib_cbrt
  162.     jmp        mathlib_ceil
  163.     jmp        mathlib_fabs
  164.     jmp        mathlib_floor
  165.     jmp        mathlib_fmod
  166.     jmp        mathlib_isinf
  167.     jmp        mathlib_finite
  168.     jmp        mathlib_scalbn
  169.     jmp        mathlib_drem
  170.     jmp        mathlib_significand
  171.     jmp        mathlib_copysign
  172.     jmp        mathlib_isnan
  173.     jmp        mathlib_ilogb
  174.     jmp        mathlib_rint
  175.     jmp        mathlib_nextafter
  176.     jmp        mathlib_remainder
  177.     jmp        mathlib_scalb
  178.     jmp        mathlib_round
  179.     jmp        mathlib_trunc
  180.     jmp        mathlib_signbit
  181.     dc.b    MathLibName
  182. }
  183.  
  184.  
  185.  
  186. /* Called by the application to validate the library's version number
  187.  * and prepare it for use.
  188.  */
  189. Err mathlib_open(UInt refnum, UInt version) {
  190.     SysLibTblEntryPtr entryP;
  191.     VoidHand mlh;
  192.     MathLibData *mld = NULL;
  193.  
  194.     // Fail if the calling program is expecting a higher version
  195.     // of the library than it's really opening; this protects the
  196.     // program from accidentally using a version of MathLib which
  197.     // doesn't include all the functions it needs.  Version numbers
  198.     // less than ours are OK, since presumably we still have all the
  199.     // old functions from that version available.
  200.     if (version > MathLibVersion)
  201.         return mlErrOldVersion;
  202.  
  203.     // Get our global data handle from the OS            
  204.     entryP = SysLibTblEntry(refnum);
  205.     mlh = (VoidHand)entryP->globalsP;
  206.     
  207.     // If we've already set up our globals, just increment the use count
  208.     if (mlh) {
  209.         mld = MemHandleLock(mlh);
  210.         ++mld->usecount;
  211.     // Otherwise, allocate space for the globals and initialize them
  212.     } else {
  213.         mlh = MemHandleNew(sizeof(MathLibData));
  214.         if (!mlh)
  215.             return mlErrNoMemory;
  216.         entryP->globalsP = (void *)mlh;
  217.         mld = MemHandleLock(mlh);
  218.         // By default, all allocated memory belongs to the app that was
  219.         // running when it was allocated, and is freed automatically
  220.         // when that app closes.  By changing the owner of this memory
  221.         // hunk to 0 (the OS), it won't be freed until we say so.
  222.         MemPtrSetOwner(mld, 0);
  223.         mld->usecount = 1;
  224.     }
  225.     MemHandleUnlock(mlh);
  226.     
  227.     return mlErrNone;
  228. }
  229.  
  230.  
  231.  
  232. /* Called by the application to release any resources used, and to
  233.  * get the current use count to determine if SysLibRemove() should
  234.  * be called.
  235.  */
  236. Err mathlib_close(UInt refnum, UIntPtr usecountP) {
  237.     SysLibTblEntryPtr entryP;
  238.     VoidHand mlh;
  239.     MathLibData *mld;
  240.     
  241.     // Get our global data pointer from the OS            
  242.     entryP = SysLibTblEntry(refnum);
  243.     mlh = (VoidHand)entryP->globalsP;
  244.     
  245.     // If globals aren't set up, we've haven't been opened yet
  246.     if (!mlh)
  247.         return mlErrNotOpen;
  248.  
  249.     // Decrement use count; if this makes it zero, free our globals        
  250.     mld = MemHandleLock(mlh);
  251.     *usecountP = --mld->usecount;
  252.     MemHandleUnlock(mlh);
  253.     
  254.     if (!*usecountP) {
  255.         MemHandleFree(mlh);
  256.         entryP->globalsP = 0;
  257.     }
  258.     
  259.     return mlErrNone;
  260. }
  261.  
  262.  
  263.  
  264. /* Called by OS when Pilot sleeps.  We don't care.
  265.  */
  266. Err mathlib_sleep(UInt refnum) {
  267. #pragma unused(refnum)
  268.     return mlErrNone;
  269. }
  270.  
  271.  
  272.  
  273. /* Called by OS when Pilot wakes.  We don't care.
  274.  */
  275. Err mathlib_wake(UInt refnum) {
  276. #pragma unused(refnum)
  277.     return mlErrNone;
  278. }
  279.