home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / mac / programm / 19873 < prev    next >
Encoding:
Text File  |  1992-12-15  |  5.3 KB  |  161 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!mcsun!sunic!ugle.unit.no!ugle.unit.no!hanord
  3. From: hanord@rubin.ibt (Haavard Nord)
  4. Subject: How to write a new NewPtr
  5. Message-ID: <HANORD.92Dec16015534@rubin.ibt>
  6. Sender: news@ugle.unit.no (NetNews Administrator)
  7. Organization: Department of Biomedical Engineering
  8. Date: 16 Dec 92 01:55:34
  9. Lines: 150
  10.  
  11. Not that I know how. I've been struggling with Mac assembly for a couple
  12. days, trying to create patches for the NewPtr and DisposePtr traps.
  13.  
  14. My program is an interrupt task (PPC completion routine) that needs to
  15. allocate memory. I had to write my own memory manager because the standard
  16. memory manager goes hay-wire in interrupt mode.
  17.  
  18. My (C) functions are:
  19.  
  20.    pascal void *mem_alloc( unsigned int );
  21.  
  22. and
  23.  
  24.    pascal void mem_free( void * );
  25.  
  26. The NewPtr/DisposePtr traps uses D0, A0 registers instead of the stack, so
  27. I figured out that I needed some assembly routines that contain the new
  28. trap routines and makes calls to mem_alloc() and mem_free().
  29.  
  30. But my (clever, I thought) approach does not work. What did I do wrong?  I
  31. suspect that my code is never called. Instead some NewNewPtr is called
  32. which in turn does some obscure things.  I have included my assembly
  33. code in case there is some Mac-wizard out there that immediatly sees what
  34. this novice has messed up. I warn you people; my assembly experience is
  35. fresh and I've picked it up by studying code with MacBugs.
  36.  
  37. BTW: Anybody wanting a simple, but efficient memory manager ? It has been
  38. tested and seems to work well.
  39.  
  40. Thanx,
  41.  
  42. -hanord
  43.  
  44.  
  45. *****************************************************************************
  46. ** %M%; version %I% %E% %U%
  47. **
  48. ** Low-level OS patches for scserver, implementation -- for Macintosh only
  49. **
  50. ** Author  : Haavard Nord
  51. ** Created : 921201
  52. **
  53. ** Copyright (C) 1992 by DBE, Trondheim. All rights reserved.
  54. **
  55. ** --------------------------------------------------------------------------
  56. ** The memory manager traps defined in this file are used by the scserver
  57. ** application to provide safe memory handling when in interrupt state.
  58. ** The actual memory manager routines are C functions found in memman.C.
  59. ******************************************************************************
  60.  
  61.         MACHINE        MC68020
  62.         
  63.         PRINT        PUSH,OFF    ;don't print include stuff
  64.         INCLUDE        'SysEqu.a'
  65.         INCLUDE        'ToolEqu.a'
  66.         INCLUDE        'Traps.a'
  67.         PRINT        POP
  68.  
  69. NewPtrNum    EQU        $1E
  70. DisposePtrNum    EQU        $1F
  71.  
  72.         SEG        'Main'        ;put code in main segment
  73.         
  74.  
  75. *****************************************************************************
  76. ** Procedure:    pascal void HookMemory()
  77. **
  78. ** Installs a patch for the memory manager newPtr and disposePtr.
  79. ** This procedure should be called from the start of the interrupt code.
  80. ******************************************************************************
  81. HookMemory    PROC        EXPORT
  82.         IMPORT        (OrigNew, OrigDispose, NewPtrPatch, DisposePtrPatch) : CODE
  83.         MOVE.W        #NewPtrNum,D0
  84.         _GetTrapAddress ,NEWTOOL
  85.         LEA.L        OrigNew,A1
  86.         MOVE.L        A0,(A1)        ;save old NewPtr address
  87.         LEA.L        NewPtrPatch,A0
  88.         MOVE.W        #NewPtrNum,D0
  89.         _SetTrapAddress ,NEWTOOL    ;set our NewPtr patch address
  90.         MOVE.W        #DisposePtrNum,D0
  91.         _GetTrapAddress ,NEWTOOL
  92.         LEA.L        OrigDispose,A1
  93.         MOVE.L        A0,(A1)        ;save old DisposePtr address
  94.         LEA.L        DisposePtrPatch,A0
  95.         MOVE.W        #DisposePtrNum,D0
  96.         _SetTrapAddress ,NEWTOOL    ;set our DisposePtr patch address
  97.         RTS
  98.         ENDPROC    ; HookGetResource
  99.  
  100.  
  101. *****************************************************************************
  102. ** Procedure:    pascal void UnHookMemory()
  103. **
  104. ** Removes the patch for the memory manager.
  105. ** This function should be called from the end of the interrupt code.
  106. ******************************************************************************
  107. UnHookMemory    PROC        EXPORT
  108.         IMPORT        (OrigNew,OrigDispose) : CODE
  109.         MOVE.L        OrigNew,A0
  110.         MOVE.W        #NewPtrNum,D0
  111.         _SetTrapAddress ,NEWTOOL    ;restore old newPtr address
  112.         MOVE.L        OrigDispose,A0
  113.         MOVE.W        #DisposePtrNum,D0
  114.         _SetTrapAddress ,NEWTOOL    ;restore old disposePtr address
  115.         RTS
  116.         ENDPROC ; UnHookMemory
  117.  
  118.  
  119. *****************************************************************************
  120. ** Trap:    pascal void *newPtr( unsigned )
  121. **
  122. ** This is our newPtr patch.
  123. ** It calls mem_alloc() in memman.C.
  124. ******************************************************************************
  125. NewPtrPatch    PROC        EXPORT
  126.         IMPORT        (mem_alloc) : CODE
  127.         MOVE.L        D0,-(A7)
  128.         JSR        mem_alloc
  129.         MOVEA.L        (A7)+,A0
  130.         RTS
  131.         ENDPROC    ; NewPtrPatch
  132.  
  133.  
  134. *****************************************************************************
  135. ** Trap:    pascal void disposePtr( void * )
  136. **
  137. ** This is our newPtr patch.
  138. ** It calls mem_alloc() in memman.C.
  139. ******************************************************************************
  140. DisposePtrPatch    PROC        EXPORT
  141.         ENTRY        (OrigNew, OrigDispose) : CODE
  142.         IMPORT        (mem_free) : CODE
  143.         MOVE.L        D0,-(A7)
  144.         JSR        mem_free
  145.         RTS
  146.         
  147. OrigNew        DS.L        0        ;address of old new handler
  148. OrigDispose    DS.L        0        ;address of old dispose handler
  149.  
  150.         ENDPROC    ; DisposePtrPatch
  151.  
  152.         END
  153.  
  154. ---------
  155. --
  156. ===============================================================================
  157.  Haavard Nord                                        email: hanord@ibt.unit.no
  158.  Dept. of Biomedical Engineering                     phone: +47 7 598685
  159.  Trondheim, Norway                                     fax: +47 7 598613
  160. ===============================================================================
  161.