home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 534.lha / vmalloc_v1.2 / track.h < prev    next >
C/C++ Source or Header  |  1991-08-08  |  11KB  |  290 lines

  1. #pragma libcall TrackBase TrackItem 1E 18003
  2. #pragma libcall TrackBase FreeTrackedItem 24 801
  3. #pragma libcall TrackBase FreeTrackList 2A 0
  4. #pragma libcall TrackBase AddProcess 30 0
  5. #pragma libcall TrackBase RemoveProcess 36 0
  6. #pragma libcall TrackBase SetTrap 3C 801
  7. #pragma libcall TrackBase GetTrap 42 0
  8. /*pragma libcall TrackBase Printf 48 9802*/
  9. /*pragma libcall TrackBase SPrintf 4E 98A03*/
  10. /*pragma libcall TrackBase FPrintf 54 98A03*/
  11. #pragma libcall TrackBase MemAlloc 5A 1002
  12. #pragma libcall TrackBase MemFree 60 901
  13. #pragma libcall TrackBase CreatePort 66 0802
  14. #pragma libcall TrackBase DeletePort 6C 801
  15. #pragma libcall TrackBase CreateExtIO 72 0802
  16. #pragma libcall TrackBase DeleteExtIO 78 901
  17. #pragma libcall TrackBase TOpenWindow 7E 801
  18. #pragma libcall TrackBase TOpenScreen 84 801
  19. #pragma libcall TrackBase TMemAlloc 8A 1002
  20. #pragma libcall TrackBase TCreatePort 90 0802
  21. #pragma libcall TrackBase TCreateExtIO 96 0802
  22. #pragma libcall TrackBase TOpenDevice 9C 190804
  23. #pragma libcall TrackBase TSetDMRequest A2 9802
  24. #pragma libcall TrackBase TSetMenuStrip A8 9802
  25. #pragma libcall TrackBase TOpenLibrary AE 0902
  26. #pragma libcall TrackBase TOpen B4 2102
  27. #pragma libcall TrackBase Reads BA 32103
  28. #pragma libcall TrackBase OnSignal C0 8002
  29. #pragma libcall TrackBase CreatePacket C6 801
  30. #pragma libcall TrackBase DeletePacket CC 0802
  31. #pragma libcall TrackBase SendPacket D2 8902
  32. #pragma libcall TrackBase WaitPacket D8 901
  33. #pragma libcall TrackBase DoPacket DE 8902
  34. #pragma libcall TrackBase TCreatePacket E4 801
  35. #pragma libcall TrackBase MakeBStr EA 801
  36. #pragma libcall TrackBase CheckPacket F0 901
  37. #pragma libcall TrackBase NewList F6 801
  38. #pragma libcall TrackBase SetJmp FC 801
  39. #pragma libcall TrackBase LongJmp 102 801
  40. #pragma libcall TrackBase ChkBound 108 12003
  41. #pragma libcall TrackBase ChkRange 10E 12003
  42. #pragma libcall TrackBase AbsChk 114 1002
  43. #pragma libcall TrackBase AbsChkWord 11A 1002
  44. #pragma libcall TrackBase ExceptInit 120 801
  45. #pragma libcall TrackBase ExceptEnter 126 801
  46. #pragma libcall TrackBase ExceptLeave 12C 0
  47. #pragma libcall TrackBase ExceptCall 132 001
  48. #pragma libcall TrackBase ExceptRaise 138 A002
  49. #pragma libcall TrackBase ExceptCode 13E 0
  50. #pragma libcall TrackBase TrapMsg 144 001
  51. #pragma libcall TrackBase TLock 14A 2102
  52. #pragma libcall TrackBase Strchr 150 1802
  53. #pragma libcall TrackBase Strcat 156 9802
  54. #pragma libcall TrackBase Atol 15C 801
  55.  
  56. /*
  57.  *
  58.  * Tracking routines.
  59.  *
  60.  */
  61.  
  62. #include "exec/types.h"
  63. #include <exec/lists.h>
  64. #include <exec/nodes.h>
  65. #include "exec/libraries.h"
  66. #include "libraries/dos.h"
  67. #include "intuition/intuition.h"
  68.  
  69. #include <setjmp.h>
  70.  
  71. /*
  72.  * Exception system.
  73.  *
  74.  * This handler is special because it requires only one support routine, and
  75.  * you write it! And it uses only AmigaDos List functions.
  76.  * 
  77.  * Usage :
  78.  *
  79.  * 1) In the HIGHEST routine that uses the exceptions, (often main()), place
  80.  *    the line 'EINIT(handlername) ;' to initialise the exception handler.
  81.  *    OR, you may use 'EHANDLERINIT()' to initialise the default handler
  82.  *    found in d.lib
  83.  *
  84.  * 2) At the beginning of the code to be protected, place the word
  85.  *    PROTECTED to define the start of the section. 
  86.  *
  87.  * 3) Place the protected code.
  88.  *
  89.  * 4) Place the work 'EXCEPTION' on a line of its own, to declare the
  90.  *    beginning of the code to jump to in case of an exception.
  91.  *
  92.  * 5) Place the exception code. Within the exception code, the reason for the
  93.  *    exception can be found via the function 'LASTEXCEPTION()' which returns
  94.  *    a long.
  95.  * 
  96.  * 6) At the end of the exception code, place the word NORMAL. This
  97.  *    terminates the exception code. Any code after this is executed whether
  98.  *    and exception occurred or not. NOTE that it is not legal to 'return'
  99.  *    from within the PROTECTed or EXCEPTION zone. NOTE that after a 'NORMAL'
  100.  *    any exception will cause the code to jump to an exception handler at
  101.  *    a higher level (eg, the handler code of the calling function.)
  102.  *
  103.  * To cause an exception to jump to the exception code (or from the
  104.  * exception code to the callers exception code), use the 'RAISEEXCEPTION'
  105.  * macro. NOTE that this acts differently depending on whether you are in 
  106.  * a protected zone or a exception zone.
  107.  *
  108.  * LASTLY, you must make an exception handler. This is an example
  109.  *
  110.  * void * myexception(void) {
  111.  *    long *codes = GetTrap() ;            GetTrap() is MY code to get the
  112.  *                                      reason for the exception. 
  113.  *    CALLEXCEPTION(codes[0]) ;            Go to the Exception handler.
  114.  *    }                                 This macro also sets the CONTEXT
  115.  *                                        to EXCEPTIONZONE.
  116.  */         
  117.  
  118.  
  119. void __stdargs _DefaultEHandler(void) ;
  120.  
  121. #define EINIT(handler)  ExceptInit(handler)
  122. #define EHANDLERINIT()  EINIT(_DefaultEHandler)
  123.  
  124. #define PROTECTED  if (!ExceptEnter(0)) {
  125. #define PROTECT(x) if (!ExceptEnter(x)) {
  126. #define EXCEPTION     } else {
  127. #define NORMAL      } ExceptLeave() ;
  128. /* #define CALLEXCEPTION(code) ExceptCall(code) */
  129. #define RAISENAMEDEXCEPTION(code,name) ExceptRaise(code,name)
  130. #define RAISEEXCEPTION(code) ExceptRaise(code,0L)
  131. #define LASTEXCEPTION() ExceptCode()
  132. #define  TRACK_VERSION    (5)
  133.  
  134. /*
  135.  * Possible exceptions codes returned by 'GetTrap' after an exception.
  136.  *
  137.  * NOTE: EX_* are processor exception codes, EXT_* are extended codes...
  138.  */
  139. typedef enum { EX_NONE, EX_TRAP, EX_BUS_ERROR, EX_ADDRESS_ERROR, EX_ILLEGAL,
  140.                EX_DIVIDE_BY_ZERO, EX_CHK, EX_TRAPV, EX_PRIVILEDGE, EX_TRACE,
  141.                EX_LINE_1010, EX_LINE_1111, EX_MC68000,
  142.            EXT_ASSERT_FAIL
  143.            } EX_CODES ;
  144.  
  145. typedef enum { PROTECTZONE, EXCEPTIONZONE } context ;
  146.  
  147. typedef struct {
  148.    struct MinNode Next ;
  149.    char *EI_name ;
  150.    jmp_buf EI_Save ;
  151.    unsigned long EI_Code ;
  152.    unsigned char EI_Context ;
  153.    } EInstance ;
  154.  
  155. struct TrackNode {
  156.     struct TrackNode *tn_Next;
  157.     long tn_Item;
  158.     void (*tn_drop)(long item, void *func, long param) ;
  159.     long tn_param;
  160.     } ;
  161.  
  162. struct TrapInfo {
  163.     long  ti_TrapNO;
  164.     long  ti_TrapPC;
  165.     long  ti_TrapMsg;
  166.     } ;
  167.  
  168. struct ProcessList {
  169.     struct ProcessList *pl_Next;
  170.     long        pl_TaskID;
  171.     struct TrackNode *pl_List;
  172.     long        pl_loaderdb;
  173.     long        pl_TrapCode;
  174.     long        pl_TrapData;
  175.     long        pl_Handler;
  176.     long        pl_ExceptCode;
  177.     long        pl_ExceptData;
  178.     struct    TrapInfo pl_TrapInfo;
  179.     long        pl_ExceptMask;
  180.     long        pl_ExceptVec;
  181.     struct        MinList pl_EHandler;        /* Exception handler list */
  182.     } ;
  183.  
  184. struct TrackBase {
  185.     struct Library tb_Lib ;
  186.     ULONG    tb_SegList;
  187.     ULONG    tb_Flags;
  188.     APTR     tb_ExecBase;
  189.     APTR     tb_Anchor;
  190.     struct Library *tb_DOSBase;
  191.     struct Library *tb_IntuitionBase;
  192.     struct Library *tb_GfxBase;
  193.     struct Library *tb_LayersBase;
  194.     LONG     tb_Data;
  195.     } ;
  196.  
  197. extern struct TrackBase *TrackBase ;
  198.  
  199. extern void AddProcess(void);
  200. extern void RemoveProcess(void);
  201. extern void TrackItem(void *item,void (*function)(),long param) ;
  202. extern void FreeTrackedItem(void *item);
  203. extern void FreeTrackList(void);
  204. extern void SetTrap(void (*func)());
  205. extern void OnSignal(int signum,void (*func)());
  206. extern struct TrapInfo *GetTrap(void);
  207. extern char *TrapMsg(int code);
  208. extern long Printf(char *fmt,...);
  209. extern long VPrintf(char *fmt,void *args,...);
  210. extern long FPrintf(BPTR fh, char *fmt, ...) ;
  211. extern long VFPrintf(BPTR fh, char *fmt,void *args) ;
  212. extern long __stdargs SPrintf(char *dest, char *fmt, ...) ;
  213. extern long __stdargs VSPrintf(char *dest, char *fmt, void *vec) ;
  214. extern void *MemAlloc(long size, long type) ;
  215. extern void MemFree(char *block) ;
  216. extern struct MsgPort *CreatePort(char *name, long pri) ;
  217. extern void DeletePort(struct MsgPort *port) ;
  218. extern struct IORequest *CreateExtIO(struct MsgPort *ioreply, long size) ;
  219. extern void DeleteExtIO(struct IORequest *ioreq) ;
  220. extern void DeletePacket(struct StandardPacket *pk,long myreply) ;
  221. extern struct StandardPacket *CreatePacket(struct MsgPort *reply) ;
  222. extern long DoPacket(struct StandardPacket *pk, struct MsgPort *dest) ;
  223. extern struct StandardPacket *SendPacket(struct StandardPacket *pk, struct MsgPort *dest) ;
  224. extern long WaitPacket(struct StandardPacket *pk) ;
  225. extern struct StandardPacket *CheckPacket(struct StandardPacket *pk) ;
  226.  
  227. /*
  228.  * Auto-tracking functions.
  229.  */
  230.  
  231. extern struct Window *TOpenWindow( struct NewWindow *nw ) ;
  232. extern struct Screen *TOpenScreen( struct NewScreen *screen ) ;
  233. extern char *TMemAlloc(long size, long type) ;
  234. extern struct MsgPort *TCreatePort(char *name, long pri) ;
  235. extern struct IORequest *TCreateExtIO(struct MsgPort *ioreply, long size) ;
  236. extern long TOpenDevice(char *devname,long unit,struct IORequest *ioRequest,long flags) ;
  237. extern char TSetDMRequest(struct Window *win,struct Requester *req) ;
  238. extern char TSetMenuStrip(struct Window *win,struct Menu *mn) ;
  239. extern struct Library *TOpenLibrary(char *lib,int rev) ;
  240. extern BPTR TOpen(char *name, int mode) ;
  241. extern BPTR TLock(char *name, int mode) ;
  242. extern struct StandardPacket *TCreatePacket(struct MsgPort *reply) ;
  243.  
  244. /*
  245.  * Misc
  246.  */
  247. extern char *Reads(BPTR fh,char *buf, int len) ;
  248. extern BPTR MakeBStr(char *str) ;
  249. extern void NewList(struct List *list) ;
  250. extern void SetJmp(jmp_buf list) ;
  251. extern void LongJmp(jmp_buf list) ;
  252.  
  253. /*
  254.  * Although the Range checking function is a single function, there are
  255.  * several points you can jump into it, depending on what you wish to check.
  256.  */
  257.  
  258. /*
  259.  * This accepts a lowbound and a highbound. It makes sure the variable is
  260.  * within the range of low->high.
  261.  */
  262. extern void ChkBound(long var,long min,long max) ;
  263. /*
  264.  * This accepts a lowbound and a size. It makes sure the variable is
  265.  * within the range of low->low+size.
  266.  */
  267. extern void ChkRange(long var,long min,long size) ;
  268. /*
  269.  * This accepts a maximum. It makes sure the variable is
  270.  * within the range of 0->maximum. Note that since CHK is a 16bit
  271.  * instruction, the high word is checked before the low word.
  272.  */
  273. extern void AbsChk(long var,long max) ;
  274. /*
  275.  * This accepts a maximum. It makes sure the variable is
  276.  * within the range of 0->maximum. This works for SHORTS, where the upper
  277.  * 16 bits are not checked.
  278.  */
  279. extern void AbsChkWord(short var,long max) ;
  280. extern void ExceptInit(void *func) ;
  281. extern long ExceptEnter(char *name) ;
  282. extern void ExceptLeave(void) ;
  283. extern void ExceptCall(long code) ;
  284. extern void ExceptRaise(long code,char *name) ;
  285. extern long ExceptCode(void) ;
  286. extern char *Strchr(char *str, int val) ;
  287. extern char *Strcat(char *str, char *data) ;
  288. extern long Atol(char *str) ;
  289. #define ASSERT(x) if (!(x)) RAISEEXCEPTION(EXT_ASSERT_FAIL,0)
  290.