home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!mcsun!sunic!ugle.unit.no!ugle.unit.no!hanord
- From: hanord@rubin.ibt (Haavard Nord)
- Subject: How to write a new NewPtr
- Message-ID: <HANORD.92Dec16015534@rubin.ibt>
- Sender: news@ugle.unit.no (NetNews Administrator)
- Organization: Department of Biomedical Engineering
- Date: 16 Dec 92 01:55:34
- Lines: 150
-
- Not that I know how. I've been struggling with Mac assembly for a couple
- days, trying to create patches for the NewPtr and DisposePtr traps.
-
- My program is an interrupt task (PPC completion routine) that needs to
- allocate memory. I had to write my own memory manager because the standard
- memory manager goes hay-wire in interrupt mode.
-
- My (C) functions are:
-
- pascal void *mem_alloc( unsigned int );
-
- and
-
- pascal void mem_free( void * );
-
- The NewPtr/DisposePtr traps uses D0, A0 registers instead of the stack, so
- I figured out that I needed some assembly routines that contain the new
- trap routines and makes calls to mem_alloc() and mem_free().
-
- But my (clever, I thought) approach does not work. What did I do wrong? I
- suspect that my code is never called. Instead some NewNewPtr is called
- which in turn does some obscure things. I have included my assembly
- code in case there is some Mac-wizard out there that immediatly sees what
- this novice has messed up. I warn you people; my assembly experience is
- fresh and I've picked it up by studying code with MacBugs.
-
- BTW: Anybody wanting a simple, but efficient memory manager ? It has been
- tested and seems to work well.
-
- Thanx,
-
- -hanord
-
-
- *****************************************************************************
- ** %M%; version %I% %E% %U%
- **
- ** Low-level OS patches for scserver, implementation -- for Macintosh only
- **
- ** Author : Haavard Nord
- ** Created : 921201
- **
- ** Copyright (C) 1992 by DBE, Trondheim. All rights reserved.
- **
- ** --------------------------------------------------------------------------
- ** The memory manager traps defined in this file are used by the scserver
- ** application to provide safe memory handling when in interrupt state.
- ** The actual memory manager routines are C functions found in memman.C.
- ******************************************************************************
-
- MACHINE MC68020
-
- PRINT PUSH,OFF ;don't print include stuff
- INCLUDE 'SysEqu.a'
- INCLUDE 'ToolEqu.a'
- INCLUDE 'Traps.a'
- PRINT POP
-
- NewPtrNum EQU $1E
- DisposePtrNum EQU $1F
-
- SEG 'Main' ;put code in main segment
-
-
- *****************************************************************************
- ** Procedure: pascal void HookMemory()
- **
- ** Installs a patch for the memory manager newPtr and disposePtr.
- ** This procedure should be called from the start of the interrupt code.
- ******************************************************************************
- HookMemory PROC EXPORT
- IMPORT (OrigNew, OrigDispose, NewPtrPatch, DisposePtrPatch) : CODE
- MOVE.W #NewPtrNum,D0
- _GetTrapAddress ,NEWTOOL
- LEA.L OrigNew,A1
- MOVE.L A0,(A1) ;save old NewPtr address
- LEA.L NewPtrPatch,A0
- MOVE.W #NewPtrNum,D0
- _SetTrapAddress ,NEWTOOL ;set our NewPtr patch address
- MOVE.W #DisposePtrNum,D0
- _GetTrapAddress ,NEWTOOL
- LEA.L OrigDispose,A1
- MOVE.L A0,(A1) ;save old DisposePtr address
- LEA.L DisposePtrPatch,A0
- MOVE.W #DisposePtrNum,D0
- _SetTrapAddress ,NEWTOOL ;set our DisposePtr patch address
- RTS
- ENDPROC ; HookGetResource
-
-
- *****************************************************************************
- ** Procedure: pascal void UnHookMemory()
- **
- ** Removes the patch for the memory manager.
- ** This function should be called from the end of the interrupt code.
- ******************************************************************************
- UnHookMemory PROC EXPORT
- IMPORT (OrigNew,OrigDispose) : CODE
- MOVE.L OrigNew,A0
- MOVE.W #NewPtrNum,D0
- _SetTrapAddress ,NEWTOOL ;restore old newPtr address
- MOVE.L OrigDispose,A0
- MOVE.W #DisposePtrNum,D0
- _SetTrapAddress ,NEWTOOL ;restore old disposePtr address
- RTS
- ENDPROC ; UnHookMemory
-
-
- *****************************************************************************
- ** Trap: pascal void *newPtr( unsigned )
- **
- ** This is our newPtr patch.
- ** It calls mem_alloc() in memman.C.
- ******************************************************************************
- NewPtrPatch PROC EXPORT
- IMPORT (mem_alloc) : CODE
- MOVE.L D0,-(A7)
- JSR mem_alloc
- MOVEA.L (A7)+,A0
- RTS
- ENDPROC ; NewPtrPatch
-
-
- *****************************************************************************
- ** Trap: pascal void disposePtr( void * )
- **
- ** This is our newPtr patch.
- ** It calls mem_alloc() in memman.C.
- ******************************************************************************
- DisposePtrPatch PROC EXPORT
- ENTRY (OrigNew, OrigDispose) : CODE
- IMPORT (mem_free) : CODE
- MOVE.L D0,-(A7)
- JSR mem_free
- RTS
-
- OrigNew DS.L 0 ;address of old new handler
- OrigDispose DS.L 0 ;address of old dispose handler
-
- ENDPROC ; DisposePtrPatch
-
- END
-
- ---------
- --
- ===============================================================================
- Haavard Nord email: hanord@ibt.unit.no
- Dept. of Biomedical Engineering phone: +47 7 598685
- Trondheim, Norway fax: +47 7 598613
- ===============================================================================
-